Last active
December 26, 2015 23:28
-
-
Save snorpey/7230329 to your computer and use it in GitHub Desktop.
Get the CSS transition duration of a DOM element, cross-browser. without jQuery.
This file contains hidden or 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
// based on http://stackoverflow.com/a/13008597 | |
function getTransitionDuration ( element, with_delay ) | |
{ | |
var prefixes = ' webkit moz ms o khtml'.split( ' ' ); | |
var result = 0; | |
var duration, delay, prefix; | |
for ( var i = 0; i < prefixes.length; i++ ) | |
{ | |
prefix = prefixes[i] + '-'; | |
if ( prefixes[i] === '' ) | |
{ | |
prefix = ''; | |
} | |
duration = element.style[ camelCase( prefix + 'transition-duration' ) ]; | |
if ( duration ) | |
{ | |
duration = ( duration.indexOf( 'ms' ) >- 1 ) ? parseFloat( duration ) : parseFloat( duration ) * 1000; | |
if ( with_delay ) | |
{ | |
delay = element.style[ camelCase( prefix + 'transition-delay' ) ]; | |
if ( delay ) | |
{ | |
duration += ( delay.indexOf( 'ms' ) >- 1 ) ? parseFloat( delay ) : parseFloat( delay ) * 1000; | |
} | |
} | |
result = duration; | |
break; | |
} | |
} | |
return result; | |
} | |
// based on http://stackoverflow.com/a/10425344/229189 | |
function camelCase ( input ) | |
{ | |
return input | |
.toLowerCase() | |
.replace( | |
/-(.)/g, | |
function ( match, group_1 ) | |
{ | |
return group_1.toUpperCase(); | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment