【问题标题】:CSS animation delay not triggeringCSS动画延迟未触发
【发布时间】:2017-01-11 06:10:50
【问题描述】:

只是想知道为什么这个简单的动画延迟似乎不起作用。我只想在元素淡入之前延迟 7 秒。很简单,我确定,但现在已经看了很长时间了。

.box1 {
  width: 100px;
  margin: 0 auto;
  position: relative;
  border: 1px solid blue;
}
.box2 {
   background: red;
  color: black;
  text-align: center;
  animation-delay: 7s;
  -webkit-animation-delay: 7s;
  animation: fadein 2s linear;
  -webkit-animation: fadein 2s linear;
 
}
@keyframes fadein {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
@-webkit-keyframes fadein {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
<div class= "box1">

  <div class="box2">

    <p>some text</p>

  </div>

</div>

【问题讨论】:

    标签: css animation css-animations


    【解决方案1】:

    把动画延迟放在动画后面,因为延迟需要附加到有问题的动画上(顺序很重要):

    .box1 {
      width: 100px;
      margin: 0 auto;
      position: relative;
      border: 1px solid blue;
    }
    .box2 {
       background: red;
      color: black;
      text-align: center;
      -webkit-animation: fadein 2s linear;
       animation: fadein 2s linear; 
      -webkit-animation-delay: 7s;
       animation-delay: 7s;
     
    }
    @keyframes fadein {
      from {
        opacity: 0;
      }
      to {
        opacity: 1;
      }
    }
    @-webkit-keyframes fadein {
      from {
        opacity: 0;
      }
      to {
        opacity: 1;
      }
    }
    <div class="box1">
    
      <div class="box2">
    
        <p>some text</p>
    
      </div>
    
    </div>

    或者使用简写方式,删除animation-delay: 7s;-webkit-animation-delay: 7s; 并将延迟时间添加到animation 属性,如下所示:

    -webkit-animation:fadein 2s linear 7s;
     animation:fadein 2s linear 7s;
    

    .box1 {
      width: 100px;
      margin: 0 auto;
      position: relative;
      border: 1px solid blue;
    }
    .box2 {
      background: red;
      color: black;
      text-align: center;
      -webkit-animation: fadein 2s linear 7s;
      animation: fadein 2s linear 7s;
    }
    @keyframes fadein {
      from {
        opacity: 0;
      }
      to {
        opacity: 1;
      }
    }
    @-webkit-keyframes fadein {
      from {
        opacity: 0;
      }
      to {
        opacity: 1;
      }
    }
    <div class="box1">
    
      <div class="box2">
    
        <p>some text</p>
    
      </div>
    
    </div>

    【讨论】:

    • 非常感谢这位朋友
    【解决方案2】:

    尝试使用长格式 animation 属性:

    • animation-delay
    • animation-name
    • animation-duration
    • animation-timing-function

    .box1 {
      width: 100px;
      margin: 0 auto;
      position: relative;
      border: 1px solid blue;
    }
    .box2 {
       background: red;
      color: black;
      text-align: center;
      opacity: 0;
      -webkit-animation-delay: 7s;
      -webkit-animation-name: fadein;
      -webkit-animation-duration: 2s; 
      -webkit-animation-timing-function: linear;
      animation-delay: 7s;
      animation-name: fadein; 
      animation-duration: 2s;
      animation-timing-function: linear;
     
    }
    @-webkit-keyframes fadein {
      from {
        opacity: 0;
      }
      to {
        opacity: 1;
      }
    }
    @keyframes fadein {
      from {
        opacity: 0;
      }
      to {
        opacity: 1;
      }
    }
    <div class= "box1">
    
      <div class="box2">
    
        <p>some text</p>
    
      </div>
    
    </div>

    【讨论】:

      【解决方案3】:

      下面的代码有效,我将盒子的初始不透明度设置为 0,因此它“淡入”,而且动画延迟属性似乎只有在动画本身之后声明它才有效。我还添加了动画填充模式:转发;保持动画后显示的框。

      .box1 {
        width: 100px;
        margin: 0 auto;
        position: relative;
        border: 1px solid blue;
      }
      .box2 {
         background: red;
        color: black;
        text-align: center;
        animation: fadein 2s linear;
        -webkit-animation: fadein 2s linear;
        animation-delay: 7s;
        -webkit-animation-delay: 7s;
        animation-fill-mode: forwards;
        -webkit-animation-fill-mode: forwards;
        opacity: 0;
       
      }
      @keyframes fadein {
        from {
          opacity: 0;
        }
        to {
          opacity: 1;
        }
      }
      @-webkit-keyframes fadein {
        from {
          opacity: 0;
        }
        to {
          opacity: 1;
        }
      }
      <div class= "box1">
      
        <div class="box2">
      
          <p>some text</p>
      
        </div>
      
      </div>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-21
        • 1970-01-01
        • 2012-12-02
        • 1970-01-01
        • 1970-01-01
        • 2021-08-05
        • 2022-01-02
        相关资源
        最近更新 更多