JavaScript animation

前端 未结 7 1988
忘了有多久
忘了有多久 2020-11-29 09:47

I am trying to animate a div moving 200px horizontally in JavaScript.

The code below makes it jump the pixels, but is there a way to make i

相关标签:
7条回答
  • 2020-11-29 10:15

    You would have to use a javascript timeout function, and change the css value a little at a time. The easiest way would be to increment by a set amount each time until a threshold is reached, which would give you a linear animation, which would look clunky and amateurish compared to jQuery's default swing animation which follows a bezier curve approximately like an s-curve.

    Untested code should do the linear animation

    var lefty = 0;
    var animate = function(){
        lefty += 20;
        var div = document.getElementById('challengeOneImageJavascript');
        div.style.left = lefty +"px";
        if(lefty < 200)
          setTimeout(animate(),100);
    }
    
    animate()
    

    n.b. there are lots of improvements to make to that block of code, but it should get you going...

    0 讨论(0)
提交回复
热议问题