【问题标题】:How to make the animation smooth?如何使动画流畅?
【发布时间】:2021-09-21 02:25:59
【问题描述】:

我正在尝试创建一个预加载器,它可以改变它的高度、宽度、边框半径和背景颜色。

动画有效,但在更改之间有一个暂停。如何让动画更流畅?

小提琴:https://jsfiddle.net/Altair827/ww077qby/4/

#preloader {
  margin-left: 300px;
  margin-top: 200px;
  height: 20px;
  width: 20px;
  border-radius: 10px;
  background-color: violet;
  -webkit-animation-name: colorIt;
          animation-name: colorIt;
  -webkit-animation-duration: 3s;
          animation-duration: 3s;
  -webkit-animation-fill-mode: both;
          animation-fill-mode: both;
}

@-webkit-keyframes colorIt {
  from {
    background-color: violet;
  }
  to 20%,40%,60%,80%,90%,99% {
    
  }
  20% {
    background-color: indigo;
    height: 40px;
    width: 40px;
    border-radius: 20px;
  }
  40% {
    background-color: blue;
    height: 50px;
    width: 50px;
    border-radius: 25px;
  }
  60% {
    background-color: green;
    height: 60px;
    width: 60px;
    border-radius: 30px;
  }
  80% {
    background-color: yellow;
    height: 70px;
    width: 70px;
    border-radius: 35px;
  }
  90% {
    background-color: orange;
    height: 80px;
    width: 80px;
    border-radius: 40px;
  }
  99% {
    background-color: red;
    height: 20px;
    width: 20px;
    border-radius: 10px;
  }
}

@keyframes colorIt {
  from {
    background-color: violet;
  }
  to 20%,40%,60%,80%,90%,99% {
    
  }
  20% {
    background-color: indigo;
    height: 40px;
    width: 40px;
    border-radius: 20px;
  }
  40% {
    background-color: blue;
    height: 50px;
    width: 50px;
    border-radius: 25px;
  }
  60% {
    background-color: green;
    height: 60px;
    width: 60px;
    border-radius: 30px;
  }
  80% {
    background-color: yellow;
    height: 70px;
    width: 70px;
    border-radius: 35px;
  }
  90% {
    background-color: orange;
    height: 80px;
    width: 80px;
    border-radius: 40px;
  }
  99% {
    background-color: red;
    height: 20px;
    width: 20px;
    border-radius: 10px;
  }
}
<div id="preloader"></div>

【问题讨论】:

    标签: css css-transitions css-animations keyframe


    【解决方案1】:

    你应该改变你的计时功能:

    animation-timing-function: linear;
    

    你也可以使用短地:

    /* @keyframes duration | timing-function | delay | name */
    animation: 3s linear .1s colorIt;
    

    【讨论】:

      【解决方案2】:

      您想将动画曲线设置为线性:

      -webkit-animation-timing-function: linear; /* Chrome, Safari, Opera */
      animation-timing-function: linear;
      

      这将确保您的动画顺利运行:jsfiddle

      【讨论】:

        【解决方案3】:

        CSS 动画的默认easing 是ease。将其设置为linear,应该没有停顿:

        animation-timing-function: linear;
        

        #preloader {
          margin-left: 300px;
          margin-top: 200px;
          height: 20px;
          width: 20px;
          border-radius: 10px;
          background-color: violet;
          -webkit-animation-name: colorIt;
          animation-name: colorIt;
          -webkit-animation-duration: 3s;
          animation-duration: 3s;
          -webkit-animation-fill-mode: both;
          animation-fill-mode: both;
          -webkit-animation-timing-function: linear;
          animation-timing-function: linear;
        }
        @-webkit-keyframes colorIt {
          from {
            background-color: violet;
          }
          to 20%,
          40%,
          60%,
          80%,
          90%,
          99% {} 20% {
            background-color: indigo;
            height: 40px;
            width: 40px;
            border-radius: 20px;
          }
          40% {
            background-color: blue;
            height: 50px;
            width: 50px;
            border-radius: 25px;
          }
          60% {
            background-color: green;
            height: 60px;
            width: 60px;
            border-radius: 30px;
          }
          80% {
            background-color: yellow;
            height: 70px;
            width: 70px;
            border-radius: 35px;
          }
          90% {
            background-color: orange;
            height: 80px;
            width: 80px;
            border-radius: 40px;
          }
          99% {
            background-color: red;
            height: 20px;
            width: 20px;
            border-radius: 10px;
          }
        }
        @keyframes colorIt {
          from {
            background-color: violet;
          }
          to 20%,
          40%,
          60%,
          80%,
          90%,
          99% {} 20% {
            background-color: indigo;
            height: 40px;
            width: 40px;
            border-radius: 20px;
          }
          40% {
            background-color: blue;
            height: 50px;
            width: 50px;
            border-radius: 25px;
          }
          60% {
            background-color: green;
            height: 60px;
            width: 60px;
            border-radius: 30px;
          }
          80% {
            background-color: yellow;
            height: 70px;
            width: 70px;
            border-radius: 35px;
          }
          90% {
            background-color: orange;
            height: 80px;
            width: 80px;
            border-radius: 40px;
          }
          99% {
            background-color: red;
            height: 20px;
            width: 20px;
            border-radius: 10px;
          }
        }
        <div id="preloader"></div>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-05-12
          • 1970-01-01
          • 2020-09-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-11-26
          • 1970-01-01
          相关资源
          最近更新 更多