【问题标题】:Fix responsive menu parent toggling - jQuery/Javasicript修复响应式菜单父切换 - jQuery/Javascript
【发布时间】:2016-03-01 21:12:12
【问题描述】:

我正在使用 jQuery/javascript 和 css 构建一个完全响应的可访问菜单。

我一切正常,除了当视口小于 535px 并且显示切换菜单时,单击父级不会展开子级,只是直接链接到该页面。

我希望当您单击父项时,例如说“工具”,子项显示并且父项和子项都是可点击的链接。

这里有一个临时站点可供查看:website link

这是一个js示例:

            var ww = 0;

jQuery(document).ready(function($) {

    $(".menu").show();

    $(".menu .currenthas-child a, .menu .has-child a").each(function() {
        if ($(this).next().length > 0) {
            $(this).addClass("parent");
        };
    });

    $(".toggleMenu").click(function(e) {
        e.preventDefault();
        $(this).toggleClass("active");

        if($(this).hasClass("active")){
            //show menu
                $(".menu").removeClass("hidden").fadeIn(250);

        } else {
            //hide menu
                $(".menu").addClass("hidden");
        }

    });

    $(".menu li ul li").click(function(e) {
        if ($(".toggleMenu").hasClass("active")){
            $(".menu").toggleClass("hidden");
        }
    });

    //on click, anywhere on page, close all menu items
    $(document).click( function() {  
        $(".menu li").each(function() {
            if ($(this).hasClass("hover") || $(this).hasClass("keyhover")) {
                $(this).removeClass("hover");
                $(this).removeClass("keyhover");
                //console.log('hover removed');
            }
        });
    });


    //Focus State - Navigation
    $(".menu li").focusin(function (e) {
        if (!$(this).hasClass("keyhover")) {
            $(this).addClass("keyhover");
            showElement($(".menu"));
        } 
        //console.log('menu item focusin');
    });

    //Blur State - Navigation
    $(".menu li").focusout(function (e) {
        if ($(this).hasClass("keyhover")) {
            $(this).removeClass("keyhover");
        } 
        if ($(this).hasClass("hover")) {
            $(this).removeClass("hover");
        } 
        //console.log('menu item focusout');

    });

    //Focus State - Navigation Sub Items
    $(".menu li ul li").focusin(function () {
        if (!$(this).hasClass("glow")) {
            $(this).addClass("glow");
        } 
    });

    //Blur State - Navigation Sub Items
    $(".menu li ul li").focusout(function () {
        if ($(this).hasClass("glow")) {
            $(this).removeClass("glow");
        } 
    });

    //on window resize, recheck size and adjustMenu
    $(window).bind('resize orientationchange', function() {

        ww = viewportSize.getWidth();
        adjustMenu();

        //UN-COMMENT FOR DEBUG OUTPUT
        //var widthTest = document.documentElement.clientWidth;
        //var widthTest2 =  screen.width;
        //console.log("document.documentElement.clientWidth ="+widthTest);
        //console.log("screen.width = "+widthTest2);
        //console.log("viewport().width = "+ww);
        //var mql = window.matchMedia("screen and (max-width: 47.938em)");
        //console.log(mql);
    });

    var hideElement = function(elementToModify) {
        elementToModify.addClass("hidden");
    }

    var showElement = function(elementToModify) {
        elementToModify.removeClass("hidden");
    }

    var adjustMenu = function() {

        if (ww <= 535) {

            $(".toggleMenu").css("display", "inline-block");
            if (!$(".toggleMenu").hasClass("active")) {
                hideElement($(".menu"));
            } else {
                showElement($(".menu"));
            }

            $(".menu li h2.parent").unbind('click').bind('click', function(e) {
                // must be attached to anchor element to prevent bubbling
                e.preventDefault();

                if (e.stopPropagation){
                    e.stopPropagation();
                }
                else if(window.event){
                   window.event.cancelBubble=true;
                }

                //close any sibling menu items
                //$(this).parent().siblings('li').removeClass("hover");
                //$(this).parent().siblings('li').removeClass("keyhover");

                //handle click request
                if (!$(this).parent().hasClass("hover")) {
                    $(this).parent().addClass("hover");
                    //console.log('hover added');
                } else {
                    $(this).parent().removeClass("hover");
                    $(this).parent().removeClass("keyhover");
                    //console.log('hover removed');
                }

            });

        } 
        else if (ww > 535) {

            $(".toggleMenu").css("display", "none");
            $(".toggleMenu").removeClass("active");

            //show menu parents
            showElement($(".menu"));

            //hide any popped up menu sub-items
            $(".menu li").removeClass("hover");

            $(".menu li h2.parent").unbind('click').bind('click', function(e) {
                // must be attached to anchor element to prevent bubbling
                e.preventDefault();

                if (e.stopPropagation){
                    e.stopPropagation();
                }
                else if(window.event){
                   window.event.cancelBubble=true;
                }

                //close any sibling menu items
                $(this).parent().siblings('li').removeClass("hover");
                $(this).parent().siblings('li').removeClass("keyhover");

                //handle click request
                if (!$(this).parent().hasClass("hover")) {
                    $(this).parent().addClass("hover");
                    //console.log('hover added');
                } else {
                    $(this).parent().removeClass("hover");
                    $(this).parent().removeClass("keyhover");
                    //console.log('hover removed');
                }                
            });
        } 
    }

    //initialize the menu
    ww = viewportSize.getWidth();
    adjustMenu();
});

