left and top properties are not animated

后端 未结 2 1454
温柔的废话
温柔的废话 2021-01-26 05:50

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

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-26 06:19

    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);
      }
    }

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

提交回复
热议问题