Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ToggleClass animate jQuery?

I have a section on my website that when a user clicks I would like it to expand, I'm using the jQuery's toggleClass for this...

expandable: function(e) {
    e.preventDefault();
    $(this).closest('article').toggleClass('expanded', 1000);
}

This is working fine, only I'd like to somehow animate it. In chrome my article slowly grows to the new size, only in Firefox it 'instantly' resizes itself with no animation, is there a way to have this animate?

like image 776
Liam Avatar asked Jun 24 '12 16:06

Liam


People also ask

What is toggleClass in jQuery?

The toggleClass() method toggles between adding and removing one or more class names from the selected elements. This method checks each element for the specified class names. The class names are added if missing, and removed if already set - This creates a toggle effect.

How do I toggle show and hide in jQuery?

jQuery toggle() MethodThe toggle() method toggles between hide() and show() for the selected elements. This method checks the selected elements for visibility. show() is run if an element is hidden. hide() is run if an element is visible - This creates a toggle effect.

How do you toggle with multiple classes?

Answer. Yes, you can toggle multiple classes using a single . toggleClass() call. To do so, you can separate the class names with spaces.


3 Answers

jQuery UI extends the jQuery native toggleClass to take a second optional parameter: duration

toggleClass( class, [duration] )

Docs + DEMO

like image 107
gdoron is supporting Monica Avatar answered Oct 09 '22 16:10

gdoron is supporting Monica


.toggleClass() will not animate, you should go for slideToggle() or .animate() method.

like image 26
thecodeparadox Avatar answered Oct 09 '22 15:10

thecodeparadox


You can simply use CSS transitions, see this fiddle

.on {
color:#fff;
transition:all 1s;
}

.off{
color:#000;
transition:all 1s;
}
like image 18
Dario Corno Avatar answered Oct 09 '22 15:10

Dario Corno