What is the cleanest way to disable CSS transition effects temporarily?
Short Answer
Use this CSS:
.notransition {
-webkit-transition: none !important;
-moz-transition: none !important;
-o-transition: none !important;
transition: none !important;
}
Plus either this JS (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
Or this JS 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
... or equivalent code using whatever other library or framework you're working with.
Explanation
This is actually a fairly subtle problem.
First up, you probably want to create a 'notransition' class that you can apply to elements to set their *-transition
CSS attributes to none
. For instance:
.notransition {
-webkit-transition: none !important;
-moz-transition: none !important;
-o-transition: none !important;
transition: none !important;
}
(Minor aside - note the lack of an -ms-transition
in there. You don't need it. The first version of Internet Explorer to support transitions at all was IE 10, which supported them unprefixed.)
But that's just style, and is the easy bit. When you come to try and use this class, you'll run into a trap. The trap is that code like this won't work the way you might naively expect:
// Don't do things this way! It doesn't work!
someElement.classList.add('notransition')
someElement.style.height = '50px' // just an example; could be any CSS change
someElement.classList.remove('notransition')
Naively, you might think that the change in height won't be animated, because it happens while the 'notransition' class is applied. In reality, though, it will be animated, at least in all modern browsers I've tried. The problem is that the browser is caching the styling changes that it needs to make until the JavaScript has finished executing, and then making all the changes in a single reflow. As a result, it does a reflow where there is no net change to whether or not transitions are enabled, but there is a net change to the height. Consequently, it animates the height change.
You might think a reasonable and clean way to get around this would be to wrap the removal of the 'notransition' class in a 1ms timeout, like this:
// Don't do things this way! It STILL doesn't work!
someElement.classList.add('notransition')
someElement.style.height = '50px' // just an example; could be any CSS change
setTimeout(function () {someElement.classList.remove('notransition')}, 1);
but this doesn't reliably work either. I wasn't able to make the above code break in WebKit browsers, but on Firefox (on both slow and fast machines) you'll sometimes (seemingly at random) get the same behaviour as using the naive approach. I guess the reason for this is that it's possible for the JavaScript execution to be slow enough that the timeout function is waiting to execute by the time the browser is idle and would otherwise be thinking about doing an opportunistic reflow, and if that scenario happens, Firefox executes the queued function before the reflow.
The only solution I've found to the problem is to force a reflow of the element, flushing the CSS changes made to it, before removing the 'notransition' class. There are various ways to do this - see here for some. The closest thing there is to a 'standard' way of doing this is to read the offsetHeight
property of the element.
One solution that actually works, then, is
someElement.classList.add('notransition'); // Disable transitions
doWhateverCssChangesYouWant(someElement);
someElement.offsetHeight; // Trigger a reflow, flushing the CSS changes
someElement.classList.remove('notransition'); // Re-enable transitions
Here's a JS fiddle that illustrates the three possible approaches I've described here (both the one successful approach and the two unsuccessful ones):
http://jsfiddle.net/2uVAA/131/
Can I temporarily turn off all CSS3 transitions/animations whilst an element is built?
I’d suggest applying your animations/transitions via a class that’s added by JavaScript after the menu is built.
There is the animation-play-state
property which can pause animations, but that only appeared (prefixed) in Safari 5 and Chrome 4 (as opposed to Safari 4 and Chrome 2 for the other animation properties), and I’m not sure if it’d work for your purposes.
How to turn off CSS for faster automation tests execution?
I put another answer for your exact question on JavaScript Executor
. let's to clarify the JavaScript
code,
- make a
style
tag element. - add
id
attribute withstyle-tag
value. - make a string of that
CSS
es I answered in another answer. - put or append the string into
style
tag. - append the
style
tohead
tag inDOM
.
Now let's write above states to JavaScript
codes:
const styleElement = document.createElement('style');
styleElement.setAttribute('id','style-tag');
const styleTagCSSes = document.createTextNode('*,:after,:before{-webkit-transition:none!important;-moz-transition:none!important;-ms-transition:none!important;-o-transition:none!important;transition:none!important;-webkit-transform:none!important;-moz-transform:none!important;-ms-transform:none!important;-o-transform:none!important;transform:none!important}');
styleElement.appendChild(styleTagCSSes);
document.head.appendChild(styleElement);
With this codes you could import my another answer CSS
es to DOM
. so you can put below string to your JavaScript Executor
:
"const styleElement = document.createElement('style');styleElement.setAttribute('id','style-tag');const styleTagCSSes = document.createTextNode('*,:after,:before{-webkit-transition:none!important;-moz-transition:none!important;-ms-transition:none!important;-o-transition:none!important;transition:none!important;-webkit-transform:none!important;-moz-transform:none!important;-ms-transform:none!important;-o-transform:none!important;transform:none!important}');styleElement.appendChild(styleTagCSSes);document.head.appendChild(styleElement);"
If you wanna revert this action, just use:
document.getElementById('style-tag').remove();
Hope these codes help you.
How to enable and disable css transition in one js-event?
There is 2 ways you can do it -
1# - Combining your function with CSS animation
$(document).ready(function() { $('#goLeft').on('click', function() { $('#box').removeClass('soft left-right'); $('#box').css('left', '300px'); }); $('#goRight').on('click', function() { $('#box').addClass('soft').removeClass('left-right'); $('#box').css('left', '400px'); }); $('#goLeftAndRight').on('click', function() { $('#box').addClass('left-right').removeClass('soft'); });});
h2 { cursor: pointer;}
#box { position: fixed; top: 100px; left: 400px; height: 100px; width: 100px; background-color: #358;}
#box p { padding: 5px; color: #fff; text-align: center;}
.soft { transition: all 0.3s ease-out;}@keyframes leftRight{ 0%{left:300px} 100%{left:400px}}
.left-right{ left:400px animation: leftRight 0.3s forwards ease-in-out; -webkit-animation: leftRight 0.3s forwards ease-in-out;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><html>
<body> <h2 id="goLeft">jump left</h2> <h2 id="goRight">slide right</h2> <h2 id="goLeftAndRight">jump left and slide right</h2> <div id="box"> <p>use headers to move</div></body>
</html>
Disable JQuery automatic animation
Perhaps the element you are changing the height of has a css
transition property that is responsibe for the animation.
$(function() { $('.myClass').css('width', '100px');});
.myClass { height: 50px; width: 300px; background-color: red;}.transition { transition: width 3s;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>with transition<div class="myClass transition"></div>
without transition<div class="myClass"></div>
Related Topics
Sound Effects in JavaScript/Html5
Node.Js App Can't Run on Port 80 Even Though There's No Other Process Blocking the Port
Execjs::Runtimeerror on Windows Trying to Follow Rubytutorial
Find the Longest Common Starting Substring in a Set of Strings
Rails Js.Erb File Cannot Find Method "Render"
Using Ruby Variable in JavaScript (In App View)
How to Return the Response from an Asynchronous Call
How to Change Style of Iframe Content Cross-Domain
Trigger CSS Transition on Appended Element
How to Change a Class CSS With a Greasemonkey/Tampermonkey Script
Inject CSS Stylesheet as String Using JavaScript
JavaScript: Find Div'S Line-Height, Not CSS Property But Actual Line-Height
Ios 7'S Blurred Overlay Effect Using Css
Detect a Finger Swipe Through JavaScript on the Iphone and Android
How to Correctly Detect Orientation Change Using Phonegap on Ios
Confirmation Before Closing of Tab/Browser
How to Hide a ≪Option≫ in a ≪Select≫ Menu With Css