CSS 过渡和 CSS 动画的用例
据我了解,我们无法使用CSS过渡来实现这样的事情,但我们不能使用CSS动画来实现,但反之亦然。
也就是说,任何过渡都有一个 css 动画等效项。 例如,这
.ablock:hover {
position: relative;
-moz-transition-property: background-color, color;
-moz-transition-duration: 1s;
-webkit-transition-property: background-color, color;
-webkit-transition-duration: 1s;
color: red;
background-color:pink;
}
相当于以下内容:
.ablock:hover {
-moz-animation-duration:1s;
-moz-animation-name:transition;
-webkit-animation-duration:1s;
-webkit-animation-name:transition;
}
@-moz-keyframes transition {
to {
color: red;
background-color: pink;
}
}
@-webkit-keyframes transition {
to {
color: red;
background-color: pink;
}
}
我的问题是 - 如果我们谈论支持 css 过渡和动画的浏览器,选择一种或另一种方法的用例是什么? 至于过渡,我只能举出一个 - 它们有更简洁的语法,我们不必为 @-moz-keyframes、@-webkit-keyframes 等复制粘贴大量代码。
至于来自 javascript 的控制,灵活性和复杂性动画是更合适的工具(至少乍一看是这样)。那么,什么是用例?
UPD: 好的,让我尝试列出问题中发现的有趣信息。
- 这是由罗曼·科马罗夫 (Roman Komarov) 贡献的。假设我们有一个 div 和子 div。当父 div 悬停时,我们正在转换子元素。一旦我们拿走鼠标,过渡就会被取消。此次取消的持续时间正是我们已经花费在转换上的时间。动画“立即”取消。不过,我不知道这两种行为有多标准。
As far as I understand, there is no such thing we can implement using css transitions, but we can not to implement using css animations, but not vice versa.
That is, any transition has a css animation equivalent.
For example, this one
.ablock:hover {
position: relative;
-moz-transition-property: background-color, color;
-moz-transition-duration: 1s;
-webkit-transition-property: background-color, color;
-webkit-transition-duration: 1s;
color: red;
background-color:pink;
}
is an equivalent of following:
.ablock:hover {
-moz-animation-duration:1s;
-moz-animation-name:transition;
-webkit-animation-duration:1s;
-webkit-animation-name:transition;
}
@-moz-keyframes transition {
to {
color: red;
background-color: pink;
}
}
@-webkit-keyframes transition {
to {
color: red;
background-color: pink;
}
}
My question is - if we a talking about browser supporting both css transitions and animations, what are use cases for choosing one or another approach?
As for transitions, I can name only one - they have more succinct syntax, we don't have to copy paste huge chucks of code for @-moz-keyframes, @-webkit-keyframes and so on.
As for control from javascript, flexibility and complexity animations are much more appropriate tool (at least, at first glance). So, what are use cases?
UPD:
OK, let me try to list interesting info found in questions.
- This one is contributed by Roman Komarov. Say, we have a div and child div. While parent div is hovered, we are transitioning the child element. Once we are taking away the mouse, transition is cancelled. Duration of this cancellation is exactly the time we've already spend for transitioning. Animation is cancelled "immediately". I don't know, nevertheless, how standard are those two behaviours.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然您可以通过动画模拟一些过渡(就像您在帖子中提到的那样),但过渡更强大:
While you can emulate some transitions by animations (like you mentioned in your post), the transitions are just more powerful:
通过转换,您可以在要转换的已定义属性的任何值之间进行转换。例如,您希望在链接悬停且处于活动状态时转换链接的颜色:
您可以独立选择哪种颜色。
现在,如果您想使用动画样式,则必须显式设置动画状态的颜色值。而且您无法轻松地在三种状态之间设置动画:正常、悬停和活动。您需要更复杂的定义。我将尝试使用动画:
现在这不包括回到之前状态的动画。我不确定你是否能拿到它。
简而言之:通过过渡,您可以为一组未定义的属性和值设置动画,而关键帧动画则用于定义明确的动画和/或过渡。
With transitions you are able to transition between any value of the defined property, which you want to be transitioned. As an example, you want to transition the color of a link, when it's hovered and active:
You are independent, which color you choose.
Now if you want to use the animation style, you have to explicitly set the color value for the animation states. And you are not able to easily animate between the three states: normal, hover and active. You need more complex definitions. I'll try this one with animations:
Now this does not include the animation back to the state before. I'm not sure if you can even fetch that.
So in short: with transitions you are able to animate an undefined set of properties and values, whilst keyframe animations are used for well defined animations and/or transitions.