Skip to content

Instantly share code, notes, and snippets.

@maxxcrawford
Created January 10, 2017 16:21
Show Gist options
  • Save maxxcrawford/411d9493afdfdf014d6b8bf66d6b1bb9 to your computer and use it in GitHub Desktop.
Save maxxcrawford/411d9493afdfdf014d6b8bf66d6b1bb9 to your computer and use it in GitHub Desktop.
Switch global variable on screen size
(function($) {
"use strict";
var isMobile = false;
var windowWidth = $(window).width();
var mobileCheck = function(width, status) {
console.log('width: ' + width);
console.log('status: ' + status);
if (width >= 768) {
// desktop
if (status) {
// Make sure is mobile is off
isMobile = false;
}
} else {
// mobile
if (!status) {
// Make sure is mobile is on
isMobile = true;
}
}
}
$(document).ready(function() {
console.log('ready');
mobileCheck(windowWidth, isMobile);
});
$(window).resize(function() {
console.log('resize');
// refresh window width variable
windowWidth = $(window).width();
mobileCheck(windowWidth, isMobile);
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment