Possible to reverse a CSS animation on class removal?
Want to reverse a CSS animation upon class removal? Use animation-direction: reverse
.
Toggle the class using JavaScript to trigger the reverse animation:
Setting animation-direction
to reverse
along with animation-fill-mode: both
confirms the element returns to its origin upon class toggle.
Make it smooth: add transitions and control the flip
Want to improve your reverse animation with enhanced effects? Here are some tips.
Get a better feel with ease-in-out
For a smoother transition, ease-in-out
is the perfect timing function. It mimics a natural feel by starting and ending the animation slowly:
Control pivot point with transform-origin
Conquer the axis of rotation for a more sophisticated animation. transform-origin
helps you take control:
JavaScript or jQuery - the choice is yours!
No matter what your preference, you've got control. Here are quick examples of class toggle, one with plain JavaScript and the other with jQuery:
Deep dive into manipulating animations
Let's go deeper and explore further into CSS and JavaScript, making your animations interactive.
Element style consistency
Consistency is key for a smooth user experience. Line-height, color, and size should remain consistent to retain visual balance:
Power of Animate.css
Animate.css library is your ready-to-use animation partner, helping you trigger animations through simple class additions:
Ease of :hover
In cases of simplicity, a :hover
pseudo-class is your ally. Pair it up with JavaScript and animations get a lot smoother:
Visualization
Let's break this down with a fun code-dance comparison:
Before Class Removal: 🕺💃👯 (Animations in action) Class Removed: 🎭➡️💺 (Animations take a chill pill)
When the class triggering animation is removed:
Stage Without Class: [🕺🪑, 💃🪑, 👯🪑]
To Reverse, roll the sequence backward with a new dance (class):
Add Reverse Class: 🔄🕺💃👯
Think flipbook: Forward animation 📖→, Backward animation 📖←
Was this article helpful?