
在现代web应用中,下拉菜单(dropdown menu)是一种常见的ui组件,用于提供导航、筛选或设置选项。当页面上存在多个下拉菜单时,如何有效地管理它们的开启与关闭状态成为一个关键问题。理想的用户体验是:
本教程将通过 jQuery 演示如何优雅地实现这些功能,确保多个下拉菜单的独立性与协同工作。
实现上述功能主要依赖于以下两个核心机制:
首先,我们需要一个清晰的HTML结构来表示下拉菜单。每个下拉菜单都应包含一个触发按钮和一个内容区域,并用一个共同的父容器包裹。这里我们使用 .tm-dropdown 作为父容器,.tm-dropdown-button 作为触发按钮,.tm-dropdown-content 作为下拉内容。
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
<div class="tm-dropdown">
<button type="button" class="tm-button tm-button-primary-l6 tm-button-ghost text-grey-medium tm-dropdown-button" aria-describedby="tooltip">
<span>EN</span>
</button>
<ul class="tm-dropdown-content" role="tooltip">
<li>English</li>
<li>Bahasa Melayu</li>
<li>中文简体</li>
</ul>
</div>
<div class="tm-dropdown">
<button type="button" class="tm-button tm-button-primary-l6 tm-button-ghost text-grey-medium tm-dropdown-button" aria-describedby="tooltip">
<span>EN</span>
</button>
<ul class="tm-dropdown-content" role="tooltip">
<li>English</li>
<li>Bahasa Melayu</li>
<li>中文简体</li>
</ul>
</div>结构说明:
为了控制下拉菜单的显示与隐藏,我们需要一些基本的CSS样式。当 .tm-dropdown 元素具有 opened 类时,其子元素 .tm-dropdown-content 应该显示。
.tm-dropdown-content {
display: none; /* 默认隐藏 */
list-style: none; /* 移除列表默认样式 */
padding: 0;
margin: 0;
position: absolute; /* 根据需要调整定位 */
background-color: #fff; /* 示例背景色 */
border: 1px solid #ddd; /* 示例边框 */
box-shadow: 0 2px 5px rgba(0,0,0,0.1); /* 示例阴影 */
z-index: 1000; /* 确保下拉菜单在其他内容之上 */
}
.tm-dropdown.opened .tm-dropdown-content {
display: block; /* 当父元素有 'opened' 类时显示 */
}
/* 示例:为按钮和列表项添加一些基础样式 */
.tm-dropdown {
position: relative; /* 使下拉内容能够相对定位 */
display: inline-block; /* 确保多个下拉菜单并排显示 */
margin-right: 10px;
}
.tm-dropdown-button {
padding: 8px 15px;
border: 1px solid #ccc;
background-color: #f0f0f0;
cursor: pointer;
border-radius: 4px;
}
.tm-dropdown-content li {
padding: 8px 15px;
cursor: pointer;
}
.tm-dropdown-content li:hover {
background-color: #f5f5f5;
}样式说明:
现在是实现核心交互逻辑的部分。我们将使用 jQuery 来处理事件。
$(document).on('click touchstart', function(e) {
// 1. 全局点击处理:点击下拉菜单外部区域时关闭所有已打开的下拉菜单
// 检查点击事件的目标元素或其任何祖先元素是否是 '.tm-dropdown'
// 如果不是,则表示点击发生在任何下拉菜单之外
if ($(e.target).closest('.tm-dropdown').length === 0) {
$('.tm-dropdown').removeClass('opened'); // 移除所有下拉菜单的 'opened' 类
}
});
$(".tm-dropdown-button").on('click', function(e) {
// 2. 下拉按钮点击处理:切换当前下拉菜单,并关闭其他已打开的下拉菜单
e.stopPropagation(); // 阻止事件冒泡,防止触发 document 上的全局点击处理
var $this = $(this); // 获取当前被点击的按钮
var parent = $this.parent(); // 获取当前按钮的父级 '.tm-dropdown' 元素
// 关闭所有其他已打开的下拉菜单
// 查找所有 '.tm-dropdown' 元素,并排除当前点击的父元素,然后移除它们的 'opened' 类
$('.tm-dropdown').not(parent).removeClass('opened');
// 切换当前点击的下拉菜单的状态
// 如果当前下拉菜单已打开,则关闭;如果已关闭,则打开
parent.toggleClass('opened');
});代码解析:
$(document).on('click touchstart', function(e) { ... });
$(".tm-dropdown-button").on('click', function(e) { ... });
将上述HTML、CSS和JavaScript整合在一起,你将得到一个功能完善的多下拉菜单系统。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery 智能下拉菜单</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
background-color: #f4f4f4;
}
.tm-dropdown {
position: relative; /* 使下拉内容能够相对定位 */
display: inline-block; /* 确保多个下拉菜单并排显示 */
margin-right: 15px;
vertical-align: top; /* 保持对齐 */
}
.tm-dropdown-button {
padding: 10px 20px;
border: 1px solid #ccc;
background-color: #fff;
cursor: pointer;
border-radius: 5px;
font-size: 16px;
color: #333;
transition: all 0.2s ease;
}
.tm-dropdown-button:hover {
background-color: #e9e9e9;
border-color: #bbb;
}
.tm-dropdown-content {
display: none; /* 默认隐藏 */
position: absolute;
top: 100%; /* 定位在按钮下方 */
left: 0;
background-color: #fff;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1000;
list-style: none;
padding: 0;
margin: 5px 0 0 0; /* 与按钮有一些间距 */
border-radius: 5px;
overflow: hidden; /* 确保圆角效果 */
}
.tm-dropdown.opened .tm-dropdown-content {
display: block; /* 当父元素有 'opened' 类时显示 */
}
.tm-dropdown-content li {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
cursor: pointer;
transition: background-color 0.2s ease;
}
.tm-dropdown-content li:hover {
background-color: #f1f1f1;
}
</style>
</head>
<body>
<h1>智能下拉菜单示例</h1>
<p>点击下拉菜单按钮打开/关闭,点击其他下拉菜单按钮会关闭当前打开的并打开新的,点击菜单外部区域会关闭所有菜单。</p>
<div class="tm-dropdown">
<button type="button" class="tm-button tm-button-primary-l6 tm-button-ghost text-grey-medium tm-dropdown-button" aria-describedby="tooltip1">
<span>语言</span>
</button>
<ul class="tm-dropdown-content" role="tooltip" id="tooltip1">
<li>English</li>
<li>Bahasa Melayu</li>
<li>中文简体</li>
</ul>
</div>
<div class="tm-dropdown">
<button type="button" class="tm-button tm-button-primary-l6 tm-button-ghost text-grey-medium tm-dropdown-button" aria-describedby="tooltip2">
<span>设置</span>
</button>
<ul class="tm-dropdown-content" role="tooltip" id="tooltip2">
<li>个人资料</li>
<li>账户安全</li>
<li>隐私设置</li>
</ul>
</div>
<div class="tm-dropdown">
<button type="button" class="tm-button tm-button-primary-l6 tm-button-ghost text-grey-medium tm-dropdown-button" aria-describedby="tooltip3">
<span>帮助</span>
</button>
<ul class="tm-dropdown-content" role="tooltip" id="tooltip3">
<li>常见问题</li>
<li>联系我们</li>
<li>用户手册</li>
</ul>
</div>
<script>
$(document).on('click touchstart', function(e) {
if ($(e.target).closest('.tm-dropdown').length === 0) {
$('.tm-dropdown').removeClass('opened');
}
});
$(".tm-dropdown-button").on('click', function(e) {
e.stopPropagation();
var $this = $(this);
var parent = $this.parent();
$('.tm-dropdown').not(parent).removeClass('opened');
parent.toggleClass('opened');
});
</script>
</body>
</html>.tm-dropdown-content {
/* ... 其他样式 ... */
opacity: 0;
visibility: hidden;
transition: opacity 0.3s ease, visibility 0.3s ease;
}
.tm-dropdown.opened .tm-dropdown-content {
opacity: 1;
visibility: visible;
}通过本教程,我们学习了如何使用 jQuery 结合事件委托和阻止事件冒泡机制,构建一个功能强大且用户体验良好的多下拉菜单系统。该方案能够确保:
这种模式在Web开发中非常常见且实用,掌握它可以帮助开发者创建更具交互性和专业性的用户界面。
以上就是jQuery 实现智能下拉菜单:全局关闭与独立切换机制的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号