經典的水平導航列下拉子選單,結合 CSS `:hover` 與 JavaScript 修正 IE6 不支援非 `` 元素 hover 的歷史方案。本文提供完整實作,包含多層級子選單、當前頁面高亮、SFHover 腳本修正舊瀏覽器相容性。
<div id="menu">
<ul>
<li><a id="current" href="#">首頁</a></li>
<li>
<a href="#">網頁版式</a>
<ul>
<li><a href="#">自適應寬度</a></li>
<li><a href="#">固定寬度</a></li>
</ul>
</li>
<li>
<a href="#">Web教程</a>
<ul>
<li><a href="#">新手入門</a></li>
<li><a href="#">視頻教程</a></li>
<li><a href="#">常見問題</a></li>
</ul>
</li>
<li><a href="#">Web實例</a></li>
<li><a href="#">常用代碼</a></li>
</ul>
</div>
<style>
#menu { width: 500px; height: 28px; margin: 0 auto; border-bottom: 3px solid #E10001; }
#menu ul { list-style: none; margin: 0; padding: 0; }
#menu ul li { float: left; margin-left: 2px; }
#menu ul li a {
display: block; width: 87px; height: 28px; line-height: 28px;
text-align: center; font-size: 14px;
}
#menu ul li a#current { font-weight: bold; color: #fff; } /* 當前頁高亮 */
/* 子選單:預設隱藏,絕對定位 */
#menu ul li ul {
border: 1px solid #ccc;
display: none;
position: absolute;
}
#menu ul li ul li {
float: none; width: 87px; background: #eee; margin: 0;
}
#menu ul li ul li a { background: none; }
#menu ul li ul li a:hover { background: #333; color: #fff; }
/* 核心:CSS hover 顯示子選單 */
#menu ul li:hover ul { display: block; }
/* IE6 修正類名 */
#menu ul li.sfhover ul { display: block; }
</style>
IE6 僅支援 `` 元素的 `:hover`,`
<script>
// 0 ? " " : "") + "sfhover";
};
sfEls[i].onmousedown = function() { // 修正點擊也觸發
this.className += (this.className.length > 0 ? " " : "") + "sfhover";
};
sfEls[i].onmouseup = function() {
this.className += (this.className.length > 0 ? " ") + "sfhover";
};
sfEls[i].onmouseout = function() {
// 移除 sfhover 類名(正則處理前導空格)
this.className = this.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
};
}
}
window.onload = menuFix;
//]]>
</script>
| 技術點 | 說明 |
|---|---|
| CSS 唯純 hover | 現代瀏覽器無需 JS,` |
| SFHover 腳本 | 專為 IE6 設計,給 ` |
| 絕對定位子選單 | `position:absolute` 讓子選單脫離文檔流,不影響佈局 |
| 浮動主選單項 | `float:left` 水平排列,`margin-left` 控制間距 |
| 當前頁高亮 | `a#current` 樣式區分,伺服端渲染時動態加上 id |
現代瀏覽器(IE10+)完全支援 `
<style>
.nav { display: flex; background: #333; }
.nav li { position: relative; }
.nav > li > a { display: block; padding: 0 20px; line-height: 40px; color: #fff; }
.nav li ul {
position: absolute; top: 100%; left: 0; min-width: 180px;
background: #444; display: none;
}
.nav li:hover > ul { display: block; } /* 核心 */
.nav li ul li { border-top: 1px solid #555; }
.nav li ul li a { padding: 0 15px; line-height: 36px; }
.nav li ul li a:hover { background: #555; }
</style>
class DropdownNav {
constructor(root) {
this.root = root;
this.bindEvents();
}
bindEvents() {
// 滑鼠進入/離開延遲(防抖)
let timer;
this.root.querySelectorAll('li').forEach(li => {
li.addEventListener('mouseenter', () => {
clearTimeout(timer);
li.classList.add('open');
});
li.addEventListener('mouseleave', () => {
timer = setTimeout(() => li.classList.remove('open'), 150);
});
});
// 鍵盤支援
this.root.addEventListener('keydown', e => {
const items = [...this.root.querySelectorAll('a')];
const idx = items.indexOf(document.activeElement);
if (e.key === 'ArrowRight' && idx > -1) {
e.preventDefault();
items[idx].parentElement.classList.add('open');
items[idx].parentElement.querySelector('ul a')?.focus();
}
if (e.key === 'ArrowLeft') {
e.preventDefault();
document.activeElement.closest('li')?.classList.remove('open');
document.activeElement.closest('li')?.parentElement.closest('li')?.querySelector('a')?.focus();
}
if (e.key === 'Escape') {
this.root.querySelectorAll('.open').forEach(el => el.classList.remove('open'));
}
});
// 觸控:首次點擊展開,二次點擊導航
this.root.addEventListener('click', e => {
const link = e.target.closest('a');
const li = link?.parentElement;
if (li && li.querySelector('ul')) {
const isOpen = li.classList.toggle('open');
if (!isOpen && link.href !== '#') return; // 二次點擊允許跳轉
e.preventDefault();
}
});
}
}
document.addEventListener('DOMContentLoaded', () => {
new DropdownNav(document.getElementById('menu'));
});
CSS 只需加一層:
.nav li ul li ul {
top: 0; left: 100%; /* 右側展開 */
margin-left: -1px; /* 邊框重疊 */
}
.nav li ul li:hover > ul { display: block; }