Last active
December 17, 2015 08:39
-
-
Save zmmbreeze/5581337 to your computer and use it in GitHub Desktop.
detect media query change in javascript
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
| /*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); } | |
| } |
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
| 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