HTML/CSS/JS移动端横向滚动菜单子菜单被遮挡jQuery方案咨询
问题根源
子菜单被遮挡和层级无关,核心是你给.exfd-filter设置overflow-x: auto时,浏览器默认会将overflow-y同步设为auto,导致父容器垂直方向溢出的子菜单被强制剪裁,即便给子菜单设置再高的z-index也无法突破父容器的溢出限制。
修复方案
方案1:CSS原生修复(优先使用)
调整你的媒体查询代码,明确指定垂直溢出规则,同时固定父容器高度避免触发垂直滚动:
@media screen and (max-width: 768px) { .exfd-filter { overflow-x: auto !important; overflow-y: visible !important; white-space: nowrap !important; height: 48px; /* 替换为你的菜单实际高度,确保不会触发垂直滚动 */ } }
如果存在浏览器兼容问题导致上述方案无效,再使用jQuery方案。
方案2:jQuery动态渲染方案
实现逻辑为展开子菜单时,将子菜单临时移至body根节点渲染,脱离父容器的overflow限制,收起时再放回原节点。
$(function() { // 存储当前激活的子菜单和其原始父节点 let activeMenu = null; let originParent = null; // 替换为你实际的父菜单选择器 $('.filter-parent-item').on('click', function(e) { e.stopPropagation(); const $parent = $(this); // 替换为你实际的子菜单选择器 const $child = $parent.find('.filter-child-dropdown'); // 关闭已展开的菜单 if (activeMenu) { activeMenu.appendTo(originParent).hide(); } // 切换当前菜单状态 if ($child.is(':visible')) { $child.hide(); activeMenu = null; return; } // 记录原始位置 originParent = $child.parent(); activeMenu = $child; // 计算父菜单位置,定位子菜单 const parentOffset = $parent.offset(); const parentHeight = $parent.outerHeight(); $child.appendTo('body').css({ position: 'absolute', top: parentOffset.top + parentHeight + 'px', left: parentOffset.left + 'px', zIndex: 9999, display: 'block' }); }); // 点击页面空白处收起菜单 $(document).on('click', function() { if (activeMenu) { activeMenu.appendTo(originParent).hide(); activeMenu = null; } }); // 窗口尺寸变化或滚动时修正菜单位置 $(window).on('resize scroll', function() { if (!activeMenu) return; // 替换为你实际的父菜单选择器 const $activeParent = $('.filter-parent-item').has(originParent); const parentOffset = $activeParent.offset(); const parentHeight = $activeParent.outerHeight(); activeMenu.css({ top: parentOffset.top + parentHeight + 'px', left: parentOffset.left + 'px' }); }); });
使用注意
- 代码中的
.filter-parent-item和.filter-child-dropdown需要替换为你项目中对应的父菜单、子菜单类名 - 提前给子菜单设置固定宽度,避免移动到body节点后宽度适配异常
- 若菜单有自定义动画,将代码中的
show/hide替换为对应的动画方法即可
内容的提问来源于stack exchange,提问作者Mehuol Dhanji
相关产品推荐
相关产品推荐

