Skip to content
Advertisement

Looking for an efficient way to animate toggle class

I have a CSS class that has left-margin: 150px and I just want to function it in an animated way. ToggleClass in jQuery works but without any animation. $(this).toggleClass("active", 1000, "easeInOutQuad");

I managed to do it animated using addClass and removeClass. But I am wondering if there is any easier way to do it. it takes 10 lines of code while there should be something much more efficient.

Any ideas?

JavaScript
JavaScript
JavaScript

Advertisement

Answer

I’d use CSS transition to do it. Add transition: margin-left 0.5s; (or similar) to your .box style rules:

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement