Skip to content

Instantly share code, notes, and snippets.

@itsoli
Last active August 29, 2015 14:03
Show Gist options
  • Save itsoli/db4740baf67e1189ba75 to your computer and use it in GitHub Desktop.
Save itsoli/db4740baf67e1189ba75 to your computer and use it in GitHub Desktop.
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