Created
January 11, 2022 08:48
-
-
Save gemcave/333fc56d006de4f0094a415131711fe8 to your computer and use it in GitHub Desktop.
Check reduced-motion with js
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
// variant 1 | |
function getPrefersReducedMotion() { | |
const mediaQueryList = window.matchMedia( | |
'(prefers-reduced-motion: no-preference)' | |
); | |
const prefersReducedMotion = !mediaQueryList.matches; | |
return prefersReducedMotion; | |
} | |
// variant 2 - complex with event | |
const mediaQueryList = window.matchMedia( | |
'(prefers-reduced-motion: no-preference)' | |
); | |
const listener = (event) => { | |
const getPrefersReducedMotion = getPrefersReducedMotion(); | |
}; | |
mediaQueryList.addListener(listener); | |
//// Later: | |
mediaQueryList.removeListener(listener); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment