单击scrollspy链接时折叠引导汉堡菜单

aurhwmvo  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(340)

当你点击scrollspy链接时,你怎么能折叠自助汉堡的菜单?

如果在智能手机上查看我的网站时,引导导航被简化为汉堡菜单,并且您单击指向my index.html中特定位置的链接(如#aboutme),而不是指向外部.html页面,则导航将保持打开状态。我希望以后它会再次崩溃。
您可以在以下位置查看代码:https://github.com/senkvalentin/personal_page
或浏览以下网址:https://senkvalentin.com/
以下是我的html的一部分:

<div data-spy="scroll" data-target=".navbar" data-offset="50">
  <div class="container">
      <div class="row"><nav class="navbar fixed-top navbar navbar-expand-lg navbar-light bg-light">
        <div class="container-fluid">
          <a class="navbar-brand" href="/"><img src="./img/logo.png" alt="">
            </a>
          <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-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 me-auto mb-2 mb-lg-0">
              <li class="nav-item">
                <a class="nav-link" aria-current="page" href="/">Home</a>
              </li>
              <li class="nav-item">
                <a class="nav-link"  href="#aboutMe">About</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" aria-current="page" href="#projectsList">Projects</a>
              </li>
              <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                  Admin
                </a>
                <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
                  <li><a class="dropdown-item" href="!#" target="_blank">Server - Info</a></li>
                  <li><a class="dropdown-item" href="#" target="_blank">Mail - Server</a></li>
                  <li><a class="dropdown-item" href="#" target="_blank">Mail - Server</a></li>
                  <li><a class="dropdown-item" href="!#" target="_blank">Cloud - Server</a></li>
                </ul>
              </li>
            </ul>
          </div>
        </div>
      </nav></div>

我已经看过了:如何在单击时隐藏可折叠的引导导航栏
并且:当您单击其中的链接时,引导面板塌陷
但什么都没用
即时通讯使用:

<script src="https://code.jquery.com/jquery-latest.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>

提前谢谢

mu0hgdu0

mu0hgdu01#

在jquery中,只要在单击链接时触发单击导航栏切换按钮即可

$('.nav-link').click(function(){
  $('.navbar-toggler').click();
});
$('.nav-link').click(function(){
  $('.navbar-toggler').click();
});
.h-100vh{
  height:100vh;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" />
<div data-spy="scroll" data-target=".navbar" data-offset="50">
  <div class="container">
    <div class="row">
      <nav class="navbar fixed-top navbar navbar-expand-lg navbar-light bg-light">
        <div class="container-fluid">
          <a class="navbar-brand" href="/"><img src="./img/logo.png" alt="">
          </a>
          <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-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 me-auto mb-2 mb-lg-0">
              <li class="nav-item">
                <a class="nav-link" aria-current="page" href="#home">Home</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#aboutMe">About</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" aria-current="page" href="#projectsList">Projects</a>
              </li>
              <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                      Admin
                    </a>
                <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
                  <li><a class="dropdown-item" href="!#" target="_blank">Server - Info</a></li>
                  <li><a class="dropdown-item" href="#" target="_blank">Mail - Server</a></li>
                  <li><a class="dropdown-item" href="#" target="_blank">Mail - Server</a></li>
                  <li><a class="dropdown-item" href="!#" target="_blank">Cloud - Server</a></li>
                </ul>
              </li>
            </ul>
          </div>
        </div>
      </nav>
    </div>
    <div id="home" class="h-100vh bg-warning">Home</div> 
    <div id="aboutMe" class="h-100vh bg-success">About Me</div>
    <div id="projectsList" class="h-100vh bg-primary">Project</div>

相关问题