Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery is animated on multiple elements, which is better?

I am using jQuerys animate function on a plugin, on many elements simulatensly with different durations for each element. I want to know if any animation is running or if there is no animation at all. So i came up with this:

if( $div1.is(':animated') || $div2.is(':animated') || $div3.is(':animated') ) return true;
else return false;

or this:

if( $div1.add($div2).add($div3).is(':animated') ) return true;
else return false;

Which is better???

Do you know any other method???

I dont want this code $("*").is(':animated'); because it will check all animations and from other plugins animations.

Have in mind that I have many elements, not just 3.

Thanks for reading...

like image 837
gadlol Avatar asked Dec 16 '22 13:12

gadlol


2 Answers

I would go with the second as it is less code to read and think about it.

Generally, DRY is better.

You could also refactor that...

return $div1.add($div2).add($div3).is(':animated');
like image 151
alex Avatar answered Dec 24 '22 13:12

alex


Perhaps you could put a class on the divs you would like to check for animation?

if ($(".myclass").is(":animated")) return true;
like image 32
Ben Cull Avatar answered Dec 24 '22 14:12

Ben Cull