Last active
August 29, 2015 14:03
-
-
Save itsoli/db4740baf67e1189ba75 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setCssTransition(target, property, duration, animation, delay) { | |
if (!target || !property || (property === 'none')) { | |
return; | |
} | |
if (target instanceof jQuery) { | |
target = target.get(0); | |
} | |
var transitionJs = css3prop.js('transition'); | |
var value; | |
if (property === 'all') { | |
if ((duration === 0) || (animation === 'none')) { | |
value = 'none'; | |
} else { | |
value = 'all ' + duration + 'ms ' + animation + ' ' + delay + 'ms'; | |
} | |
} else { | |
var components = target.style[transitionJs] | |
.split(',') | |
.map(Function.prototype.call, String.prototype.trim) | |
.filter(function (e) { return e; }); | |
var effectiveProperty = css3prop.css(property); | |
var allNoneExpr = /^(all|none)/; | |
var index = -1; | |
for (var i = 0; i < components.length; ++i) { | |
if ((index === -1) && (components[i].indexOf(effectiveProperty) === 0)) { | |
index = i; | |
} else if (components[i].match(allNoneExpr) !== null) { | |
index = -1; | |
components = []; | |
break; | |
} | |
} | |
if ((duration === 0) || (animation === 'none')) { | |
if (index !== -1) { | |
if (components.length > 1) { | |
components.splice(index, 1); | |
} else { | |
components = []; | |
} | |
if (components.length === 0) { | |
components = ['none']; | |
} | |
} | |
} else { | |
var transition = effectiveProperty + ' ' + duration + 'ms ' + animation + ' ' + delay + 'ms'; | |
if (index !== -1) { | |
components[index] = transition; | |
} else { | |
components.push(transition); | |
} | |
} | |
value = components.join(', '); | |
} | |
target.style[transitionJs] = value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment