简体   繁体   English

单击菜单项时,移动菜单不会折叠

[英]Mobile menu doesn't collapse when a menu item is clicked

I'm having problem with a template where mobile menu doesn't collapse when a menu item is clicked. 我的模板有问题,单击菜单项时,移动菜单不会折叠。 Tried to solve it adding extra class, extra ID's but persist. 试图解决此问题,添加了额外的类,额外的ID,但是仍然存在。 It only closes/hide when clicking again on hamburger icon. 仅当再次单击汉堡包图标时,它才会关闭/隐藏。 And this is particularly annoying... 这特别烦人...

The navbar html code is: 导航栏html代码为:

<!-- Fixed navbar -->
    <div class="navbar navbar-smak navbar-fixed-top" id="navbar" role="navigation">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"><i class="fa fa-bars"></i></a>
                <h1><a class="navbar-brand animate" href="#home">TEST SITE</a></h1>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav navbar-right animate">
                    <li><a class="btn-navbar" href="#home">Home</a></li>
                    <li><a href="#blog">Blog</a></li>
                    <li><a href="#contact">Contacto</a></li>
                </ul>
            </div>
            <!--/.nav-collapse -->
        </div>
    </div>

This is the dummy html file I'm working on, feel free to download and check it: https://aleare.com.ar/testing3/index2tst.html 这是我正在处理的虚拟html文件,请随时下载并检查: https : //aleare.com.ar/testing3/index2tst.html

This is the function that handle the collapsing menu: 这是处理折叠菜单的功能:

$('a[href*=#]').each(function () {
    if (filterPath(location.pathname) == filterPath(this.pathname) && location.hostname == this.hostname && this.hash.replace(/#/, '')) {
        var $targetId = $(this.hash),
            $targetAnchor = $('[name=' + this.hash.slice(1) + ']');
        var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;

        if ($target) {

            $(this).click(function () {

                //Hack collapse top navigation after clicking
                topMenu.parent().attr('style', 'height:0px').removeClass('in'); //Close navigation
                $('.navbar .btn-navbar').addClass('collapsed');

                var targetOffset = $target.offset().top - 52;
                $('html, body').animate({
                    scrollTop: targetOffset
                }, 800);
                return false;
            });
        }
    }
});

Does someone know why it's failing? 有人知道为什么失败吗?

You must have a little more markup, eg topMenu and filterPath. 您必须有一些标记,例如topMenu和filterPath。

However, if I understand your question correctly it sounds like you want the mobile menu to toggle closed when you select an item from the mobile menu. 但是,如果我正确理解了您的问题,这听起来像是您希望从移动菜单中选择一个项目时,移动菜单切换为关闭状态。 I am not using your code, but borrowed a navbar from the Bootstrap examples and added a little extra code to just close the menu when you click on a link in the menu. 我没有使用您的代码,而是从Bootstrap示例中借用了导航栏,并添加了一些额外的代码来在单击菜单中的链接时关闭菜单。 I would not be surprised if there is something like that built into bootstrap, which I assume you are using ? 如果引导程序中内置了类似的东西,我认为您正在使用,我不会感到惊讶。 If not, you can probably do something similar by triggering a click on the hamburger when "appropriate": 如果没有,您可以通过在“适当”时触发对汉堡包的点击来执行类似的操作:

 $('.nav-link:not(.dropdown-toggle), .dropdown-item').each(function () { $(this).on("click", function(e) { $(".navbar-toggler").trigger("click"); alert($(this).attr("href")); }); }); 
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha256-YLGeXaapI0/5IgZopewRJcFXomhRMlYYjugPLSyNjTY=" crossorigin="anonymous" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/all.min.css" integrity="sha256-UzFD2WYH2U1dQpKDjjZK72VtPeWP50NoJjd26rnAdUI=" crossorigin="anonymous" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha256-CjSoeELFOcH0/uxWu6mC/Vlrc1AARqbm/jiiImDGV3s=" crossorigin="anonymous"></script> <nav class="navbar navbar-expand-lg navbar-light bg-light"> <a class="navbar-brand" href="#">Navbar</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav mr-auto"> <li class="nav-item active"> <a class="nav-link" href="#home">Home <span class="sr-only">(current)</span></a> </li> <li class="nav-item"> <a class="nav-link" href="#link">Link</a> </li> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Dropdown </a> <div class="dropdown-menu" aria-labelledby="navbarDropdown"> <a class="dropdown-item" href="#action">Action</a> <a class="dropdown-item" href="#another">Another action</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="#soemthingelse">Something else here</a> </div> </li> <li class="nav-item"> <a class="nav-link disabled" href="#">Disabled</a> </li> </ul> <form class="form-inline my-2 my-lg-0"> <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search"> <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button> </form> </div> </nav> <div class="col"> 2 of 3 </div> <div class="col"> 3 of 3 </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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