【问题标题】:Css Transitions one after another一个接一个的Css过渡
【发布时间】:2016-07-16 03:49:34
【问题描述】:

我试图让CSS transitions一个接一个地播放,这意味着首先它必须转换宽度和高度。
我尝试了几个选项,但一切都是徒劳的。我在javascript 中实现了这一点,但一些CSSprofessionals 可以帮助探索并打破它。提前致谢。

<style> 
div {
    width: 100px;
    height: 100px;
    background: red;
    -webkit-transition: width 2s;
    transition: all 2s;
}

div:hover {
    width: 300px;
    height:500px;
}
</style>

【问题讨论】:

    标签: css css-transitions transitions


    【解决方案1】:

    为了便于参考,将各个转换属性分解为逗号分隔的值……然后只需使用转换延迟对它们进行排序。

    div {
        width: 100px;
        height: 100px;
        background: red;
        transition-property: width, height;
        transition-duration:2s, 4s;
        transition-delay:0s, 2s;
        transition-timing-function:linear;
    }
    
    div:hover {
        width: 300px;
        height: 300px;
     }
    &lt;div&gt;&lt;/div&gt;

    【讨论】:

      【解决方案2】:

      您可以为此使用Keyframes。

      /* Standard */
      @keyframes all {
          0%   {width: 100px; height: 100px;}
          50%  {width: 300px; height: 100px;}
          100% {width: 300px; height: 500px;}
      }
      
      /* Chrome, Safari, Opera */
      @-webkit-keyframes all {
          0%   {width: 100px; height: 100px;}
          50%  {width: 300px; height: 100px;}
          100% {width: 300px; height: 500px;}
      }
      

      这样称呼它:

      div:hover {
          -webkit-animation: all 2s; /* Chrome, Safari, Opera */ 
          animation: all 2s;
          animation-fill-mode: forwards;
      }
      

      见这个例子JsFiddle

      编辑:在我之前发布的其他答案使用了比这更好的方法,因为它们可用于在用户滚动 div 时反转动画。

      【讨论】:

      • 感谢您的回答
      【解决方案3】:

      只需使用transition 速记中的transition-delay 属性即可在一个CSS 规则中按顺序执行多个转换。

      -webkit-transition: width 2s, height 2s ease 2s;
      transition: width 2s, height 2s ease 2s;
      

      【讨论】:

        猜你喜欢
        • 2012-06-24
        • 2015-12-14
        • 1970-01-01
        • 2016-01-24
        • 1970-01-01
        • 2021-01-24
        • 1970-01-01
        • 2015-10-26
        • 2014-03-20
        相关资源
        最近更新 更多