Skip to content

Instantly share code, notes, and snippets.

@gemcave
Created January 11, 2022 08:48
Show Gist options
  • Save gemcave/333fc56d006de4f0094a415131711fe8 to your computer and use it in GitHub Desktop.
Save gemcave/333fc56d006de4f0094a415131711fe8 to your computer and use it in GitHub Desktop.
Check reduced-motion with js
// 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