Created
February 22, 2018 21:28
-
-
Save 6ui11em/74dd57e6cf958c9ac709128878a42d03 to your computer and use it in GitHub Desktop.
Break points javascript vanilla #js #vanilla #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
// https://gomakethings.com/breakpoint-conditional-javascript-in-vanilla-js/ | |
// Define our viewportWidth variable | |
var viewportWidth; | |
// Set/update the viewportWidth value | |
var setViewportWidth = function () { | |
viewportWidth = window.innerWidth || document.documentElement.clientWidth; | |
} | |
// Log the viewport width into the console | |
var logWidth = function () { | |
if (viewportWidth > 640) { | |
console.log('Wide viewport'); | |
} else { | |
console.log('Small viewport'); | |
} | |
} | |
// Set our initial width and log it | |
setViewportWidth(); | |
logWidth(); | |
// On resize events, recalculate and log | |
window.addEventListener('resize', function () { | |
setViewportWidth(); | |
logWidth(); | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment