简体   繁体   中英

Achieving a bounce effect on text using jquery animate()?

I am trying to use Jquery to create a bounce effect, this is what i have so far:

Html:

<div id ="animation">bounce</div>

Jquery:

$("#animation").animate({ marginTop: "80px" }, 1500 )
               .animate({ marginBottom: "40px" }, 800 );

Its goes downwards but not upwards, i tried searching the documentation, but it doesn't

working example here: http://jsfiddle.net/zLw8F/

To go upwards again, you'd need to reduce the margin-top instead of animating margin-bottom :

$("#animation").animate({ marginTop: "80px" }, 1500 )
               .animate({ marginTop: "40px" }, 800 );

Demo at jsfiddle.net

Yet, to animate the element decoupled from the rest of the page, I recommend relative positioning instead of playing with margins.

Why not just use the jQuery UI effects ? Ex:

$("#animation").effect("bounce", { times:3 }, 300);​

jsFiddle example

Try that:

$("#animation").animate({ marginTop: "80px" }, 1500, function() {
  $(this).animate({ marginTop: "40px" }, 800 );
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM