這是一個用原生 JS 實現的手風琴式滑動菜單,同一時刻只展開一項,非常適合 FAQ 列表或二級導航。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body,div,ul,li,p,h1,h2 { margin:0; padding:0; border:0; background:#FAFAFA;
font-family:Arial, Helvetica, sans-serif,"宋體" }
body { text-align:center; font-size:12px }
li { list-style:none }
.rolinList { width:402px; height:auto; margin:20px auto 0 auto; text-align:left }
.rolinList li { margin-bottom:1px; border:1px solid #DADADA }
.rolinList li h2 { width:380px; height:40px; background:#fff; font-size:14px;
line-height:40px; padding-left:20px; color:#333; cursor:pointer }
.content { width:400px; background:#FAFAFA }
.content p { margin:12px }
</style>
<script type="text/javascript">
window.onload = function () { rolinTab("rolin"); }
function rolinTab(obj) {
var list = $(obj).getElementsByTagName("LI");
// state 是一個共享狀態:是否正在展開 / 正在收起 / 當前展開的項
var state = { show: false, hidden: false, showObj: false };
for (var i = 0; i < list.length; i++) {
var tmp = new rolinItem(list[i], state);
if (i == 0) tmp.pShow(); // 默認展開第一項
}
}
function rolinItem(obj, state) {
var speed = 0.0666; // 動畫速度係數
var range = 1; // 收尾容差
var interval;
var tarH;
var tar = this;
var head = getFirstChild(obj);
var content = getNextChild(head);
var isOpen = false;
this.pHidden = function () { if (isOpen) hidden(); }
this.pShow = show;
var baseH = content.offsetHeight; // 先量出真實高度
content.style.display = "none";
head.onmouseover = function () { this.style.background = "#EFEFEF"; }
head.onmouseout = mouseout;
head.onclick = function () {
this.style.background = "#EFEFEF";
if (!state.show && !state.hidden) { // 動畫期間不響應點擊,避免錯亂
if (!isOpen) { head.onmouseout = null; show(); }
else { hidden(); }
}
};
function mouseout() { this.style.background = "#FFF" }
function show() {
head.style.borderBottom = "1px solid #DADADA";
state.show = true;
if (state.openObj && state.openObj != tar) state.openObj.pHidden(); // 關閉上一項
content.style.height = "0px";
content.style.display = "block";
content.style.overflow = "hidden";
state.openObj = tar;
tarH = baseH;
interval = setInterval(move, 10);
}
function showS() { isOpen = true; state.show = false; }
function hidden() {
state.hidden = true;
tarH = 0;
interval = setInterval(move, 10);
}
function hiddenS() {
head.style.borderBottom = "none";
head.onmouseout = mouseout;
head.onmouseout();
content.style.display = "none";
isOpen = false;
state.hidden = false;
}
function move() {
// 每幀走完剩餘距離的固定比例,形成先快後慢的緩動
var dist = (tarH - content.style.height.pxToNum()) * speed;
if (Math.abs(dist) < 1) dist = dist > 0 ? 1 : -1;
content.style.height = (content.style.height.pxToNum() + dist) + "px";
if (Math.abs(content.style.height.pxToNum() - tarH) <= range) {
clearInterval(interval);
content.style.height = tarH + "px";
if (tarH != 0) showS(); else hiddenS();
}
}
}
var $ = function ($) { return document.getElementById($); };
String.prototype.pxToNum = function () { return Number(this.replace("px", "")); };
function getFirstChild(obj) {
var result = obj.firstChild;
while (!result.tagName) result = result.nextSibling;
return result;
}
function getNextChild(obj) {
var result = obj.nextSibling;
while (!result.tagName) result = result.nextSibling;
return result;
}
</script>
<ul id="rolin" class="rolinList">
<li><h2>1</h2><div class="content"><p>--專業IT綜合性網站<br><br>
<a target="_blank" href="https://www.jb51.net/">https://www.jb51.net/</a></p></div></li>
<li><h2>2</h2><div class="content"><p>--專業IT綜合性網站</p></div></li>
<li><h2>3</h2><div class="content"><p>--專業IT綜合性網站</p></div></li>
</ul>
共享 state 對象。所有 rolinItem 實例引用同一個 state,因此可以實現「展開新項時自動收起舊項」,同時用 show / hidden 兩個標誌鎖住動畫過程中的重複點擊,避免多個 setInterval 打架。這是純原生實現手風琴最關鍵的一點。
先量高度再隱藏。content 在 display:none 之前先讀取 offsetHeight 存進 baseH,因為一旦隱藏就量不到真實高度了。後續動畫的目標值都來自這個快照。
比例緩動。每幀移動「剩餘距離 × 係數」,距離越短步子越小,形成自然的先快後慢;再配合 range 容差與最小步長 1px 收尾,避免無限接近卻停不下來。
現代化改造建議:CSS3 的 height 過渡無法直接對 auto 生效,可以改用 max-height 過渡,或者用 grid-template-rows: 0fr → 1fr 的技巧,完全拋開 JS 計時器;同時把 String.prototype 擴展改成獨立工具函數,避免污染內置原型。
以上就是本文的全部內容,希望對大家的學習有所幫助。