【问题讨论】:

    标签: javascript jquery html css responsive-design


    【解决方案1】:

    我认为你想要的是你的菜单有一个奇怪的行为。如果第二次点击主链接重定向到此页面,如何关闭子菜单?

    也就是说,你可以像这样做你想做的事:

      $(".menu li").click(function(e) {
        if (!$(this).children('ul').is('.open')) {
          e.preventDefault();
          $(this).children('ul').addClass('open').css({'left' : '0px'});
        }
      });
    

    最好测试子菜单是否可见,但您必须将 .sub-menu 类的 CSS 更改为 display:none 代替 left: -9999px

    更新

    您不能在子菜单上使用 slideDown 效果,因为它们具有 position : absolute 属性。因此,您需要处理媒体查询以更改它并将其设置为 display : none 属性。这样,您可以对click event 使用简单的slideDown 效果,并在相应的子菜单不可见时阻止该事件。

    看看这个示例:

    $(document).ready(function(){
      $(".dropdown").click(function(e) {
        if (!$(this).children('ul').is(':visible')) {
          e.preventDefault();
          $(this).children('ul').slideDown();
        }
      });
    });
    *, *:before, *:after {   
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    ul {
      list-style-type: none;
      width: 100%;
    }
    
    ul li {
      display: block;
      position: relative;
    }
    
    ul li a {
      color: #fff;
      text-decoration: none;
      cursor: pointer;
      padding: 10px 20px;
      display: block;
      width: 100%;
    }
    
    ul > li > a {
      background: #28AADC;
    }
    
    ul li a:hover {
      background: #00648C;
    }
    
    ul.submenu {
      display: none;
    }
    
    ul.submenu > li > a {
      background: #FFBE4F;
    }
    
    nav > ul {
      width: 400px;
    }
    
    @media screen and (min-width:536px){
      nav > ul > li {
        width: 33.33%;
        float: left;
      }  
      
      nav > ul > li:hover ul.submenu {
        display:block;
      }
      
      ul.submenu {
        position: absolute;
      }
    }
    
    @media screen and (max-width:535px){
      nav > ul > li {
        width: 100%;
        float: none;
      }  
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <nav>
      <ul>
        <li class="dropdown">
          <a href="http://stackoverflow.com">Nav 1</a>
          <ul class="submenu">
            <li><a href="#">Nav 1.1</a></li>
            <li><a href="#">Nav 1.2</a></li>
            <li><a href="#">Nav 1.3</a></li>
            <li><a href="#">Nav 1.4</a></li>
          </ul>
        </li>
        <li class="dropdown">
          <a href="http://stackoverflow.com">Nav 2</a>
          <ul class="submenu">
            <li><a href="#">Nav 2.1</a></li>
            <li><a href="#">Nav 2.2</a></li>
            <li><a href="#">Nav 2.3</a></li>
            <li><a href="#">Nav 2.4</a></li>
          </ul>
        </li>
        <li class="dropdown">
          <a href="http://stackoverflow.com">Nav 3</a>
          <ul class="submenu">
            <li><a href="#">Nav 3.1</a></li>
            <li><a href="#">Nav 3.2</a></li>
            <li><a href="#">Nav 3.3</a></li>
            <li><a href="#">Nav 3.4</a></li>
          </ul>
        </li>
      </ul>
    </nav>

    【讨论】:

    • 最后,我只想让父母在点击或触摸时切换打开,所有这些都是链接。尝试以几种不同的方式实现此代码,但没有运气......还有其他想法吗?
    • 也许我没有将函数放在脚本中的正确位置。我也按照推荐更改了 css,但没有运气。您对这个响应式菜单的功能有更好的了解吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    相关资源
    最近更新 更多