純 CSS 實現、無 JavaScript、支援 5 級巢狀、半透明背景、多瀏覽器兼容的下拉選單。
:hover 偽類控制子選單顯示/隱藏<table> 模擬懸停觸發left: -9999px) 隱藏非激活子選單#nav, #nav ul {
position: relative; z-index: 100;
background: #999; list-style: none;
margin: 0; padding: 0;
}
#nav { background: #528e34; width: 740px; height: 23px; }
#nav li { float: left; }
#nav li li { float: none; } /* 子選單垂直排列 */
#nav li a {
display: block; float: left;
padding: 0 10px 0 20px; line-height: 22px; height: 23px;
color: #fff; font-weight: bold; text-decoration: none;
border-left: 1px solid #528e34; white-space: nowrap;
}
#nav li:hover, #nav a:hover { z-index: 100; position: relative; }
#nav li:hover > a, #nav a:hover {
background: #779e25; border-left-color: #779e25; color: #fff;
}
/* 子選單顯示邏輯 */
#nav ul { position: absolute; left: -9999px; top: -9999px; }
#nav li:hover > ul { left: 0; top: 23px; border-left: 1px solid #779e25; }
#nav li:hover li:hover > ul { left: 100%; top: 0; margin-left: 0; }
#nav li:hover > ul ul { left: -9999px; top: -9999px; }
<ul id="nav">
<li><a href="#">首頁</a></li>
<li><a href="#">菜單一</a>
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul>
<li><a href="#">百度菜單</a></li>
...
</ul>
<!--[if lte IE 6]></td></tr></table><![endif]-->
</li>
<li><a href="#">菜單二 »</a>
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul>
<li><a href="#">三級菜單 »</a>
<!--[if lte IE 6]><table><tr><td><![endif]-->
<ul>...</ul>
<!--[if lte IE 6]></td></tr></table><![endif]-->
</li>
</ul>
<!--[if lte IE 6]></td></tr></table><![endif]-->
</li>
</ul>
opacity/rgba() 替代濾鏡實現半透明transition 可添加平滑展開動畫aria-haspopup、aria-expanded 屬性