這裡分享一款視覺清爽的滑動導覽選單,由 CSS 與 JavaScript 搭配完成:游標移到一級項目上,二級選單會以「高度遞增 + 透明度遞增」的方式滑展開來。相容性不錯,適合大多數網站使用。範例用到兩張背景圖,請先下載到本地再替換路徑。
運行效果預覽:

線上展示:https://demo.jb51.net/js/2015/js-css-dqqx-scroll-menu-demo/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="https://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>滑動導航菜單</title> <style> body {margin:25px; font:12px Verdana, Arial, Helvetica} * {padding:0; margin:0} .dropdown {float:left; padding-right:5px} .dropdown dt {width:188px; border:2px solid #9ac1c9; padding:8px; font-weight:bold; cursor:pointer; background:url(images/header.gif)} .dropdown dt:hover {background:url(images/header_over.gif)} .dropdown dd {position:absolute; overflow:hidden; width:208px; display:none; background:#fff; z-index:200; opacity:0} .dropdown ul {width:204px; border:2px solid #9ac1c9; list-style:none; border-top:none} .dropdown li {display:inline} .dropdown a, .dropdown a:active, .dropdown a:visited {display:block; padding:5px; color:#333; text-decoration:none; background:#eaf0f2; width:194px} .dropdown a:hover {background:#d9e1e4; color:#000} .dropdown .underline {border-bottom:1px solid #b9d6dc} </style> <script type="text/javascript"> var DDSPEED = 10; var DDTIMER = 15; function ddMenu(id,d){ var h = document.getElementById(id + '-ddheader'); var c = document.getElementById(id + '-ddcontent'); clearInterval(c.timer); if(d == 1){ clearTimeout(h.timer); if(c.maxh && c.maxh <= c.offsetHeight){return} else if(!c.maxh){ c.style.display = 'block'; c.style.height = 'auto'; c.maxh = c.offsetHeight; c.style.height = '0px'; } c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER); }else{ h.timer = setTimeout(function(){ddCollapse(c)},50); } } function ddCollapse(c){ c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER); } function cancelHide(id){ var h = document.getElementById(id + '-ddheader'); var c = document.getElementById(id + '-ddcontent'); clearTimeout(h.timer); clearInterval(c.timer); if(c.offsetHeight < c.maxh){ c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER); } } function ddSlide(c,d){ var currh = c.offsetHeight; var dist; if(d == 1){ dist = (Math.round((c.maxh - currh) / DDSPEED)); }else{ dist = (Math.round(currh / DDSPEED)); } if(dist <= 1 && d == 1){ dist = 1; } c.style.height = currh + (dist * d) + 'px'; c.style.opacity = currh / c.maxh; c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')'; if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){ clearInterval(c.timer); } } </script> </head> <body> <dl class="dropdown"> <dt id="one-ddheader" onmouseover="ddMenu('one',1)" onmouseout="ddMenu('one',-1)">源碼下載</dt> <dd id="one-ddcontent" onmouseover="cancelHide('one')" onmouseout="ddMenu('one',-1)"> <ul> <li><a href="#" class="underline">最新源碼</a></li> <li><a href="#" class="underline">下載排行</a></li> <li><a href="#" class="underline">腳本資源</a></li> <li><a href="#">Ajax實例</a></li> </ul> </dd> </dl> <dl class="dropdown"> <dt id="two-ddheader" onmouseover="ddMenu('two',1)" onmouseout="ddMenu('two',-1)">網頁特效</dt> <dd id="two-ddcontent" onmouseover="cancelHide('two')" onmouseout="ddMenu('two',-1)"> <ul> <li><a href="#" class="underline">菜單導航</a></li> <li><a href="#" class="underline">層和布局</a></li> <li><a href="#" class="underline">圖片特效</a></li> </ul> </dd> </dl> </body> </html>
選單用 <dl> 包裝,<dt> 是一級標題、<dd> 是展開後的內容。這比用 div 堆疊更貼近「標籤—內容」的語意。CSS 上 .dropdown dd 設為 position:absolute; overflow:hidden; opacity:0,配合 JS 動態改高度與透明度,才有滑出的錯覺。
用 offsetHeight 記住原始高度。第一次展開時先把 display 設為 block、高度設為 auto,量出 c.maxh = c.offsetHeight 後再壓回 0px。之後所有動畫都以這個值為目標,不必把高度寫死在程式碼裡——選單有幾項都能自動適應。
50 毫秒的收合緩衝。ddMenu(id, -1) 不直接收合,而是 setTimeout(..., 50) 延遲執行;而 dd 上的 onmouseover="cancelHide()" 會把這個計時器清掉並恢復展開。這段設計解決了滑鼠從 dt 移到 dd 途中經過空隙時選單「閃退」的經典問題。
easing 藏在除法裡。ddSlide() 每 15 毫秒執行一次,移動距離是 (maxh - 目前高) / 10 或 目前高 / 10——離目標越遠走得越多、越近走得越少,自然形成先快後慢的緩減速。透明度則直接用 目前高 / maxh 換算,高度與淡入完全同步。終止條件用「小於 2px 就停」,避免無限逼近造成計時器永遠不結束。
同樣的效果,用 CSS transition 三行就能取代整段 ddSlide:
.dropdown dd {
display: block;
max-height: 0;
opacity: 0;
overflow: hidden;
transition: max-height .3s ease, opacity .3s ease;
}
.dropdown:hover dd,
.dropdown:focus-within dd { max-height: 200px; opacity: 1; }
改用 focus-within 之後,鍵盤 Tab 也能展開選單;再把 <dl> 換成 <nav> + <ul> 並加上 aria-expanded,對輔具更友善。手寫 JS 動畫的價值在於理解「緩動函式」與「計時器終止條件」這兩件事——理解之後,交給瀏覽器做會更省效能也更好維護。