Created
January 13, 2017 22:58
-
-
Save axelmichel/a47c89f66fa2282dcb1a4b24b72db390 to your computer and use it in GitHub Desktop.
Extracting the transition with the longest duration (and delay)
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 getMaxDurationTransitionProperty(el) { | |
// small helper to extract the values | |
function extract(str) { | |
return str | |
.replace(/[A-Z]/gi, "") | |
.split(", ") | |
.map(parseFloat); | |
}; | |
// get the current style | |
var style = getComputedStyle(el); | |
// get all transition properties | |
var props = style.transitionProperty.split(", "); | |
// we need delay and duration | |
var delays = extract(style.transitionDelay); | |
var durations = extract(style.transitionDuration); | |
// combinate delay and duration | |
var totals = durations.map(function(v, i) { | |
return v + delays[i]; | |
}); | |
// find the property with longest value | |
var max = totals[0]; | |
var maxIndex = 0; | |
for (var i = 1; i < totals.length; i++) { | |
if (totals[i] > max) { | |
maxIndex = i; | |
max = totals[i]; | |
} | |
} | |
// and return this property | |
return props[maxIndex]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment