CSS Animation from Left to Right

后端 未结 5 790
生来不讨喜
生来不讨喜 2021-02-08 11:55

I am trying to make an animation in CSS. I read some examples of it online and I cannot figure out what I\'m doing wrong... I want my potato image to go from left t

5条回答
  •  没有蜡笔的小新
    2021-02-08 12:19

    The accepted answer has the flaw that the element is completely pushed out of the viewport during the animation. This might be desired for some use cases, but I wanted to share a cleaner solution that leverages the CSS transform: translate property.

    #pot {
      bottom: 15%;
      display: block;
      position: absolute;
      animation: linear infinite alternate;
      animation-name: run;
      animation-duration: 2s;
    }
    
    @-webkit-keyframes run {
        0% {
          left: 0;
          transform: translateX(0);
        }
        100% {
          left: 100%;
          transform: translateX(-100%);
        }
    }

    I wrote in a bit more detail about this technique here: https://medium.com/@taig/dynamically-position-and-center-an-html-element-with-css-based-on-percentage-5fea0799a589.

提交回复
热议问题