萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

JS 經典特效:仿 Windows XP 左側伸縮菜單實現原理

效果描述

左側垂直菜單,點擊主標題(如「系統任務」、「其他位置」),對應的子面板以**滑動動畫**展開/收起,同時下方主標題位置跟隨位移。同一時刻只能展開一個面板(手風琴模式)。

核心架構(原版)


// 全局狀態
var myLayer = [];      // 所有層(主標題層、子面板層交替)
var myTop = [];        // 各層初始 top
var myLeft = [];       // 各層 left
var myHeight = [];     // 各層高度
var mySlide = [];      // 子面板展開後底部位置
var mySpeed = 5;       // 滑動步長(px/frame)
var whichOpen = -1;    // 當前展開面板索引
var running = false;   // 動畫進行中鎖
var closes = true;     // 是否允許點擊收起

// 初始化:計算所有層的幾何屬性
function MFXinitMenu() {
    // 兼容 IE (document.all) 和 NN4 (document.layers)
    // 遍歷 myLayer,區分主標題(MFXmain[i]=1)和子面板(MFXmain[i]=0)
    // 計算初始位置、高度、滑動目標位置
}

// 點擊主標題觸發
function MFXrunMenu(myName, newspeed) {
    // 三種情況:
    // 1. 無面板展開且點擊主標題 → 展開該面板
    // 2. 有面板展開且點擊其他主標題 → 收起舊面板,展開新面板
    // 3. 點擊已展開面板的主標題 → 收起該面板
    
    // 並行啟動兩個動畫:
    // MFXopenMenuS(myName)  // 移動下方主標題位置 (S = Shift)
    // MFXopenMenuC(myName)  // 子面板高度從 0 增加到目標高度 (C = Collapse/Expand)
}

// 子面板高度動畫
function MFXopenMenuC(myName) {
    const step = mySpeed;
    if (myLayer[first+1].clip.height + step > myHeight[first+1]) {
        myLayer[first+1].clip.height = myHeight[first+1]; // 到達目標
    } else {
        myLayer[first+1].clip.height += step;
    }
    if (running) setTimeout('MFXopenMenuC(first)', 10);
}

// 下方主標題位移動畫
function MFXopenMenuS(myName) {
    const step = mySpeed;
    // 移動所有後續主標題層
    for (let i = first+2; i < myLayer.length; i += 2) {
        myLayer[i].top += step; // NN4
        // IE: myLayer[i].pixelTop += step;
    }
    if (running) setTimeout('MFXopenMenuS(first)', 10);
}

// 關閉動畫對稱(負 step)

關鍵技術點

技術原版實現現代替代
動畫循環setTimeout('fn()', 10) 字符串回調requestAnimationFrame 或 CSS transition
剪裁動畫layer.clip.height / style.clip = 'rect(...)'max-height + transition 或 transform: scaleY()
位移動畫直接修改 top/pixelToptransform: translateY() (GPU 加速)
瀏覽器兼容IF/ELSE 區分 document.all / document.layers無需兼容,標準 API 統一
全局變量大量全局變量ES6 Class 封裝私有狀態
字符串定時器setTimeout('fn()', 10) (eval 內部)函數引用 setTimeout(fn, 10)

現代重寫:CSS + 少量 JS (手風琴模式)


/* HTML */


/* CSS */
.xp-menu { width: 200px; font-family: Tahoma, sans-serif; font-size: 12px; }
.xp-menu-header {
    width: 100%; padding: 6px 10px;
    background: linear-gradient(#0a246a, #000080); color: white;
    border: none; border-bottom: 1px solid #000040;
    text-align: left; cursor: pointer; font: inherit;
}
.xp-menu-header:hover { background: linear-gradient(#1a3a8a, #0000a0); }
.xp-menu-panel {
    max-height: 0; overflow: hidden;
    background: #f0f0f0; border: 1px solid #ccc; border-top: none;
    transition: max-height 0.3s ease, padding 0.3s ease, opacity 0.3s ease;
    padding: 0 10px; opacity: 0;
}
.xp-menu-panel.open {
    max-height: 500px; /* 足夠大的值 */
    padding: 10px; opacity: 1;
}
.xp-menu-panel a { display: block; padding: 4px 8px; color: #000; text-decoration: none; }
.xp-menu-panel a:hover { background: #316ac5; color: #fff; text-decoration: none; }

// JS:僅切換類名
document.querySelector('.xp-menu').addEventListener('click', e => {
    const header = e.target.closest('.xp-menu-header');
    if (!header) return;
    
    const panel = document.getElementById(header.dataset.panel);
    const isOpen = panel.classList.contains('open');
    
    // 手風琴:關閉其他
    document.querySelectorAll('.xp-menu-panel.open').forEach(p => {
        if (p !== panel) p.classList.remove('open');
    });
    document.querySelectorAll('.xp-menu-header.active').forEach(h => {
        if (h !== header) h.classList.remove('active');
    });
    
    // 切換當前
    panel.classList.toggle('open');
    header.classList.toggle('active');
});

原版代碼截斷分析

原文在 MFXcloseMenuC 中斷。完整關閉邏輯應對稱於打開:

  • MFXcloseMenuC:clip.height -= step 直到 0,然後 visibility = hidden
  • MFXcloseMenuS:下方主標題 top -= step 移回原位
  • 動畫結束時設置 running = false,更新 whichOpen

總結

XP 風格伸縮菜單是**手風琴交互**的經典案例。原版用 clip 裁剪高度 + 絕對定位位移實現動畫,代碼量大、耦合度高、依賴瀏覽器私有 API。現代方案:**CSS max-height + transition 控制展開/收起動畫,JS 僅切換 .open 類名**,代碼量減少 95%,性能更優、維護更易、無障礙天然支持。理解原版邏輯有助於掌握「狀態驅動 UI」的思想演進。

copyright © 萬盛學電腦網 all rights reserved