答:使用jQuerycss()方法
您可以将jQuerycss()方法与CSS3animation-play-state属性结合使用,以在循环的中间播放和停止CSS动画。
让我们看下面的示例,以了解其基本工作原理:
例试试这个代码»
<!DOCTYPE html>
<html lang="en">
<head>
<title>jQuery Play and Stop CSS Animation</title>
<style>
.animated {
height: 200px;
margin: 10px 0;
background: url("smiley.png") no-repeat left center #e4eacf;
-webkit-animation: test 4s infinite; /* Chrome, Safari, Opera */
animation: test 4s infinite;
}
/* Chrome, Safari, Opera */
@-webkit-keyframes test {
50% {background-position: right center;}
}
/* Standard syntax */
@keyframes test {
50% {background-position: right center;}
}
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
$(".play-animation").click(function(){
$(".animated").css("animation-play-state", "running");
});
$(".stop-animation").click(function(){
$(".animated").css("animation-play-state", "paused");
});
});
</script>
</head>
<body>
<div class="animated"></div>
<button type="button" class="play-animation">Play Animation</button>
<button type="button" class="stop-animation">Stop Animation</button>
<p><strong>Warning:</strong> CSS Animations do not work in Internet Explorer 9 and earlier versions.</p>
</body>
</html>
如果您觉得本文的内容对您的学习有所帮助:
关键字:
jQuery