jquery - Check browser width after resize, without reload (Javascript) -
jquery - Check browser width after resize, without reload (Javascript) -
i have little piece of javascript checks browser's width on load and, depending on result, runs piece of jquery. display different menu on mobile devices
however, can happen user starts little browser on desktop, running jquery (and hence mobile menu). if resizes full-screen, menu doesn't change, because check on browser's width doesn't run again.
the question: how adapt javascript checks every time browser resized?
my code:
if (window.innerwidth <= 992) { jquery(document).ready(function($) { $(".main-menu").hide(); $(".mobile-nav-button").click(function() { $(".main-menu").slidetoggle(500); }); }); } else { jquery(document).ready(function($) { $(".mobile-nav-button").hide(); $(".mobile-cart-button").hide(); }); }
you can utilize resize function wrapper this:
function resize(){ // stuff here }
and phone call 1 time on page load, , recall on every resize event:
$(document).ready(function(){ resize(); $(window).resize(resize); });
instead of wrapper utilize anonymous function ($(window).resize(function(){/*do stuff here*/})
), can't phone call function on page load , you'd have repeat yourself.
in specific case you'd have take out document readys. heres how should look:
function resize(){ // first need show all, selectively hide based on page width $(".main-menu, .mobile-nav-button, .mobile-cart-button").show(); if (window.innerwidth <= 992) { $(".main-menu").hide(); // also, because bind click event here, you'll need unbind every time // otherwise executed multiple times after couple of resizes // (you @david , move document.ready below) $(".mobile-nav-button").off("click").click(function() { // slidetoggle toggle display of element, because wrapped in click() // gets executed onclick, not resize. also, don't want animation on resize. $(".main-menu").slidetoggle(500); }); } else { $(".mobile-nav-button").hide(); $(".mobile-cart-button").hide(); } } $(document).ready(function(){ resize(); $(window).resize(resize); });
a snippet below snippet code working. reduced width 700 pixels see effects @ smaller screen difference (because how snippet editor looks) works.
class="snippet-code-html lang-html prettyprint-override"><!doctype html> <html> <head> </head> <body> <div class="mobile-nav-button">mobile nav button</div> <div class="mobile-cart-button">mobile cart button</div> <div class="main-menu">main menu</dov> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript"> function resize(){ $(".mobile-nav-button, .mobile-cart-button, .main-menu").show(); if (window.innerwidth <= 700) { $(".main-menu").hide(); $(".mobile-nav-button").off("click").click(function() { $(".main-menu").slidetoggle(500); }); } else { $(".mobile-nav-button").hide(); $(".mobile-cart-button").hide(); } } $(document).ready(function(){ resize(); $(window).resize(resize); }); </script> </body> </html>
javascript jquery css
Comments
Post a Comment