javascript - function triggers on window width but will not turn off if bigger than set width -
here have work if window width under 1024 still trigger though set trigger if on 1024
$(function () { $('#hamburger').click(function () { $('div.burger_nav').slidetoggle(); }); $(window).resize(function () { if ($(window).width() < 1024) { $('.nav_shown').hide(); $('div.footerdiv_2').hide(); $('div.hidden_nav').hide(); $('div.burger_btn').show(); $('#ft').removeclass('footerdiv_3').addclass('footer_img_clear'); } //end of if else { $(".nav_shown").show(); $('.footerdiv_2').show(); $('div.burger_btn').hide(); $('#ft').removeclass('footer_img_clear').addclass('footerdiv_3'); $(document).scroll(function () { var headershow = $(this).scrolltop(); if (headershow > 200) { $('div.hidden_nav').fadein(); $(".nav_shown").hide(); } else { $('div.hidden_nav').fadeout(); $(".nav_shown").show(); } }); } //end of else }); });
it looks want modify document when window reaches breakpoint, in case 1024 pixels. known responsive web design.
instead of updating screen every time on resize, it's useful set flag triggering breakpoint.
$(function() { $('#hamburger').click(function(){ $('div.burger_nav').slidetoggle(); }); var currentlysmall = false; function update() { if ($(window).width() < 1024 && !currentlysmall) { currentlysmall = true; console.log('less 1024'); $('.nav_shown').hide(); $('div.footerdiv_2').hide(); $('div.hidden_nav').hide(); $('div.burger_btn').show(); $('#ft').removeclass('footerdiv_3').addclass('footer_img_clear'); } else if ($(window).width() >= 1024 && currentlysmall) { currentlysmall = false; console.log('more 1024'); $(".nav_shown").show(); $('.footerdiv_2').show(); $('div.burger_btn').hide(); $('#ft').removeclass('footer_img_clear').addclass('footerdiv_3'); } } //calling in else part above bind new scroll event each time //instead, if should happen when screen large, use //flag created $(document).scroll(function () { if (!currentlysmall) { var headershow = $(this).scrolltop(); if (headershow > 200) { $('div.hidden_nav').fadein(); $(".nav_shown").hide(); } else { $('div.hidden_nav').fadeout(); $(".nav_shown").show(); } } }); $(window).resize(update); update(); //force initial calculation since resize won't called when page loads });
now changes make in above update()
function occur when screen sizes changes past 1024 breakpoint instead of every time screen resized.
fiddle: http://jsfiddle.net/25nfqxzk/1/
edit expanding off blgt's comment, don't believe need bind scroll event in if-else. can assigned outside , use same trigger flags.
Comments
Post a Comment