Created
October 17, 2012 21:20
-
-
Save dieseltravis/3908275 to your computer and use it in GitHub Desktop.
Feature-detecting browser support for CSS Transitions on a pseudo-element like :before or :after
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
Currently only IE10 and Firefox support CSS Transitions on pseudo-elements like :before and :after. | |
Original SO question that inspired me: | |
http://stackoverflow.com/questions/12922619/how-would-i-detect-css-transition-support-on-before-pseudo-elements-with-javasc | |
jQuery version demo: | |
http://jsbin.com/eliwum/3 | |
javascript version demo: | |
http://jsbin.com/ekafan/1 |
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
var isTransitionSupported = (function (pseudo, transProp, transPropStart, transPropEnd) { | |
var ticks = (new Date()).valueOf(), | |
id = pseudo + transProp + '-' + ticks, | |
prefixes = ['o', 'ms', 'moz', 'webkit'], | |
prop = "transition: " + transProp + " 99s linear;", | |
allprops = (function () { | |
var props = ""; | |
for (var l = prefixes.length; l--;) { | |
props += "-" + prefixes[l] + "-" + prop; | |
} | |
return props + prop; | |
}()), | |
body = document.body || document.createElement('body'), | |
node = document.createElement('div'), | |
css = "<style>" + | |
"#" + id + "{position:absolute;left:-999em;}" + | |
"#" + id + ":" + pseudo + "{display:block;content:'M';" + transProp + ":" + transPropStart + ";}" + | |
"#" + id + ".t" + ticks + ":" + pseudo + "{" + allprops + transProp + ":" + transPropEnd + ";}" + | |
"</style>", | |
bct = document.createElement('div'), | |
isSupported = false; | |
bct.id = id; | |
node.innerHTML += css; | |
node.appendChild(bct); | |
body.appendChild(node); | |
try { | |
// get style value before any changes | |
window.getComputedStyle(bct, ':' + pseudo).getPropertyValue(transProp); | |
bct.className += "t" + ticks; | |
// test style after changes | |
isSupported = (window.getComputedStyle(bct, ':' + pseudo).getPropertyValue(transProp) !== transPropEnd); | |
} catch (e) {} | |
node.parentNode.removeChild(node); | |
return isSupported; | |
}("before", "width", "0px", "1000px")); | |
document.documentElement.className += ' ' + (isTransitionSupported ? '' : 'no-') + "pseudo-trans"; |
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() { | |
var isTransitionSupported = (function (pseudo, transProp, transPropStart, transPropEnd) { | |
var id = pseudo + transProp + '-' + (new Date()).valueOf(), | |
prefixes = ['o', 'ms', 'moz', 'webkit'], | |
prop = "transition: " + transProp + " 99s linear;", | |
allprops = (function () { | |
var props = ""; | |
for (var l = prefixes.length; l--;) { | |
props += "-" + prefixes[l] + "-" + prop; | |
} | |
return props + prop; | |
}()), | |
$css = $("<style>" + | |
"#" + id + "{position:absolute;left:-999em;}" + | |
"#" + id + ":" + pseudo + "{display:block;content:'M';" + transProp + ":" + transPropStart + ";}" + | |
"#" + id + ".t:" + pseudo + "{" + allprops + transProp + ":" + transPropEnd + ";}" + | |
"</style>"), | |
$bct = $('<div id="' + id + '" />'); | |
$css.appendTo("head"); | |
$bct.appendTo("body"); | |
try { | |
// get style value before any changes | |
window.getComputedStyle($bct[0], ':' + pseudo).getPropertyValue(transProp); | |
$bct.addClass("t"); | |
// test style after changes | |
return (window.getComputedStyle($bct[0], ':' + pseudo).getPropertyValue(transProp) !== transPropEnd); | |
} catch (e) { | |
return false; | |
} | |
}("before", "width", "0px", "1000px")); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment