這次要分享的是一款淡藍色調的滑動門,配色清爽、結構單純,很適合後台或資訊類網站直接沿用。

線上示範:https://demo.jb51.net/js/2015/js-blue-hdm-menu-demo-codes/
滑動門之所以看起來像「真的頁籤」,靠的是一個小技巧——選用中的頁籤把下邊框設為 0,讓它與內容區融為一體;未選用的頁籤則保留 1px 底框。
.normal, .selected{
list-style:none; float:left; width:90px;
padding:5px 2px 2px 2px; text-align:center;
cursor:pointer; border-right:solid 1px #9cd9f5;
background-color:#dee7f5;
}
.normal{ border-bottom:solid 1px #9cd9f5; } /* 未選用:有底框 */
.selected{
border-bottom:solid 0px #9cd9f5; /* 選用:無底框 */
background-color:#f8f8f8; /* 與內容區同色 */
}
#divMainTab{
border-left:solid 1px #9cd9f5;
border-top:solid 1px #9cd9f5;
float:left; margin:0; padding:0;
}
.divContent{
width:474px; height:400px; clear:both;
border-bottom:solid 1px #9cd9f5;
border-left:solid 1px #9cd9f5;
border-right:solid 1px #9cd9f5;
}
顏色規劃也很值得參考:外框統一用 #9cd9f5 當描邊、頁籤底 #dee7f5、內容區 #f8f8f8,三層明度差讓層次自然浮現,不需要陰影。
function changeTab(index){
for (var i = 1; i <= 5; i++){
document.getElementById("li_" + i).className = "normal";
document.getElementById("li_" + index).className = "selected";
document.getElementById("div" + i).style.display = "none";
}
document.getElementById("div1").innerText = "www.jb51.net" + index;
document.getElementById("div" + index).style.display = "block";
}
HTML 端把 onmouseover 掛在每個 li 上:
<li id="li_1" class="selected" onmouseover="changeTab('1')"><a href="#">最新更新</a></li>
<li id="li_2" class="normal" onmouseover="changeTab('2')"><a href="#">閱讀排行</a></li>
...
<div id="div1" style="display:block" class="divContent">1號</div>
<div id="div2" style="display:none" class="divContent">2號</div>
第一,innerText 在 Firefox 舊版是有問題的,建議改用 innerHTML 或 textContent。第二,頁籤數量寫死成 5,日後增減要改 JS;比較好的做法是讓迴圈上限取自實際抓到的 li 數量,例如 document.getElementById("divMainTab").getElementsByTagName("li").length。
另外,若同一頁要放兩組滑動門,務必把 id 命名區隔開(例如 li_2_1),並將函式改成接受群組名稱的通用版本,否則兩組會互相搶樣式。