CSS 过渡和 CSS 动画的用例

发布于 2024-11-28 11:46:29 字数 1998 浏览 0 评论 0原文

据我了解,我们无法使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅我要高飞 2024-12-05 11:46:29
  • 动画可以循环(并且可以有关键帧,是的)。
  • 转换可以更加灵活,您可以在不同的情况下轻松转换到不同的值。

虽然您可以通过动画模拟一些过渡(就像您在帖子中提到的那样),但过渡更强大:

  • 您只需告诉哪些属性必须设置动画以及在哪些条件下(使用不同的选择器)
  • 您可以以不同的方式触发过渡:
    1. 更改伪类 :hover、:active 等 CSS 属性(创建纯 CSS UI)
    2. 出于不同目的更改不同类中的属性。
    3. 更改内联样式的属性:与 JS 结合使用,它比动画更强大。
  • Animations can be looped (and there can be keyframes, yeeah).
  • Transitions can be more flexible and you can easily make transitions to different values and in different circumstances.

While you can emulate some transitions by animations (like you mentioned in your post), the transitions are just more powerful:

  • You just tell which properties you must animate and in which conditions (using the different selectors)
  • You can trigger the transition in different ways:
    1. Changing properties in CSS for pseudo-classed :hover, :active etc. (Creating pure CSS UI)
    2. Changing properties in different classes for different purposes.
    3. Changing properties in inline styles: in conjunction with JS it's just more powerful than animations.
傲鸠 2024-12-05 11:46:29

通过转换,您可以在要转换的已定义属性的任何值之间进行转换。例如,您希望在链接悬停且处于活动状态时转换链接的颜色:

a {
    color: #000;
    transition: color .4s ease;
}
a:hover {
    color: #888;
}
a:active {
    color: #faa;
}

您可以独立选择哪种颜色。
现在,如果您想使用动画样式,则必须显式设置动画状态的颜色值。而且您无法轻松地在三种状态之间设置动画:正常、悬停和活动。您需要更复杂的定义。我将尝试使用动画:

a {
    color: #000;
    animation-duration: 0.4s;
    animation-fill-mode: forwards;
    animation-name: toDefault;
}
a:hover {
    animation-duration: 0.4s;
    animation-fill-mode: forwards;
    animation-name: toHover;
}
a:active {
    animation-duration: 0.4s;
    animation-fill-mode: forwards;
    animation-name: toActive;
}
@keyframes toDefault {
    to {
        color: #000;
    }
}
@keyframes toHover {
    to {
        color: #888;
    }
}
@keyframes toActive {
    to {
        color: #faa;
    }
}

现在这不包括回到之前状态的动画。我不确定你是否能拿到它。

简而言之:通过过渡,您可以为一组未定义的属性和值设置动画,而关键帧动画则用于定义明确的动画和/或过渡。

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:

a {
    color: #000;
    transition: color .4s ease;
}
a:hover {
    color: #888;
}
a:active {
    color: #faa;
}

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:

a {
    color: #000;
    animation-duration: 0.4s;
    animation-fill-mode: forwards;
    animation-name: toDefault;
}
a:hover {
    animation-duration: 0.4s;
    animation-fill-mode: forwards;
    animation-name: toHover;
}
a:active {
    animation-duration: 0.4s;
    animation-fill-mode: forwards;
    animation-name: toActive;
}
@keyframes toDefault {
    to {
        color: #000;
    }
}
@keyframes toHover {
    to {
        color: #888;
    }
}
@keyframes toActive {
    to {
        color: #faa;
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文