【问题标题】:Why is my bounce animation so jumpy instead of smooth?为什么我的弹跳动画如此跳跃而不是流畅?
【发布时间】:2022-03-09 01:31:02
【问题描述】:

我需要使用纯 CSS 实现无限弹跳效果,所以我参考了this 站点并最终做了this

.animated {
  -webkit-animation-duration: 2.5s;
  animation-duration: 2.5s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
  -webkit-animation-timing-function: linear;
  animation-timing-function: linear;
  animation-iteration-count: infinite;
  -webkit-animation-iteration-count: infinite;
} 

@-webkit-keyframes bounce {
  0%, 20%, 40%, 60%, 80%, 100% {-webkit-transform: translateY(0);}
  50% {-webkit-transform: translateY(-5px);}
} 

@keyframes bounce { 
  0%, 20%, 40%, 60%, 80%, 100% {transform: translateY(0);}
  50% {transform: translateY(-5px);}
} 

.bounce { 
  -webkit-animation-name: bounce;
  animation-name: bounce;
}

#animated-example {
    width: 20px;
    height: 20px;
    background-color: red;
    position: relative;
    top: 100px;
    left: 100px;
    border-radius: 50%;
}

hr {
    position: relative;
    top: 92px;
    left: -300px;
    width: 200px;
}
<div id="animated-example" class="animated bounce"></div>
<hr>

现在,我唯一的问题是弹跳元素在再次开始弹跳之前需要更长的休息时间。如何让它像我们使用 jQuery.animate 得到的弹跳效果一样平滑弹跳?

【问题讨论】:

    标签: css css-animations


    【解决方案1】:

    中间的长休息是由于您的关键帧设置。您当前的关键帧规则意味着实际反弹仅发生在动画持续时间的 40% - 60% 之间(即动画的 1s - 1.5s 标记之间)。删除这些规则,甚至可能减少 animation-duration 以满足您的需求。

    .animated {
      -webkit-animation-duration: .5s;
      animation-duration: .5s;
      -webkit-animation-fill-mode: both;
      animation-fill-mode: both;
      -webkit-animation-timing-function: linear;
      animation-timing-function: linear;
      animation-iteration-count: infinite;
      -webkit-animation-iteration-count: infinite;
    }
    @-webkit-keyframes bounce {
      0%, 100% {
        -webkit-transform: translateY(0);
      }
      50% {
        -webkit-transform: translateY(-5px);
      }
    }
    @keyframes bounce {
      0%, 100% {
        transform: translateY(0);
      }
      50% {
        transform: translateY(-5px);
      }
    }
    .bounce {
      -webkit-animation-name: bounce;
      animation-name: bounce;
    }
    #animated-example {
      width: 20px;
      height: 20px;
      background-color: red;
      position: relative;
      top: 100px;
      left: 100px;
      border-radius: 50%;
    }
    hr {
      position: relative;
      top: 92px;
      left: -300px;
      width: 200px;
    }
    <div id="animated-example" class="animated bounce"></div>
    <hr>

    以下是浏览器将如何解释您的原始 keyframe 设置:

    • 在 0% 处(即在 0 秒或动画开始处) - translate 在 Y 轴上 0 像素。
    • 在 20% 时(即在 0.5 秒的动画处) - translate 在 Y 轴上 0 像素。
    • 在 40%(即动画的 1 秒处)-translate 在 Y 轴上 0 像素。
    • 在 50% 时(即在 1.25 秒的动画处) - translate 在 Y 轴上 5 像素。这会导致逐渐向上运动。
    • 60%(即动画 1.5 秒时)-translate 在 Y 轴上 0px。这会导致逐渐向下移动。
    • 在 80% 处(即动画的 2 秒处)-translate 在 Y 轴上 0 像素。
    • 100% 时(即 2.5 秒或动画结束时)-translate 在 Y 轴上 0 像素。

    【讨论】:

      【解决方案2】:

      这里的代码没有使用关键帧中的百分比。 因为你使用了百分比,所以动画很长一段时间都没有做任何事情。

      • 0% 翻译 0px
      • 20% 翻译 0px

      这个例子是如何工作的:

      1. 我们设置了animation。这是动画属性的简写。
      2. 我们立即开始动画,因为我们在关键帧中使用了fromto。从是 = 0% 到是 = 100%
      3. 我们现在可以通过设置动画时间来控制它反弹的速度:animation: bounce 1s infinite alternate; 1s 是动画的持续时间。

      .ball {
        margin-top: 50px;
        border-radius: 50%;
        width: 50px;
        height: 50px;
        background-color: cornflowerblue;
        border: 2px solid #999;
        animation: bounce 1s infinite alternate;
        -webkit-animation: bounce 1s infinite alternate;
      }
      @keyframes bounce {
        from {
          transform: translateY(0px);
        }
        to {
          transform: translateY(-15px);
        }
      }
      @-webkit-keyframes bounce {
        from {
          transform: translateY(0px);
        }
        to {
          transform: translateY(-15px);
        }
      }
      &lt;div class="ball"&gt;&lt;/div&gt;

      【讨论】:

        【解决方案3】:

        如果您已经在使用 transform 属性来定位元素(就像我现在一样),您还可以为上边距设置动画:

        .ball {
          animation: bounce 1s infinite alternate;
          -webkit-animation: bounce 1s infinite alternate;
        }
        
        @keyframes bounce {
          from {
            margin-top: 0;
          }
          to {
            margin-top: -15px;
          }
        }
        

        【讨论】:

          猜你喜欢
          • 2014-01-16
          • 2021-12-20
          • 2023-01-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多