How can I use jQuery to move a div across the screen

前端 未结 5 1303
旧时难觅i
旧时难觅i 2020-12-30 23:50

I need to make multiple divs move from right to left across the screen and stop when it gets to the edge. I have been playing with jQuery lately, and it seem like what I wa

5条回答
  •  一整个雨季
    2020-12-31 00:12

    In jQuery 1.2 and newer you no longer have to position the element absolutely; you can use normal relative positioning and use += or -= to add to or subtract from properties, e.g.

    $("#startAnimation").click(function(){
        $(".toBeAnimated").animate({ 
            marginLeft: "+=250px",
        }, 1000 );
    });
    

    And to echo the guy who answered first's advice: Javascript is not performant. Don't overuse animations, or expect things than run nice and fast on your high performance PC on Chrome to look good on a bog-standard PC running IE. Test it, and make sure it degrades well!

提交回复
热议问题