如何使用 JavaScript 暂停和恢复 CSS3 动画?
我尝试用谷歌搜索并从这个论坛寻找解决我的问题的方法,但到目前为止还没有运气。我想通过单击图片暂停 CSS3 动画(图像幻灯片放映),并通过单击图片恢复相同的动画。
我知道如何暂停幻灯片放映,并且也能够恢复幻灯片放映一次,但如果尝试暂停和恢复多次,它就会停止工作。我的代码如下所示:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
.pic {
position: absolute;
opacity: 0;
}
#pic1 {
-webkit-animation: pic1 4s infinite linear;
}
#pic2 {
-webkit-animation: pic2 4s infinite linear;
}
@-webkit-keyframes pic1 {
0% {opacity: 0;}
5% {opacity: 1;}
45% {opacity: 1;}
50% {opacity: 0;}
100% {opacity: 0;}
}
@-webkit-keyframes pic2 {
0% {opacity: 0;}
50% {opacity: 0;}
55% {opacity: 1;}
95% {opacity: 1;}
100% {opacity: 0;}
}
</style>
<script type="text/javascript">
function doStuff(){
var pic1 = document.getElementById("pic1");
var pic2 = document.getElementById("pic2");
pic1.style.webkitAnimationPlayState="paused";
pic2.style.webkitAnimationPlayState="paused";
pic1.onclick = function(){
pic1.style.webkitAnimationPlayState="running";
pic2.style.webkitAnimationPlayState="running";
}
pic2.onclick = function(){
pic1.style.webkitAnimationPlayState="running";
pic2.style.webkitAnimationPlayState="running";
}
}
</script>
</head>
<body>
<img id="pic1" class="pic" src="photo1.jpg" />
<img id="pic2" class="pic" src="photo2.jpg" onclick="doStuff()" />
</body>
</html>
我不想使用任何 JS 库(例如 jQuery)或任何其他外部解决方案。
我的猜测是,doStuff
函数中的函数仍在运行,这就是为什么 pause
和 resume
只能工作一次。
有没有办法在我点击一次这些功能后清除它们?或者我试图以完全错误的方式做到这一点?感谢帮助。 :)
I've tried to google and look from this forum a solution for my problem but no luck so far. I would like to pause my CSS3 animation (image slide show) by clicking a picture and also resume to the same animation by clicking a picture.
I know how to pause the slide show and I was also able to resume it once, but then it stops working if try to pause and resume more than one time. Here is how my code looks like:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
.pic {
position: absolute;
opacity: 0;
}
#pic1 {
-webkit-animation: pic1 4s infinite linear;
}
#pic2 {
-webkit-animation: pic2 4s infinite linear;
}
@-webkit-keyframes pic1 {
0% {opacity: 0;}
5% {opacity: 1;}
45% {opacity: 1;}
50% {opacity: 0;}
100% {opacity: 0;}
}
@-webkit-keyframes pic2 {
0% {opacity: 0;}
50% {opacity: 0;}
55% {opacity: 1;}
95% {opacity: 1;}
100% {opacity: 0;}
}
</style>
<script type="text/javascript">
function doStuff(){
var pic1 = document.getElementById("pic1");
var pic2 = document.getElementById("pic2");
pic1.style.webkitAnimationPlayState="paused";
pic2.style.webkitAnimationPlayState="paused";
pic1.onclick = function(){
pic1.style.webkitAnimationPlayState="running";
pic2.style.webkitAnimationPlayState="running";
}
pic2.onclick = function(){
pic1.style.webkitAnimationPlayState="running";
pic2.style.webkitAnimationPlayState="running";
}
}
</script>
</head>
<body>
<img id="pic1" class="pic" src="photo1.jpg" />
<img id="pic2" class="pic" src="photo2.jpg" onclick="doStuff()" />
</body>
</html>
I don't want to use any JS libraries (e.g. jQuery) or any other external solution.
My guess is that my functions inside doStuff
function are still running and that's why pause
and resume
works only once.
Is there a way to clear these functions after I have clicked them once? Or am I trying to do this in a totally wrong way? Help is appreciated. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我发现使用 css 类更容易做到这一点。有了它,您可以为每个浏览器使用前缀。
然后,您只需将此类添加或删除到动画元素即可暂停/恢复动画。
I find it easier to do it with a css class. With it, you can use prefixes for every browser.
Then you only have to add or remove this class to your animated element yo pause / resume the animation.
这是一个使用javascript的解决方案:
jQuery 解决方案(更短且更具可读性):
Here is a solution using javascript:
jQuery solution (shorter and more readable):
这是为了扩展 Luis Hijarrubia 给出的答案
。在我的页面中,我触发了父元素的类更改。我想做的就是旋转里面的图像。但似乎因为我没有使用悬停,所以添加暂停类对动画状态没有任何影响。
使用 !important 可确保这些值被新添加的 CSS 值覆盖。
This is to extend the answer given by Luis Hijarrubia
In my page I was triggering the change of class from the parent element. All I wanted to do was rotate the image inside. But it seems that because I was not using the hover, the adding of the paused class made no difference to the animation state.
The use of !important ensured that these values were overwritten by the new added CSS values.
我还没有测试过,但也许是这样的?
I havent tested it but perhaps something like this?