导航栏在移动端视图自动展开问题求助
解决移动端导航栏自动展开的问题
问题根源
你的代码里,移动端视图下#nav_bar未设置初始高度为0%,默认会根据内容撑开高度,导致页面加载或窗口调整到移动端尺寸时导航栏自动展开;同时移动端下按钮的初始显示状态也不符合预期。
修复步骤
- 设置导航栏初始隐藏状态:在移动端媒体查询中给
#nav_bar添加height: 0%,确保默认隐藏 - 修正按钮初始显示状态:移动端下默认显示
open-btn,隐藏close-btn - 同步aria-expanded属性(可选但推荐):在
openNav和closeNav函数里更新按钮的aria-expanded值,提升页面可访问性
修改后的完整代码
JavaScript
function closeNav() { const navBar = document.getElementById("nav_bar"); const openBtn = document.getElementById("open-btn"); const closeBtn = document.getElementById("close-btn"); navBar.style.height = "0%"; openBtn.style.display = "inline-block"; closeBtn.style.display = "none"; // 更新aria-expanded状态 openBtn.setAttribute("aria-expanded", "false"); closeBtn.querySelector("i").setAttribute("aria-expanded", "false"); } function openNav() { const navBar = document.getElementById("nav_bar"); const openBtn = document.getElementById("open-btn"); const closeBtn = document.getElementById("close-btn"); navBar.style.height = "100%"; openBtn.style.display = "none"; closeBtn.style.display = "inline-block"; // 更新aria-expanded状态 openBtn.setAttribute("aria-expanded", "true"); closeBtn.querySelector("i").setAttribute("aria-expanded", "true"); }
CSS
body { background: url(images/bg-img-01.jpg) no-repeat; background-size: cover; } *, *::after, *::before { margin: 0; padding: 0; box-sizing: border-box; } #nav_bar { background: radial-gradient( ellipse at top, rgba(196, 199, 200, 0.8), rgba(250, 255, 255, 0.02) 60%, rgba(0, 0, 0, 0) 1%); position: fixed; top: 0; width: 100%; } #nav_bar>img { display: none; } .nav { background: none; overflow: hidden; display: flex; margin-inline: auto; flex-direction: row; justify-content: space-around; align-items: center; list-style: none; width: 65%; left: 20%; padding: 1.4em; } .list-item { text-decoration: none; color: #CBD5DF; font-weight: bolder; } .list-item { position: relative; } .list-item::before { position: absolute; content: ""; background-color: #535792; height: 4px; width: 0%; top: 25px; transition: all .3s ease-in; } .list-item:hover::before { width: 100%; } .list-item:hover { color: #C4C7C8; } #close-btn, #open-btn { display: none; } @media only screen and (max-width:768px) { #nav_bar>img { display: block; position: absolute; width: 10em; left: 20%; top: 10%; } #nav_bar { width: 100%; position: fixed; z-index: 1; top: 0; overflow: hidden; transition: 0.5s; height: 0%; /* 添加这行,默认隐藏导航栏 */ } .nav { position: relative; display: flex; flex-direction: column; gap: 1.2rem; top: 20%; width: 100%; text-align: center; margin-top: 30px; } .list-item { text-decoration: none; display: block; color: #ffdada; font-size: 1.5rem; transition: 0.3s; } #close-btn { display: none; /* 移动端默认隐藏关闭按钮 */ position: absolute; right: 25px; top: 20px; font-size: 2rem; color: #818181; transition: all 0.3s; } #open-btn { display: block; /* 移动端默认显示打开按钮 */ position: absolute; right: 25px; top: 20px; font-size: 2rem; color: #818181; transition: all 0.3s; } #close-btn:hover { color: #fff; } }
HTML
<body> <div id="nav_bar"> <a href="#" id="close-btn"> <i aria-expanded="false" onclick="closeNav()" class="bi bi-x-lg"></i> </a> <img src="assests/images/moon.png" alt="" /> <div class="nav"> <a class="list-item" href="#">Home</a> <a class="list-item" href="#">About Me</a> <a class="list-item" href="#">Projects</a> <a class="list-item" href="#">C.V</a> <a class="list-item" href="#">Contact</a> </div> </div> <a aria-expanded="false" href="#" id="open-btn" onclick="openNav()"><i class="bi bi-list"></i></a> <script src="assests/nav.js"></script> </body> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
内容的提问来源于stack exchange,提问作者Usama Khan
相关产品推荐
相关产品推荐

