0

Is it possible to animate the same object with different speeds and starting at the same time?

function loop_horiz(direction){
    var d = (direction == 1) ? '+' : '-';
    $('.op').animate({ left: d + '200' },
     4000, function() { loop_horiz(direction * (-1)) });
}

function loop_vert(direction){
    var d = (direction == 1) ? '+' : '-';
    $('.op').animate({ bottom: d + '200' },
    2000, function() { loop_vert(direction * (-1)) });
}

loop_horiz(1);
loop_vert(1)
2
  • Did you try? What errors (if any) are you getting? Commented Oct 29, 2013 at 9:07
  • @GlauberRocha, it doesn't need 'px', @David, it works, but the animations are queueing. If I set queue: false, the animations won't loop Commented Oct 29, 2013 at 9:08

1 Answer 1

1

Normally when animating an object with jQuery, the animation is added to the queue for that object. When previous animations have ended, the animation starts running. Your code above does exactly that, animates horizontally, then vertically.

This behavior can be overwritten by the use of the queue option of the animation (set it to false).

function loop_horiz(direction) {
    var d = (direction == 1) ? '+' : '-';
    $('.op').animate({
        left: d + '200'
    }, {
        queue: false,
        duration: 4000,
        done: function () {
            loop_horiz(direction * (-1))
        }
    });
}

function loop_vert(direction) {
    var d = (direction == 1) ? '+' : '-';
    $('.op').animate({      
        bottom: d + '200'
    }, {
        queue: false,
        duration: 2000,
        done: function () {
            loop_vert(direction * (-1))
        }
    });

}

loop_horiz(1);
loop_vert(1)

Another (uglier) possibility is to merge the animations, but it would require you to split the horizontal animation in two for half the distance running over 2s.

http://api.jquery.com/animate/

Even better, with queue as string you can add the animations to different queues. Same code as above, but using different queues for vertical and horizontal animation:

function loop_horiz(direction) {
    var d = (direction == 1) ? '+' : '-';
    $('.op').animate({
        left: d + '200'
    }, {
        queue: 'horizontal',
        duration: 4000,
        done: function () {
            loop_horiz(direction * (-1))
        }
    }).dequeue('horizontal');
}

function loop_vert(direction) {
    var d = (direction == 1) ? '+' : '-';
    $('.op').animate({
        bottom: d + '200'
    }, {
        queue: 'vertical',
        duration: 2000,
        done: function () {
            loop_vert(direction * (-1))
        }
    }).dequeue('vertical');

}

loop_horiz(1);
loop_vert(1)

DEMO: http://jsfiddle.net/Uewzc/1/

Sign up to request clarification or add additional context in comments.

3 Comments

As I responded in an earlier comment, setting the queue to false, stops the loop. Il will try to split the horizontal animation though
I think I may have put the queue in the wrong place. I've put the queue and duration in the second parameter and the callback function in the third. So the correct form is that the queue, duration and callback all must be in the second parameter. Thanks!
There are two forms of calling the animate method, one with two objects as parameters (where you can specify the queue) and another one with 1-4 parameters. You probably had them confused. The documentation is pretty clear though, check it when you have doubts!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.