Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ifthenelse
Forked from mandelbro/jQuery-transitionend
Last active September 4, 2018 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ifthenelse/f8c48d5c64e51739a2b5d566e779c9c7 to your computer and use it in GitHub Desktop.
Save ifthenelse/f8c48d5c64e51739a2b5d566e779c9c7 to your computer and use it in GitHub Desktop.
Quick jQuery plugin to get the duration of a CSS transition for an element. Support multiple transition values
/**
* jQuery.getTransitionDuration
* A jQuery plugin to get transition-duration in milliseconds
* Works with multiple values and supports all browser vendor prefixes
*
* Usage with single transition-duration values $(div).getTsDuration()
* Usage with multiple transition-duration values $(div).getTsDuration("opacity") (otherwise would return the first)
*
* Based on a gist by Chris Montes https://gist.github.com/mandelbro/4067903
*/
(function(factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof module === 'object' && module.exports) {
// Node/CommonJS
module.exports = function(root, jQuery) {
if (jQuery === undefined) {
if (typeof window !== 'undefined') {
jQuery = require('jquery');
} else {
jQuery = require('jquery')(root);
}
}
factory(jQuery, window, document);
return jQuery;
};
} else {
// Browser globals
factory(jQuery, window, document);
}
}(function($, window, document, undefined) {
"use strict";
var pluginName = "getTsDuration",
defaults = {
property: "",
};
// The actual plugin constructor
function Plugin(element, o) {
this.element = element;
// make options an object
var options = (typeof o !== "undefined" && typeof o !== "object") ? { property: "" + o } : o;
this.settings = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
// Avoid Plugin.prototype conflicts
$.extend(Plugin.prototype, {
init: function() {
var propRx = new RegExp(this.settings.property, 'g'),
propArray = (this.element.style.transitionProperty.length) ? this.element.style.transitionProperty.split(", ") : false,
propItem = "",
propIndex = 0;
// if there are multiple values, get the position of the property reqeusted
if (!!propArray && propArray.length) {
propItem = propArray.find(function(v) { return propRx.test(v); });
propIndex = propArray.indexOf(propItem);
}
// check the main transition duration property
if (this.element.style.transitionDuration) {
return Math.round(parseFloat(this.element.style.transitionDuration.split(", ")[propIndex]) * 1000);
} else {
// check the vendor transition duration properties
if (!!this.element.style.webkitTranstionDuration) return Math.round(parseFloat(this.element.style.webkitTranstionDuration.split(", ")[propIndex]) * 1000);
if (!!this.element.style.mozTranstionDuration) return Math.round(parseFloat(this.element.style.mozTranstionDuration.split(", ")[propIndex]) * 1000);
if (!!this.element.style.msTranstionDuration) return Math.round(parseFloat(this.element.style.msTranstionDuration.split(", ")[propIndex]) * 1000);
if (!!this.element.style.oTranstionDuration) return Math.round(parseFloat(this.element.style.oTranstionDuration.split(", ")[propIndex]) * 1000);
}
// if we're here, then no transition duration was found, return 0
return 0;
}
});
$.fn[pluginName] = function(options) {
return this.each(function() {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" +
pluginName, new Plugin(this, options));
}
});
};
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment