如何用JavaScript修复Bootstrap 5导航栏下拉菜单响应式交互问题
解决方案
针对你遇到的Bootstrap 5导航栏下拉菜单移动端无法收起的问题,结合你期望的响应式交互需求,以下是完整的修复方案:
核心问题分析
- 现有代码未正确绑定Bootstrap Dropdown的JS交互,导致移动端点击后无法切换收起状态
- 移动端CSS中保留了hover展开逻辑,与点击交互冲突
- 原JS仅处理了桌面端href的切换,未兼顾移动端的阻止跳转与状态控制
修复步骤与完整代码
1. 调整HTML结构
给Services的下拉触发器添加data-bs-toggle="dropdown"属性,同时保留原ID以便JS控制:
<li class="nav-item dropdown" id="dropdown"> <a class="nav-link text-white nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false" id="nav_service_object"> Services </a> <!-- 下拉菜单内容不变 --> </li>
2. 替换JavaScript逻辑
移除原有JS,添加以下代码,实现跨设备的交互逻辑:
document.addEventListener('DOMContentLoaded', function() { const serviceLink = document.getElementById('nav_service_object'); const dropdownElement = document.getElementById('dropdown'); const dropdown = new bootstrap.Dropdown(dropdownElement); const isMobile = window.matchMedia('(max-width: 991px)').matches; // 对应Bootstrap navbar-expand-lg的断点 // 桌面端逻辑:hover展开/收起,点击跳转 if (!isMobile) { serviceLink.addEventListener('mouseenter', function() { dropdown.show(); this.href = '/Bootstrap-/services.html'; }); serviceLink.addEventListener('mouseleave', function() { dropdown.hide(); this.href = '#'; }); serviceLink.addEventListener('click', function(e) { if (this.href !== '#') { return true; } }); } else { // 移动端逻辑:点击切换展开/收起,阻止跳转 serviceLink.addEventListener('click', function(e) { e.preventDefault(); // 阻止默认跳转 // 切换下拉状态 if (dropdown._isShown) { dropdown.hide(); } else { dropdown.show(); } }); } // 监听窗口大小变化,动态调整逻辑 window.addEventListener('resize', function() { location.reload(); // 窗口切换后刷新确保逻辑生效 }); });
3. 修正CSS代码
移除移动端的hover展开逻辑,避免冲突:
#navbar-color { background-color: hsl(210, 45%, 30%); } .navbar-light .navbar-toggler-icon { background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'%3e%3cpath stroke='rgba%28255, 255, 255, 1%29' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/%3e%3c/svg%3e"); } .navbar-light .navbar-toggler { border: 1px solid hsl(0, 0%, 100%); outline: none; box-shadow: none; } #nav_object { position: relative; } /* 仅桌面端hover展开下拉 */ @media (min-width: 992px) { .dropdown:hover .dropdown-menu { display: block; background-color: hsl(210, 45%, 30%); } #nav_object::after { content: ''; opacity: 0; transition: all 0.2s; height: 2px; width: 100%; background: hsl(18, 100%, 62%); position: absolute; bottom: 0; left: 0; } #nav_object:hover::after { opacity: 1; } } .dropdown-item:hover { background-color: hsl(210, 55%, 41%); }
完整整合后的HTML代码
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Home | </title> <link rel="stylesheet" href="/_bridgetown/static/css/main.c7d4dd3f1984a290e9be.css" /> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> <link rel="stylesheet" href="../css/index.css" /> <link rel="stylesheet" data-purpose="Layout StyleSheet" title="Default" disabled href="/css/app-937c1ff7d52fd6f78dd9322599e2b5d4.css?vsn=d"> <link rel="stylesheet" data-purpose="Layout StyleSheet" title="Web Awesome" href="/css/app-wa-8d95b745961f6b33ab3aa1b98a45291a.css?vsn=d"> <link rel="stylesheet" href="https://site-assets.fontawesome.com/releases/v6.4.0/css/all.css"> <link rel="stylesheet" href="https://site-assets.fontawesome.com/releases/v6.4.0/css/sharp-solid.css"> <link rel="stylesheet" href="https://site-assets.fontawesome.com/releases/v6.4.0/css/sharp-regular.css"> <link rel="stylesheet" href="https://site-assets.fontawesome.com/releases/v6.4.0/css/sharp-light.css"> <link rel="icon" type="image/x-icon" href="/Bootstrap-/images/favicon-32x32.png"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <!-- NAVIGATION BAR START --> <nav class="navbar navbar-expand-lg navbar-light py-3 sticky-top" id="navbar-color"> <div class="container"> <a href="/Bootstrap-/index.html"> <img src="./src/img/logo-topnav.png" height="45" width="225" class="img-fluid"/> </a> <!-- TOGGLE MENU START --> <button type="button" class="navbar-toggler" data-bs-toggle="collapse" data-bs-target="#navmenu"> <span class="navbar-toggler-icon"></span> </button> <!-- TOGGLE MENU CLOSE --> <div class="collapse navbar-collapse" id="navmenu"> <ul class="navbar-nav ms-auto"> <li class="nav-item"><a href="/Bootstrap-/index.html" class="nav-link text-white" id="nav_home_object">Home</a></li> <li class="nav-item"><a href="/Bootstrap-/about.html" class="nav-link text-white" id="nav_about_object">About</a></li> <li class="nav-item dropdown" id="dropdown"> <a class="nav-link text-white nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false" id="nav_service_object"> Services </a> <ul class="dropdown-menu"> <li><a class="dropdown-item text-capitalize text-white" href="/Bootstrap-/cctv.html">CCTV Services</a></li> <li><a class="dropdown-item text-white text-capitalize" href="/Bootstrap-/datacenter.html">Datacenter Support</a></li> <li><a class="dropdown-item text-white text-capitalize" href="/Bootstrap-/digital-signage.html">Digital Signage</a></li> <li><a class="dropdown-item text-white text-capitalize" href="/Bootstrap-/gen-support.html">General Support</a></li> <li><a class="dropdown-item text-white text-capitalize" href="/Bootstrap-/net-support.html">Network Support</a></li> <li><a class="dropdown-item text-white text-capitalize" href="/Bootstrap-/pos.html">POS Services</a></li> <li><a class="dropdown-item text-white text-capitalize" href="/Bootstrap-/telecom.html">Telecom Services</a></li> </ul> </li> <li class="nav-item"><a href="/Bootstrap-/contact.html" class="nav-link text-white" id="nav_contact_object">Contact</a></li> </ul> </div> </div> </nav> <!-- NAVIGATION BAR CLOSE --> <script> document.addEventListener('DOMContentLoaded', function() { const serviceLink = document.getElementById('nav_service_object'); const dropdownElement = document.getElementById('dropdown'); const dropdown = new bootstrap.Dropdown(dropdownElement); const isMobile = window.matchMedia('(max-width: 991px)').matches; if (!isMobile) { serviceLink.addEventListener('mouseenter', function() { dropdown.show(); this.href = '/Bootstrap-/services.html'; }); serviceLink.addEventListener('mouseleave', function() { dropdown.hide(); this.href = '#'; }); serviceLink.addEventListener('click', function(e) { if (this.href !== '#') { return true; } }); } else { serviceLink.addEventListener('click', function(e) { e.preventDefault(); if (dropdown._isShown) { dropdown.hide(); } else { dropdown.show(); } }); } window.addEventListener('resize', function() { location.reload(); }); }); </script> </body> </html>
关键说明
- 使用Bootstrap官方Dropdown JS API控制展开收起状态,确保移动端交互正常
- 以
991px为断点(对应navbar-expand-lg的展开阈值),区分桌面/移动端逻辑 - 桌面端hover时自动展开下拉并设置跳转链接,离开时重置;点击时正常跳转
- 移动端点击时阻止跳转,仅切换下拉菜单的展开/收起状态
- 窗口大小变化时刷新页面,确保逻辑适配当前设备尺寸
内容的提问来源于stack exchange,提问作者PFL
相关产品推荐
相关产品推荐

