Skip to content

Instantly share code, notes, and snippets.

@snorpey
Last active December 26, 2015 23:28
Show Gist options
  • Save snorpey/7230329 to your computer and use it in GitHub Desktop.
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.
// 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