Keyframe animation back to initial state

2019-09-08 01:38发布

I did a slow infinite CSS3 animation. It animates from top:0; to top:40px;. The error is when the animate ends, it jumps to the start point. I think it should jump with easeor ease-in but that's not happening. I tried a lot to fix this, but I failed.

I need the return transition to have an ease-in effect.

My Code:

span {
  margin-left: 200px;
}
/** Down Slow Animation **/

@-webkit-keyframes downSlow {
  from { top: 0px; }
  to { top: 40px; }
}
@-moz-keyframes downSlow {
  from { top: 0px; }
  to { top: 40px; }
}
@keyframes downSlow {
  from { top: 0px; }
  to { top: 40px; }
}
.down-slow {
  position: relative;
  -webkit-animation: downSlow 1.5s infinite;
  -moz-animation: downSlow 1.5s infinite;
  animation: downSlow 1.5s infinite;
  -webkit-animation-fill-mode: forwards;
  -moz-animation-fill-mode: forwards;
  animation-fill-mode: forwards;
}
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" />

<span class="down-slow"><i class="fa fa-angle-down fa-5x"></i></span>

1条回答
我想做一个坏孩纸
2楼-- · 2019-09-08 02:33

This is just an alternative solution

Just add alternate ant the end of animation.

span {
  margin-left: 200px;
}
/** Down Slow Animation **/

@-webkit-keyframes downSlow {
  from { top: 0px; }
  to { top: 40px; }
}
@-moz-keyframes downSlow {
  from { top: 0px; }
  to { top: 40px; }
}
@keyframes downSlow {
  from { top: 0px; }
  to { top: 40px; }
}
.down-slow {
  position: relative;
  -webkit-animation: downSlow 1.5s infinite alternate;
  -moz-animation: downSlow 1.5s infinite alternate;
  animation: downSlow 1.5s infinite alternate;
}
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" />

<span class="down-slow"><i class="fa fa-angle-down fa-5x"></i></span>

The same code can be reduce to

span {
  margin-left: 200px;
  top: 0px;
}

@-webkit-keyframes downSlow {
  to { top: 40px; }
}
@-moz-keyframes downSlow {
  to { top: 40px; }
}
@keyframes downSlow {
  to { top: 40px; }
}
.down-slow {
  position: relative;
  
  -webkit-animation: downSlow 1.5s infinite alternate;
  -moz-animation: downSlow 1.5s infinite alternate;
  animation: downSlow 1.5s infinite alternate;
}
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" />

<span class="down-slow"><i class="fa fa-angle-down fa-5x"></i></span>

A much better approach would be to use translate this way your element will not interfere with other DOM elements

span {
  margin-left: 200px;
}

@-webkit-keyframes downSlow {
  to { transform: translateY(40px) }
}
@-moz-keyframes downSlow {
  to { transform: translateY(40px) }
}
@keyframes downSlow {
  to { transform: translateY(40px) }
}
.down-slow {
  position: relative;

  -webkit-animation: downSlow 1.5s infinite alternate;
  -moz-animation: downSlow 1.5s infinite alternate;
  animation: downSlow 1.5s infinite alternate;
}
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" />

<span class="down-slow"><i class="fa fa-angle-down fa-5x"></i></span>

But again you can reduce this with prefixfree frind a cdn here

span {
  margin-left: 200px;
}

@keyframes downSlow {
  to { transform: translateY(40px) }
}
.down-slow {
  position: relative;
  animation: downSlow 1.5s infinite alternate;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" />

<span class="down-slow"><i class="fa fa-angle-down fa-5x"></i></span>

at the end set speed/2

span {
  margin-left: 200px;
}

@keyframes downSlow {
  to { transform: translateY(40px) }
}
.down-slow {
  position: relative;
  animation: downSlow .7s infinite alternate forwards;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" />

<span class="down-slow"><i class="fa fa-angle-down fa-5x"></i></span>

REF: animation-fill-mode

查看更多
登录 后发表回答