JQuery - if the element has a class, do this

I need a jQuery script that will see if any element has a specific class and performs an action, such as a change position.

That's it, but I don’t think it will work.

$("a.contact").toggle(function() { $("#contact").animate({ right: '0' }, 2000); if ($("#about").hasClass("opened")) { $("#about").animate({ right: -700 + "px" }, 2000); } }, function() { $("#contact").animate({ right: -700 + "px" }, 2000); }); 
+71
javascript jquery
Dec 30 '10 at 18:10
source share
1 answer

First, you are missing parentheses in a conditional expression:

 if ($("#about").hasClass("opened")) { $("#about").animate({right: "-700px"}, 2000); } 

But you can also simplify this:

 $('#about.opened').animate(...); 

If #about does not have an opened class, it will not be animated.

If the problem is related to the animation itself, we will need to learn more about your positioning of the element (absolute absolute inside relative parent? Does the parent have a layout?)

+116
Dec 30 '10 at 18:16
source share



All Articles