开发者

CSS3 Animation Question

开发者 https://www.devze.com 2022-12-20 03:43 出处:网络
How can I make a CSS3 Animation play to the end and then stop dead. I don\'t want it to return the elements being transformed back to their initial states.

How can I make a CSS3 Animation play to the end and then stop dead. I don't want it to return the elements being transformed back to their initial states.

Right now I'm using some javascript to add a class to the element after the animation's duration with the same properties as 1开发者_StackOverflow00% in the animation.


This is possible with the "animation-fill-mode" defined as "forwards", at least in Webkit. I got this result with code like this:

@-webkit-keyframes test {
  100% { background-color: #0000ff; }
} 

a { background-color: #ff0000; }

a:hover { -webkit-animation: test 1s 1 ease forwards }

Note that specifying start color in 0% keyframe and end color in :hover was not necessary.

Of course, this code is Webkit specific. I haven't tried in other browsers with other vendor prefixes or with the general "animation" property.


put your end values in the main css class and the start values in the animation keyframes at 0%:

@keyframes test {
  0% {
    background-color: #ff0000; /* start value */
  }
  100% {
    background-color: #0000ff;
  } 
}

a {
  background-color: #ff0000; /* normal state */
}

a:hover {
  animation-name: test;
  animation-duration: 1s;
  background-color: #ff0000; /* final state, after animation is finished */
}


In case this question is still open, I don't think this is possible using CSS3 animations as they're currently specified:

An animation does not affect the computed value before the application of the animation, before the animation delay has expired, and after the end of the animation.

However, you should be able to use CSS3 transitions for basic effects. There's a slide in the html5rocks.com presentation that shows how to do this. Here's the relevant [paraphrased] excerpt:

#box.left { margin-left: 0; }
#box.right { margin-left: 1000px; }
#box { -webkit-transition: margin-left 1s ease-in-out; }

// Run this to animate to the left
document.getElementById('box').className = 'left';

// Run this to animate to the right
document.getElementById('box').className = 'right';


animation-fill-mode: forwards The animation-fill-mode CSS property specifies how a CSS animation should apply styles to its target before and after it is executing

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号