无法在一个循环结束时停止动画

问题描述:

我在一分钟内制作一个CSS动画,并在其中移动东西,并希望它停留在最后位置,直到用户将鼠标移开。无法在一个循环结束时停止动画

body { 
    background: url('osx.jpg'); 
    padding: 0; 
    margin: 0; 
    line-height: 60px; 
} 

@-webkit-keyframes item1 { 
    0% { bottom: -120px; left: 0px; } 
    10% { bottom: -40px; left: 10px; -webkit-transform: rotate(5deg); } 
    100% { bottom: -40px; left: 10px; -webkit-transform: rotate(5deg); } 
    } 

@-webkit-keyframes item2 { 
    0% { bottom: -120px; left: 0px; } 
    10% { bottom: 60px; left: 20px; -webkit-transform: rotate(7deg); } 
    100% { bottom: 60px; left: 20px; -webkit-transform: rotate(7deg); } 
} 

@-webkit-keyframes item3 { 
    0% { bottom: -120px; left: 0px; } 
    10% { bottom: 160px; left: 30px; -webkit-transform: rotate(9deg); } 
    100% { bottom: 160px; left: 30px; -webkit-transform: rotate(9deg); } 
} 

    div { 
    position: relative; 
    } 
#folder { 
    width: 120px; 
    margin: 0px auto; 
} 

#folder > div { 
    position: absolute; 
} 

#folder:hover > div:nth-of-type(1) { 
    -webkit-animation-name: item1; 
    -webkit-animation-duration: 10s; 
} 

#folder:hover > div:nth-of-type(2) { 
    -webkit-animation-name: item2; 
    -webkit-animation-duration: 10s; 
} 

#folder:hover > div:nth-of-type(3) { 
    -webkit-animation-name: item3; 
    -webkit-animation-duration: 10s; 
} 

只要动画结束,它只是重复自己。我只希望它发生一次,并保持它的位置,直到用户离开。我尝试了使用规范中暂停的东西,但它并不像我预期的那样工作。任何帮助表示赞赏。谢谢。 :)

以下评论适合我。感谢迈克尔

“你需要添加填充模式在动画的最后冻结动画状态。

-webkit-animation-fill-mode: forwards; 

forwards离开动画的最后一帧的状态

backwards在开始处留下动画

+1

这工作!谢谢! – 2011-06-13 08:23:23

+0

我知道这有点迟,但正确的方式是动画播放状态:https://developer.mozilla.org/en-US/docs/Web/CSS/animation-play-state – Marco 2017-10-18 12:44:55

试试这个:

-webkit-animation-duration: 10s, 10s; 

:)

+0

将动画超过10秒钟并且超过10秒。 – 2010-06-22 09:59:28

+0

为你工作? – 2010-06-23 10:19:11

+0

还是什么都没有,我决定让这个动画真的很长(比如10,000秒),这样用户不会在这段时间内悬停在它上面; D – Johnny 2010-07-03 23:25:04

的CSS3动画重置为默认样式在动画的结尾。您可以使用javascript尝试:

您可以为动画添加一个eventListener,检索想要保留的样式,手动应用它们并删除动画。

document.getElementById('yourdiv').addEventListener(
    'webkitAnimationEnd', 
    function(){ 
     document.getElementById('yourdiv').style.backgroundPosition = '0px 0px'; 
     document.getElementById('yourdiv').style.webkitAnimationName = 'none'; 
    }, 
    false 
); 
+0

这是真的,但是我正在进行的实验是尝试并只用CSS来完成。感谢您尝试 – Johnny 2011-02-17 21:12:09

+5

您需要添加填充模式以冻结动画结束时的动画状态。 “-webkit-animation-fill-mode:forwards”你不应该让动画超长 - 这会咀嚼用户的CPU - CSS动画非常耗费CPU资源。 – 2011-04-14 18:36:44

+0

@Michael有趣!感谢你,我不知道它存在。我会尝试它,并将在我的下一个项目中使用它。 – Stef 2011-04-15 01:32:14

如果我正确地理解了这个问题,听起来好像你需要设置迭代为'1'。

-webkit-animation-iteration-count: 1; 
+0

就是这样,谢谢一堆! – 2017-02-15 12:03:37

为了防止动画重置为第一帧,请注意订单,其中声明了css:

这是工作的罚款:

animation: yourAnimationName 1s; 
    animation-fill-mode: forwards; 

这不会(复位到第一帧):

animation-fill-mode: forwards; 
    animation: yourAnimationName 1s; 

整蛊!

+0

谢谢!任何理由为什么这是? – mariachimike 2014-01-09 00:35:41

+0

发生这种情况是因为'animation'是所有'animation- *'属性的缩写,并且设置'animation'会覆盖所有'animation- *'属性。这种行为与其他的短手势(例如'background'或'border')相同。如果您使用'动画'缩写并且不定义填充模式,则默认情况下将使用'none'。 – 2015-05-08 06:10:57

+0

好点!我不知道为什么这发生在我身上! – 2015-09-09 10:15:42