菜单打开后关闭时页面冻结问题及现有JS下的关闭实现咨询
菜单关闭功能修复方案
问题说明
开发的菜单点击关闭按钮(X)后页面冻结,删除原有JS函数后菜单打开不再卡顿,但失去关闭功能。
问题原因
现有JS仅为汉堡图标(#hamburger-icon)绑定了菜单切换事件,关闭按钮(#closebtn)未绑定任何点击逻辑;同时关闭按钮是<a>标签但未设置有效href,点击时触发浏览器默认锚点行为,导致页面出现冻结假象。
修复代码
修改后的JavaScript
$(document).ready(function() { const hamburger = $('#hamburger-icon'); const closeBtn = $('#closebtn'); const nav = $('.canvas'); // 汉堡按钮点击事件 hamburger.click(function(e) { e.preventDefault(); nav.toggleClass('shown'); hamburger.toggleClass('active'); }); // 关闭按钮点击事件 closeBtn.click(function(e) { e.preventDefault(); nav.removeClass('shown'); hamburger.removeClass('active'); }); });
原有CSS(无需修改)
body, html { overflow: hidden; } body { font-family: 'Montserrat', sans-serif; height: 100%; margin: 0; font-size: 1rem; line-height: 1.5; letter-spacing: 0.0625em; background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/6/62/Paracas_National_Reserve%2C_Ica%2C_Peru-3April2011.jpg/1600px-Paracas_National_Reserve%2C_Ica%2C_Peru-3April2011.jpg"); } .menuaperto .card { margin: 1rem; } .menuaperto { margin-top: 20rem; } @media (min-width:800px) { .menuaperto { margin-top: 28rem; } } .minemenu { padding: 1rem; color: whitesmoke; } .separatore { padding-left: 5px; padding-right: 5px; } .bi-bag { margin-right: 12px; font-size: 20px; } .bi-heart { font-size: 20px; } .bi-person { font-size: 20px; } .bi-list { margin-right: 12px; font-size: 25px; } .bi-x { margin-right: 14px; font-size: 25px; } .minemenu a { text-decoration: none; color: whitesmoke; font-size: 18px; } #menumio { background-color: transparent; } .menu { font-family: 'Cormorant Garamond', serif; color: #c0a680; font-size: 23px; } .shop { font-family: 'Cormorant Garamond', serif; color: #c0a680; font-size: 20px; } @media(max-width:550px) { .nascondi { display: none!important; } .minemenu a { font-size: 16px; line-height: 3px; } } .canvas { padding: 1rem; color: whitesmoke; left: 0; width: 100%; height: 100%; display: block; background: #303030; position: fixed; opacity: 0; max-height: 0; overflow: hidden; transition: all 0.2s; z-index: 10; text-align: center; } .canvas.shown { top: 0; opacity: 1; max-height: none; z-index: 10; } .canvas a { text-decoration: none; color: whitesmoke; font-size: 18px; } .canvas .separatore { padding-left: 5px; padding-right: 5px; } @media(max-width:550px) { .canvas a { font-size: 16px; line-height: 3px; } .bi-x { margin-right: 8px; margin-left: -15px; } }
原有HTML(无需修改)
<!--BOOTSTRAP--> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"> <link href="https://getbootstrap.com/docs/5.2/assets/css/docs.css" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.2/font/bootstrap-icons.css"> <!--MYSTILE--> <link href="style.css" rel="stylesheet" type="text/css"> <!--GOOGLE FONT--> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=La+Belle+Aurore&family=Montserrat:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet"> <!--APERTURA MENU'--> <nav class="canvas"> <div class="container"> <div class="row"> <div class="col-4 d-flex"> <a id="closebtn"><i class="bi bi-x"></i></a><span class="menu"><i>Menù</i></span> </div> <div class="col-4"> <a href="#"> <font style="font-weight: 600;">Brand</font> Name</a> </div> <div class="col-4 d-flex justify-content-end"> <i class="bi bi-bag"></i><span class="shop"><i>Shop</i></span> <span class="separatore nascondi">|</span> <i class="bi bi-person mx-1 nascondi"></i> <span class="separatore nascondi">|</span> <i class="bi bi-heart mx-1 nascondi"></i> </div> </div> </div> </nav> <!--CHIUSURA MENU'--> <nav id="menumio"> <div class="container text-center minemenu"> <div class="row"> <div class="col-4 d-flex"> <a id="hamburger-icon" href="#"><i class="bi bi-list"></i></a><span class="menu"><i>Menù</i></span> </div> <div class="col-4"> <a href="#"> <font style="font-weight: 600;">Brand</font> Name</a> </div> <div class="col-4 d-flex justify-content-end"> <i class="bi bi-bag"></i><span class="shop"><i>Shop</i></span> <span class="separatore nascondi">|</span> <i class="bi bi-person mx-1 nascondi"></i> <span class="separatore nascondi">|</span> <i class="bi bi-heart mx-1 nascondi"></i> </div> </div> </div> </nav> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.4.1/jquery.easing.min.js"></script>
修复说明
- 为关闭按钮(
#closebtn)绑定点击事件,触发菜单隐藏逻辑:移除.shown类,同时移除汉堡按钮的.active类 - 使用
e.preventDefault()替代return false,精准阻止<a>标签的默认跳转行为,避免页面冻结假象 - 缓存DOM元素选择器,提升代码执行效率
内容的提问来源于stack exchange,提问作者Elia d'Albenzio
相关产品推荐
相关产品推荐

