Skip to content

Instantly share code, notes, and snippets.

@zmmbreeze
Last active December 17, 2015 08:39
Show Gist options
  • Select an option

  • Save zmmbreeze/5581337 to your computer and use it in GitHub Desktop.

Select an option

Save zmmbreeze/5581337 to your computer and use it in GitHub Desktop.
detect media query change in javascript
/*http://css-tricks.com/media-query-change-detection-in-javascript-through-css-animations/?utm_source=feedly*/
body {
animation-duration: 0.001s;
}
@media screen and (min-width: 1000px) {
body {
animation-name: min-width-1000px;
}
}
@media screen and (min-width: 700px) {
body {
animation-name: min-width-700px;
}
}
@keyframes min-width-700px {
from { clip: rect(1px, auto, auto, auto); }
to { clip: rect(0px, auto, auto, auto); }
}
@keyframes min-width-1000px {
from { clip: rect(1px, auto, auto, auto); }
to { clip: rect(0px, auto, auto, auto); }
}
document.addEventListener(animationEnd, dispatchEvent, false);
// check the animation name and operate accordingly
function dispatchEvent(event) {
if (event.animationName === 'min-width-700px') {
document.body.innerHTML = 'Min width is 700px';
} else if (event.animationName === 'min-width-1000px') {
document.body.innerHTML = 'Min width is 1000px';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment