Last active
September 3, 2015 15:38
-
-
Save matherton/2dba558b69adb0bdf6cf to your computer and use it in GitHub Desktop.
JS resize function for mobile and tablet
This file contains 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
$(window).resize(function() { | |
//var viewportWidth = $(window).width(); jQuery selector has inconsistencies with Media Queries | |
var viewportWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; // JS better consistency with Media Queries | |
/*Mobile view*/ | |
if (( viewportWidth <= 600 ) || ( viewportWidth > 776 ) && (viewportWidth < 921)) { | |
//Setup Markup to add attribute requirements for mobile | |
console.log( "on mobile" ); | |
} | |
/*Descktop view */ | |
else { | |
console.log( "NOT on mobile" ); | |
} | |
}); | |
$(window).trigger('resize'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After doing a bit of testing/googling turned out that using JS to define the viewport was more consistent than jQuery. After a bit more trial and error it appears that using jQuery like so:
var viewportWidth = $(window).outerWidth(true);
is just as good as defining the viewportWidth var with JS