【问题标题】:CSS Image Fade Animation Only Runs First TimeCSS 图像淡入淡出动画仅在第一次运行
【发布时间】:2018-06-14 21:13:38
【问题描述】:

我正在使用 CSS 制作背景图像褪色动画,它在第一次运行循环时按预期工作,但第二次在整个持续时间内卡在最后一张图像上,短暂跳转到第三张图像,然后回到最后一个。

如何更新它,使其在每个无限循环期间平稳地运行动画?

#rotate {
  width: 100%;
  height: 100%;
  position: absolute;
  bottom: 0;
  right: 0;
  margin: 0;
  padding: 0;
}

#rotate li {
  animation: func 16s linear 0s infinite;
  list-style-type: none;
  width: 100%;
  height: 100%;
  position: absolute;
  bottom: 0;
  right: 0;
  background: no-repeat 50% 30% / cover;
  opacity: 0;
}

#rotate li:nth-child(1) {
  animation-delay: 0s;
  -webkit-animation-delay: 0s;
  -moz-animation-delay: 0s;
  -o-animation-delay: 0s;
}

#rotate li:nth-child(2) {
  animation-delay: 4s;
  -webkit-animation-delay: 4s;
  -moz-animation-delay: 4s;
  -o-animation-delay: 4s;
}

#rotate li:nth-child(3) {
  animation-delay: 8s;
  -webkit-animation-delay: 8s;
  -moz-animation-delay: 8s;
  -o-animation-delay: 8s;
}

#rotate li:nth-child(4) {
  animation-delay: 12s;
  -webkit-animation-delay: 12s;
  -moz-animation-delay: 12s;
  -o-animation-delay: 12s;
}

@keyframes func {
  0%,
  100% {
    opacity: 0;
    animation-timing-function: ease-in;
  }
  5%,
  90% {
    opacity: 1;
  }
}
<ul id="rotate">
  <li style="background-image: url('https://dummyimage.com/600x400/000/fff&text=one')"></li>
  <li style="background-image: url('https://dummyimage.com/600x400/000/fff&text=two')"></li>
  <li style="background-image: url('https://dummyimage.com/600x400/000/fff&text=three');"></li>
  <li style="background-image:url('https://dummyimage.com/600x400/000/fff&text=four')"></li>
</ul>

以及指向 Codepen 示例的链接:https://codepen.io/erinpearson/pen/YvxVRM?editors=1100

【问题讨论】:

    标签: html css css-animations


    【解决方案1】:

    更改@keyframes 中的不透明度。

    #rotate {
      width: 100%;
      height: 100%;
      position: absolute;
      bottom: 0;
      right: 0;
      margin: 0;
      padding: 0;
    }
    
    #rotate li {
      animation: func 16s linear 0s infinite;
      list-style-type: none;
      width: 100%;
      height: 100%;
      position: absolute;
      bottom: 0;
      right: 0;
      background: no-repeat 50% 30% / cover;
      opacity: 0;
    }
    
    #rotate li:nth-child(1) {
      animation-delay: 0s;
      -webkit-animation-delay: 0s;
      -moz-animation-delay: 0s;
      -o-animation-delay: 0s;
    }
    
    #rotate li:nth-child(2) {
      animation-delay: 4s;
      -webkit-animation-delay: 4s;
      -moz-animation-delay: 4s;
      -o-animation-delay: 4s;
    }
    
    #rotate li:nth-child(3) {
      animation-delay: 8s;
      -webkit-animation-delay: 8s;
      -moz-animation-delay: 8s;
      -o-animation-delay: 8s;
    }
    
    #rotate li:nth-child(4) {
      animation-delay: 12s;
      -webkit-animation-delay: 12s;
      -moz-animation-delay: 12s;
      -o-animation-delay: 12s;
    }
    
    @keyframes func {
      0% {
        opacity: 0;
      }
      6% {
        opacity: 1;
      }
      24% {
        opacity: 1;
      }
      30% {
        opacity: 0;
      }
      100% {
        opacity: 0;
      }
    }
    <ul id="rotate">
      <li style="background-image: url('https://dummyimage.com/600x400/000/fff&text=one')"></li>
      <li style="background-image: url('https://dummyimage.com/600x400/000/fff&text=two')"></li>
      <li style="
      background-image: url('https://dummyimage.com/600x400/000/fff&text=three');"></li>
      <li style="background-image: url('https://dummyimage.com/600x400/000/fff&text=four')"></li>
    </ul>

    【讨论】:

      猜你喜欢
      • 2016-02-07
      • 2019-02-11
      • 1970-01-01
      • 2023-04-10
      • 2023-03-05
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      相关资源
      最近更新 更多