简体   繁体   English

单击移动设备中的项目时折叠导航栏

[英]Collapse nav bar when clicked on item in mobile

I have created a landing page for an event.我为一个活动创建了一个登陆页面。 Everything is working fine, but when the navbar is opened in mobile view, it doesn't collapse back when clicked on an item.一切正常,但是当导航栏在移动视图中打开时,单击某个项目时它不会折叠回来。 It stays open covering almost 3/4 of the screen while the page scrolls to the sections finely.当页面精细滚动到各个部分时,它保持打开状态,几乎覆盖了屏幕的 3/4。 I need the nav bar to collapse when an item is clicked.单击项目时,我需要导航栏折叠。

Here's the HTML for the navbar I've created这是我创建的导航栏的 HTML

    <div id="nav-bar" >
        <nav class="navbar navbar-expand-lg navbar-light" style="background:#2E935D;">
            <a href="#"><img src="assets/images/logonew.png" style="height: 3.35rem; padding-left: 20px;"/></a> <!-- logo on nav bar-->

            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <i class="fas fa-bars"></i>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav ml-auto">
                    <li class="nav-item"> <a class="nav-link" d onMouseOver="this.style.color='#E99D23'" onMouseOut="this.style.color='#fff'" href="#home">Home<span class="sr-only">(current)</span></a></li>
                    <li class="nav-item"> <a class="nav-link" onMouseOver="this.style.color='#E99D23'" onMouseOut="this.style.color='#fff'" href="#info">Info</a> </li>
                    <li class="nav-item"> <a class="nav-link" onMouseOver="this.style.color='#E99D23'" onMouseOut="this.style.color='#fff'" href="#schedule">Schedule</a> </li>
                    <li class="nav-item"> <a class="nav-link" onMouseOver="this.style.color='#E99D23'" onMouseOut="this.style.color='#fff'" href="#rules">Rules</a> </li>
                    <li class="nav-item"> <a class="nav-link" onMouseOver="this.style.color='#E99D23'" onMouseOut="this.style.color='#fff'" href="#about">About</a> </li>
                    <li class="nav-item"> <a class="nav-link" onMouseOver="this.style.color='#E99D23'" onMouseOut="this.style.color='#fff'" href="#register">Register</a> </li>
                    <li class="nav-item"> <a class="nav-link" onMouseOver="this.style.color='#E99D23'" onMouseOut="this.style.color='#fff'" href="#contact">Contact Us</a> </li>
                    <li class="nav-item"><a style="color: #fff;" onMouseOver="this.style.color='#E99D23'" onMouseOut="this.style.color='#fff'" href="https://examplelink.com" target="_blank"><i class="fab fa-facebook" style="font-size:30px;"></i></a></li>
                </ul>
            </div>
        </nav>
    </div>

Are there any mistakes or issues in the code?代码中是否有任何错误或问题? How can I achieve the desired behavior of the navbar?如何实现导航栏的所需行为? Thank you in advance.先感谢您。

This worked for me这对我有用

// Close Bootstrap 3 navbar when a nav item is clicked
$('.navbar-collapse ul li a:not(.dropdown-toggle)').bind('click touchstart', function () {
      setTimeout(function () {
         $('.navbar-toggle:visible').click();
      }, 100);
    });

found here .这里找到。 I also had to add a delay.我还不得不添加一个延迟。

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

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