Created
December 15, 2016 05:47
-
-
Save motss/cb65fd2034b6937c4cfd0264fc458cf6 to your computer and use it in GitHub Desktop.
Feature detection for Native CSS Properties aka CSS Custom Variables
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
// Not too sure if it will trigger layout and paint for other CSS properties. | |
// I just use opacity which only triggers composite by default! | |
function hasNativeCSSProperties() { | |
const opacity = 1; | |
const el = document.body; | |
let hasNativeCSSProperties = false; | |
// Setup CSS properties. | |
el.style.setProperty('--test-opacity', opacity); | |
el.style.setProperty('opacity', 'var(--test-opacity)'); | |
// Feature detect then remove all set properties. | |
hasNativeCSSProperties = window.getComputedStyle(el).opacity == opacity; | |
el.style.setProperty('--test-opacity', ''); | |
el.style.opacity = ''; | |
return hasNativeCSSProperties; | |
} | |
hasNativeCSSProperties(); |
1
is the default value for opacity
.
so window.getComputedStyle(document.body).opacity
give 1
even if css variables are'n supported
you should test it with other value like 0.5
or other property like z-index
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! My version: