Skip to content

Instantly share code, notes, and snippets.

@vicainelli
Last active December 25, 2015 21:09
Show Gist options
  • Save vicainelli/7040536 to your computer and use it in GitHub Desktop.
Save vicainelli/7040536 to your computer and use it in GitHub Desktop.

Detect window size with Javascript

jQuery(document).ready(function($){
var $window = $(window),
$menu = $('div.menu');
function checkWindowSize() {
var width = $window.width();
if ( width < 824 ) {
return $menu.addClass('nav-mobile');
}
$menu.removeClass('nav-mobile');
}
$window
.resize(checkWindowSize)
.trigger('checkWindowSize');
checkWindowSize();
/* prepend menu icon */
$('div.menu').prepend('<div id="menu-icon">Menu</div>');
/* toggle nav */
$("#menu-icon").on("click", function(){
$("div.menu > ul").slideToggle();
$(this).toggleClass("active");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment