简体   繁体   中英

responsive jquery nav interaction - issue on resize

Requirement: Horizontal nav in 900px or above and vertical in 900 below. but in 900 or below, the nav should hide by default and appear only when clicked on the "header"

What I have so far: http://jsfiddle.net/0rmgpjtr/3/

   $(document).ready(function(){

$('.dropdown ul:first').show();
    $('.dropdown ul:first').css("display:table");
    $('.dropdown li').click(function (e) {
                $(this).addClass('active').siblings('li').removeClass('active');
                $(this).children('ul').slideToggle('fast');
                $(this).siblings('li').find('ul').slideUp('fast');
                e.preventDefault();
                e.stopPropagation();

          });
              $(document).on('click', function (event) {
                $('.dropdown li').children('ul').slideUp('fast');
              });

});
$(window).on("resize", function () {

     if($(window).width()<=900){
         console.log("800");
        $('.ch-logo').bind('click',function(){
            $('.dropdown').slideToggle('fast');
        })
    }else{
                 console.log("900+");
        $('.ch-logo').unbind()
    }

}).resize();

Issue: 1. binding and unbinding the header click event is not working 2. when i click on parent link to open subnav, it is flickering after i resize the browser - actually any interaction, starts flickering the menu, may be because its tied to resize function.

http://jsfiddle.net/extca38f/

$(window).on("resize", function () {

     if($(window).width()<=900){
         console.log("800");
         $('.dropdown').hide();
        $('.ch-logo').bind('click',function(){
            $('.dropdown').toggle();
        });
     }else{
         $('.dropdown').show();
     }
}).resize();

Use toggle instead of slidetoggle Demo Here

$(window).on("resize", function () {

     if($(window).width()<=900){
         console.log("800");
         $('.dropdown').hide();
        $('.ch-logo').bind('click',function(){
            $('.dropdown').toggle('slow');
        });
     }else{
         $('.dropdown').show();
     }
}).resize();

Check the differnce between toggle and slidetoggle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM