219

Is it possible to run two animations on two different elements simultaneously? I need the opposite of this question Jquery queueing animations.

I need to do something like this...

$('#first').animate({ width: 200 }, 200); $('#second').animate({ width: 600 }, 200); 

but to run those two at the same time. The only thing I could think of would be using setTimeout once for each animation, but I don't think it is the best solution.

5
  • Erm... have you tried it? If you use the exact code that you have there, to my understanding of how animate() works, they should run simultaneously. Commented Aug 9, 2009 at 12:36
  • Here we go -jsbin.com/axata . Add /edit to see the code Commented Aug 9, 2009 at 12:41
  • I have a real problem when animating different CSS properties. This question looks old, and I put it in the JSFiddle, seems both approaches work. Is this still relevant? jsfiddle.net/AVsFe Commented Oct 21, 2013 at 19:57
  • 3
    In current jquery, and I believe since jQuery 1.4, the above code does animate both simultaneously, since fx queues are attached to the element that you call .animate() on, not a global queue. Commented Dec 5, 2014 at 18:39
  • Above jsbin is dead. jsfiddle of same, that works: jsfiddle.net/b9chris/a8pwbhx1 Commented Dec 5, 2014 at 18:52

7 Answers 7

430

yes there is!

$(function () { $("#first").animate({ width: '200px' }, { duration: 200, queue: false }); $("#second").animate({ width: '600px' }, { duration: 200, queue: false }); }); 
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for this, didn't know about that queue variable!
May I ask what is the purpose of passing a function to jQuery?
@pilau It will be executed when the document is ready. This is a shorthand for $(document).ready(function(){}); and enables you to put your javascript code before your element definition.
yes, you can eater set queue to true/false, or give it a string (queue-name) this way, both animations use the same timer...
How would you add an onComplete to this?
|
20

That would run simultaneously yes. what if you wanted to run two animations on the same element simultaneously ?

$(function () { $('#first').animate({ width: '200px' }, 200); $('#first').animate({ marginTop: '50px' }, 200); }); 

This ends up queuing the animations. to get to run them simultaneously you would use only one line.

$(function () { $('#first').animate({ width: '200px', marginTop:'50px' }, 200); }); 

Is there any other way to run two different animation on the same element simultaneously ?

Comments

10

I believe I found the solution in the jQuery documentation:

Animates all paragraph to a left style of 50 and opacity of 1 (opaque, visible), completing the animation within 500 milliseconds. It also will do it outside the queue, meaning it will automatically start without waiting for its turn.

$( "p" ).animate({ left: "50px", opacity: 1 }, { duration: 500, queue: false }); 

simply add: queue: false.

1 Comment

How to integrate the complete function on this one ? :s
8

If you run the above as they are, they will appear to run simultaenously.

Here's some test code:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script> $(function () { $('#first').animate({ width: 200 }, 200); $('#second').animate({ width: 600 }, 200); }); </script> <div id="first" style="border:1px solid black; height:50px; width:50px"></div> <div id="second" style="border:1px solid black; height:50px; width:50px"></div> 

Comments

4

While it's true that consecutive calls to animate will give the appearance they are running at the same time, the underlying truth is they're distinct animations running very close to parallel.

To insure the animations are indeed running at the same time use:

$(function() { $('#first').animate({..., queue: 'my-animation'}); $('#second').animate({..., queue: 'my-animation'}); $('#first,#second').dequeue('my-animation'); }); 

Further animations can be added to the 'my-animation' queue and all can be initiated provided the last animation dequeue's them.

Cheers, Anthony

3 Comments

If you also need to trigger animations after a set of other animations have finished, here's an answer for that which relies on promises and may also be helpful: stackoverflow.com/a/35141159/4342230
Also, @Borgboy, your answer didn't work for me. According to stackoverflow.com/a/1218485/4342230 the animation queue for each object is different. You'd have to queue up all the animations and then do a new jQuery query that encompasses all of the elements that have queued animations so you can call dequeue on the results so they start together. In other words, $('#first, #second').dequeue().
@GuyPaddock - Yes, you are correct. I need to update my answer accordingly. It looks like this change occurred around jQuery 3.3.1 or thereafter.
2

See this brilliant blog post about animating values in objects.. you can then use the values to animate whatever you like, 100% simultaneously!

http://www.josscrowcroft.com/2011/code/jquery-animate-increment-decrement-numeric-text-elements-value/

I've used it like this to slide in/out:

 slide : function(id, prop, from, to) { if (from < to) { // Sliding out var fromvals = { add: from, subtract: 0 }; var tovals = { add: to, subtract: 0 }; } else { // Sliding back in var fromvals = { add: from, subtract: to }; var tovals = { add: from, subtract: from }; } $(fromvals).animate(tovals, { duration: 200, easing: 'swing', // can be anything step: function () { // called on every step // Slide using the entire -ms-grid-columns setting $(id).css(prop, (this.add - this.subtract) + 'px 1.5fr 0.3fr 8fr 3fr 5fr 0.5fr'); } }); } 

1 Comment

1

Posting my answer to help someone, the top rated answer didn't solve my qualm.

When I implemented the following [from the top answer], my vertical scroll animation just jittered back and forth:

$(function () { $("#first").animate({ width: '200px' }, { duration: 200, queue: false }); $("#second").animate({ width: '600px' }, { duration: 200, queue: false }); }); 

I referred to: W3 Schools Set Interval and it solved my issue, namely the 'Syntax' section:

setInterval(function, milliseconds, param1, param2, ...)

Having my parameters of the form { duration: 200, queue: false } forced a duration of zero and it only looked at the parameters for guidance.

The long and short, here's my code, if you want to understand why it works, read the link or analyse the interval expected parameters:

var $scrollDiv = '#mytestdiv'; var $scrollSpeed = 1000; var $interval = 800; function configureRepeats() { window.setInterval(function () { autoScroll($scrollDiv, $scrollSpeed); }, $interval, { queue: false }); }; 

Where 'autoScroll' is:

 $($scrollDiv).animate({ scrollTop: $($scrollDiv).get(0).scrollHeight }, { duration: $scrollSpeed }); //Scroll to top immediately $($scrollDiv).animate({ scrollTop: 0 }, 0); 

Happy coding!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.