treehouse : what would you like to learn today?
Web Design Web Development iOS Development

allow one animation at a time with jQuery?

  • How do you allow one animation to be going at a time with jQuery? Otherwise you can just go crazy with your mouse and things will be animating forever. Here's my code.

    $(document).ready(function() {
    //slides animations
    $(".slide").width(161);
    $(".s1").width(380);
    $(".slide").hover(function() {
    $(this).animate({
    width: "380px"
    }, 200);
    },
    function () {
    $(this).animate({
    width: "161px"
    }, 200);
    });
    });
  • what you have to do is chain the animations:

    $(".slide").hover(function(){
    $(this).animate( { width:"380px"}, 200 )
    .animate( { width:"161px" }, 200 );
    });


    Have a look here and at the examples towards the bottom of the page :)

    http://api.jquery.com/animate/
  • Thanks!