Created
January 10, 2017 16:21
-
-
Save maxxcrawford/411d9493afdfdf014d6b8bf66d6b1bb9 to your computer and use it in GitHub Desktop.
Switch global variable on screen size
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
(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