Skip to content Skip to sidebar Skip to footer

Chaining Jquery Animations

I have come code that chains animations together (i.e. move left, then move right). I have 20 of
  • tags laid out in a grid and I am animating the main
      to move t
  • Solution 1:

    Animations are queued by default, so there's no point in applying the second animation within the first animation's callback.

    By using filter() you can filter out all of the elements that are not on the same row and then only apply the y animation to them, then you can apply the x animation to the entire collection.

    $('.elements')
        .filter(function(){
            return $(this).offset().top !== y;
        })
        .animate({
            top: y
        }, howFast, easeItIn)
        .end().animate({
            left: x
        }, howFast, easeItIn);
    

    Solution 2:

    You maybe can solve that problem without an if statement. Just adapting your selector might help.

    $('.elements:first-child')
    

    for instance. Another could be

    $('table').find('tr').find('.elements:nth-child(2)')
    

    jQuery selectors

    Solution 3:

    The key here is that you can animate multiple properties at the same time. So, remove your callback function, and simply add another property to the params of the first animation:

    if ($(window).width()<1259) 
    { 
        if (screenFlipMode == "chained") 
        {
            function(x, y)
            { //Small Chained
                $('.elements').animate({
                    top: y,
                    left: x
                }, howFast, easeItIn)
            }(x,y); 
        }
    }
    

    If either property is already at that value, it won't need to change, and thus that property won't animate. The other one will still animate, though.

    Also, jQuery automatically treats numeric values as pixes when using animate(), so there's no need to append "px". That may actually be hurting your performance.

    Post a Comment for "Chaining Jquery Animations"