From 52b4790104d8caec15ba42debdba298e02bba725 Mon Sep 17 00:00:00 2001 From: Joseph Delgado Date: Thu, 30 Jun 2016 05:05:20 -0400 Subject: [PATCH] Add support for .stopTransition(), stopping the current transition with it's computed, fractional values. --- jquery.transit.js | 59 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/jquery.transit.js b/jquery.transit.js index a2b47b8..0e1ac09 100644 --- a/jquery.transit.js +++ b/jquery.transit.js @@ -74,6 +74,7 @@ // Check for the browser's transitions support. support.transition = getVendorPropertyName('transition'); + support.transitionProp = getVendorPropertyName('transitionProperty'); support.transitionDelay = getVendorPropertyName('transitionDelay'); support.transform = getVendorPropertyName('transform'); support.transformOrigin = getVendorPropertyName('transformOrigin'); @@ -651,7 +652,11 @@ self.bind(transitionEnd, cb); } else { // Fallback to timers if the 'transitionend' event isn't supported. - window.setTimeout(cb, i); + var timer = window.setTimeout(cb, i); + + // Downsider: + // Store the timer callback, so it can be stopped later + self.data('timerCallback', timer); } // Apply transitions. @@ -666,7 +671,7 @@ // Defer running. This allows the browser to paint any pending CSS it hasn't // painted yet before doing the transitions. var deferredRun = function(next) { - this.offsetWidth = this.offsetWidth; // force a repaint + this.offsetWidth; // force a repaint run(next); }; @@ -677,6 +682,56 @@ return this; }; + $.fn.stopTransition = $.fn.stopTransit = function(doCallback) { + this.each(function() { + // Keep a reference to the JQuery element + var self = $(this); + + // Get the string used by CSS to transition the element + var transitPropertyString = this.style[support.transitionProp].replace(/\s+/g, ''); + + if (transitPropertyString && transitPropertyString.length != 0) { + // Capture the style mid-animation + var capturedStyle = window.getComputedStyle(this); + + // Iterate over the transiting properties and freeze them + var properties = transitPropertyString.split(','); + for (var i=0;i