CSS平滑反弹动画

Abh*_*bhi 19 css css3 css-animations

我需要使用纯CSS实现无限弹跳效果,所以我引用了这个网站并最终做到了这一点.

.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;
}
Run Code Online (Sandbox Code Playgroud)
<div id="animated-example" class="animated bounce"></div>
<hr>
Run Code Online (Sandbox Code Playgroud)

现在,我唯一的问题是弹跳元素在它再次开始弹跳之前需要更长时间的休息.我怎样才能让它像我们使用的弹跳效果一样平稳地反弹jQuery.animate

Har*_*rry 40

中间的长时间休息是由于您的关键帧设置.您当前的关键帧规则意味着实际反弹仅发生在动画持续时间的40%到60%之间(即动画的1到1.5秒之间).删除这些规则,甚至可以减少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;
}
Run Code Online (Sandbox Code Playgroud)
<div id="animated-example" class="animated bounce"></div>
<hr>
Run Code Online (Sandbox Code Playgroud)


以下是keyframe浏览器解释原始设置的方法:

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

  • 哦,你输入的速度比我快一点. (3认同)

Per*_*ijn 18

这是不使用关键帧中的百分比的代码.因为你使用百分比,动画不会长时间没用.

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

这个例子是如何工作的:

  1. 我们设置了一个animation.这是动画属性的简写.
  2. 我们立即启动动画,因为我们使用from ,并to在关键帧.从is = 0%到is = 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);
  }
}
Run Code Online (Sandbox Code Playgroud)
<div class="ball"></div>
Run Code Online (Sandbox Code Playgroud)


Ger*_*ink 5

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

.ball {
  animation: bounce 1s infinite alternate;
  -webkit-animation: bounce 1s infinite alternate;
}

@keyframes bounce {
  from {
    margin-top: 0;
  }
  to {
    margin-top: -15px;
  }
}
Run Code Online (Sandbox Code Playgroud)