Home >Web Front-end >CSS Tutorial >How to Temporarily Disable CSS Transitions During DOM Manipulation?
How to Temporarily Disable CSS Transition Effects
When working with DOM elements that have CSS transitions applied, there may be situations where you need to temporarily disable these effects for smooth resizing. This article explores the most elegant solution to achieve this.
Disabling Transitions
To disable CSS transitions effectively, you can create a "notransition" class like this:
.notransition { -webkit-transition: none !important; -moz-transition: none !important; -o-transition: none !important; transition: none !important; }
Implementing with JavaScript
There are two ways to apply the "notransition" class with JavaScript:
Without jQuery:
someElement.classList.add('notransition'); // Disable transitions doWhateverCssChangesYouWant(someElement); someElement.offsetHeight; // Trigger a reflow, flushing the CSS changes someElement.classList.remove('notransition'); // Re-enable transitions
With jQuery:
$someElement.addClass('notransition'); // Disable transitions doWhateverCssChangesYouWant($someElement); $someElement[0].offsetHeight; // Trigger a reflow, flushing the CSS changes $someElement.removeClass('notransition'); // Re-enable transitions
Explanation
The problem with disabling transitions is that browsers buffer styling changes until JavaScript execution finishes and apply them in a single "reflow." This can result in unwanted animations. To force a reflow and flush the CSS changes, you need to read the element's offsetHeight property.
Additional Notes
The above is the detailed content of How to Temporarily Disable CSS Transitions During DOM Manipulation?. For more information, please follow other related articles on the PHP Chinese website!