
在网页交互中,菜单是常见的导航元素。为了提升用户体验,当用户点击某个菜单项时,通常需要将其高亮显示,同时取消其他菜单项的高亮状态。这不仅能清晰地指示当前选中的是哪个选项,还能提供即时视觉反馈。本教程将采用一种高效且易于维护的方法来实现这一功能。
实现菜单项的动态高亮,主要包含以下几个核心思想:
我们将从 HTML 结构、CSS 样式和 JavaScript 逻辑三个方面来构建这个功能。
首先,定义一个包含多个菜单项的 HTML 结构。每个菜单项都使用一个 p 标签,并带有相同的 menu 类,它们都包含在一个 container div 中。
<div class="container">
<p class="menu">Home</p>
<p class="menu">Gallery</p>
<p class="menu">Technology</p>
<p class="menu">Information</p>
<p class="menu">Contact</p>
<p class="menu">About</p>
</div>为了提供更好的视觉效果和用户体验,我们可以为菜单项设置默认样式和可选的悬停效果。默认情况下,所有菜单项背景色为白色。
立即学习“Java免费学习笔记(深入)”;
body { background-color: #efefef; font-family: sans-serif; }
.menu { background-color: white; cursor: pointer; padding: 8px 15px; margin: 5px 0; border-radius: 4px; transition: background-color 0.2s ease; }
.menu:hover { background-color: #e0e0e0; } /* 可选:鼠标悬停时的背景色 */
/* .menu.active { background-color: #beffc7; } */ /* 另一种通过CSS类控制高亮的方式 */注意:在上述 CSS 中,我们注释掉了一个 .menu.active 样式。虽然本教程的 JavaScript 部分将直接修改 style.backgroundColor,但在更复杂的应用中,通过添加/移除 CSS 类(如 .active)来控制样式是更推荐的做法,因为它能更好地分离结构、样式和行为。
JavaScript 是实现动态高亮的核心。我们将使用事件委托机制,监听父容器的点击事件,并根据点击目标来更新菜单项的样式。
// 获取菜单容器元素
let container = document.querySelector('.container');
// 用于存储当前被选中(高亮)的菜单项的引用
let currentActiveMenuItem = null;
// 为容器添加点击事件监听器
container.addEventListener("click", (event) => {
// 检查点击事件的实际目标是否是具有 'menu' 类的元素
// event.target 指向实际触发事件的元素
if (event.target.matches('.menu')) {
// 如果存在上一个被选中的菜单项,将其背景色恢复为白色
if (currentActiveMenuItem) {
currentActiveMenuItem.style.backgroundColor = '#fff';
}
// 如果当前点击的不是之前已经高亮的菜单项,则将其高亮
// 否则,如果点击的是同一个高亮项,则取消其高亮(使其变白)
if (event.target !== currentActiveMenuItem) {
event.target.style.backgroundColor = '#beffc7'; // 设置为绿色
// 更新 currentActiveMenuItem 为当前点击的元素
currentActiveMenuItem = event.target;
} else {
// 如果点击的是同一个高亮项,则取消高亮,并重置 currentActiveMenuItem
event.target.style.backgroundColor = '#fff';
currentActiveMenuItem = null;
}
}
});代码解析:
将上述 HTML、CSS 和 JavaScript 组合起来,形成一个完整的可运行示例。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>动态菜单高亮</title>
<style>
body { background-color: #efefef; font-family: sans-serif; display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; }
.container { background-color: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); }
.menu { background-color: white; cursor: pointer; padding: 8px 15px; margin: 5px 0; border-radius: 4px; transition: background-color 0.2s ease; }
.menu:hover { background-color: #e0e0e0; } /* 鼠标悬停时的背景色 */
</style>
</head>
<body>
<div class="container">
<p class="menu">Home</p>
<p class="menu">Gallery</p>
<p class="menu">Technology</p>
<p class="menu">Information</p>
<p class="menu">Contact</p>
<p class="menu">About</p>
</div>
<script>
let container = document.querySelector('.container');
let currentActiveMenuItem = null; // To hold reference to last 'clicked' menu item
container.addEventListener("click", (event) => {
// When a .menu inside .container gets clicked
if (event.target.matches('.menu')) {
// If 'currentActiveMenuItem' exists/defined, make it white
if (currentActiveMenuItem) {
currentActiveMenuItem.style.backgroundColor = '#fff';
}
// If 'clicked' is not the current active item, toggle to green
if (event.target !== currentActiveMenuItem) {
event.target.style.backgroundColor = '#beffc7'; // Set to green
currentActiveMenuItem = event.target; // Save reference to clicked element for next 'click'
} else {
// If clicked the same active item, deselect it
event.target.style.backgroundColor = '#fff';
currentActiveMenuItem = null;
}
}
});
</script>
</body>
</html>通过本教程,我们学习了如何利用 JavaScript 的事件委托机制和简单的状态管理,实现一个动态、高效且用户友好的菜单点击高亮功能。这种方法不仅代码简洁,易于理解和维护,而且在性能上也表现出色,是构建交互式网页菜单的理想选择。
以上就是JavaScript 动态菜单选中效果实现:点击高亮与取消高亮的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号