Skip to content

Instantly share code, notes, and snippets.

@6ui11em
Created February 22, 2018 21:28
Show Gist options
  • Save 6ui11em/74dd57e6cf958c9ac709128878a42d03 to your computer and use it in GitHub Desktop.
Save 6ui11em/74dd57e6cf958c9ac709128878a42d03 to your computer and use it in GitHub Desktop.
Break points javascript vanilla #js #vanilla #javascript
// 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