left and top properties are not animated

强颜欢笑 提交于 2019-12-04 06:32:32

问题


In the animation below the transform is animated correctly, but the left and top properties are not. Why is this?

.element-animation {
  background-color: yellow;
  width: 30px;
  height: 30px;
  animation: animationFrames ease 2s;
  animation-iteration-count: infinite;
}
@keyframes animationFrames {
  0% {
    left: 0px;
    top: 0px;
    opacity: 1;
    transform: rotate(0deg) scaleX(1) scaleY(1) skewX(0deg) skewY(0deg);
  }
  25% {
    left: 0px;
    top: -90px;
  }
  75% {
    left: 200px;
    top: -90px;
  }
  100% {
    left: 200px;
    top: 0px;
    opacity: 1;
    transform: rotate(0deg) scaleX(2) scaleY(2) skewX(45deg) skewY(45deg);
  }
}
<div class="element-animation"></div>

回答1:


Your animation relies on positioning, therefore you have to add a position property:

.element-animation{
    position:relative;
}

.element-animation {
  background-color: yellow;
  width: 30px;
  height: 30px;
  animation: animationFrames ease 2s;
  animation-iteration-count: 1;
  position: relative;
}
@keyframes animationFrames {
  0% {
    left: 0px;
    top: 0px;
    opacity: 1;
    transform: rotate(0deg) scaleX(1) scaleY(1) skewX(0deg) skewY(0deg);
  }
  25% {
    left: 0px;
    top: -90px;
  }
  75% {
    left: 200px;
    top: -90px;
  }
  100% {
    left: 200px;
    top: 0px;
    opacity: 1;
    transform: rotate(0deg) scaleX(1) scaleY(1) skewX(0deg) skewY(0deg);
  }
}
<div class="element-animation"></div>

For older browsers you may need to add the -webkit- prefix for the animation property. Check browser compatibility over on caniuse.com




回答2:


You should copy all the code for every Browser. not just standard.

so it should contain the following stuff

-webkit-animation: animationFrames linear 0.7s;
-webkit-animation-iteration-count: 1;
-webkit-transform-origin: ;

see in http://jsfiddle.net/KxM68/8/



来源:https://stackoverflow.com/questions/24570269/left-and-top-properties-are-not-animated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!