Here is a reliable way to skip CSS element transitions, the code comes from the Mozilla X-Tag Web Component Library :
var prefix = (function () { var styles = window.getComputedStyle(document.documentElement, ''), pre = (Array.prototype.slice .call(styles) .join('') .match(/-(moz|webkit|ms)-/) || (styles.OLink === '' && ['', 'o']) )[1]; return { dom: pre == 'ms' ? pre.toUpperCase() : pre, lowercase: pre, css: '-' + pre + '-', js: pre[0].toUpperCase() + pre.substr(1) }; })(); var requestFrame = (function(){ var raf = window.requestAnimationFrame || window[prefix.lowercase + 'RequestAnimationFrame'] || function(fn){ return window.setTimeout(fn, 20); }; return function(fn){ return raf.call(window, fn); }; })(); var skipTransition = function(element, fn, bind){ var prop = prefix.js + 'TransitionProperty'; element.style[prop] = element.style.transitionProperty = 'none'; var callback; if (fn) callback = fn.call(bind); requestFrame(function(){ requestFrame(function(){ element.style[prop] = element.style.transitionProperty = ''; if (callback) requestFrame(callback); }); }); };
HOW TO USE IT - this snippet assumes that you set the transition to the width of the foo element and want to immediately change it to 100px without the transition of the element.
var foo = document.querySelector('#foo') skipTransition(foo, function(){ foo.style.width = '100px'; });
DIRECT EXAMPLE
Click on each of the colored divs in the example - the red div has its own width style set in the skipTransition function, thereby preventing the transition and setting the style immediately. The blue div has a usually set width without skipTransition, and the CSS transition effect happens as usual: http://codepen.io/csuwldcat/pen/nFlse
source share