萬盛學電腦網

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

jQuery 翻牌式導航菜單:animate 縮放+透明度組合特效

效果描述

導航項為 6 個方塊,懸停時鏈接元素上下縮放「翻牌」一次;點擊時放大淡出再縮小歸位,產生 3D 翻轉視覺錯覺。

核心動畫邏輯

var nummove = 0, numout = 0; // 防重入鎖

$(function() {
    $("#btncell li").hover(
        function() { // 進入:快速縮放動畫
            if (nummove === 0) {
                nummove = 1;
                $("a", this)
                    .animate({ height: "0px", top: "25px" }, 80, function() { nummove = 0; })
                    .animate({ height: "28px", top: "10px" }, 80)
                    .css("background", "yellow");
            }
        },
        function() { // 離開:反向動畫
            if (numout === 0) {
                numout = 1;
                $("a", this)
                    .animate({ height: "0px", top: "25px" }, 80, function() { numout = 0; })
                    .animate({ height: "28px", top: "10px" }, 80)
                    .css("background", "#888");
            }
        }
    );

    $("#btncell li a").click(function() {
        var $li = $(this).parent().css("z-index", 2);
        $(this).animate({
            height: "558px", top: "-255px", width: "1000px", left: "-460px",
            opacity: 'toggle', lineHeight: '558px', fontSize: '500px'
        }, 1000)
        .animate({
            height: "28px", top: "10px", width: "50px", left: "25px",
            opacity: 'toggle', lineHeight: '28px', fontSize: '12px'
        }, 400, function() {
            $li.css("z-index", 1);
        });
    });
});

關鍵技術點

  • 鎖變量防抖:nummove/numout 防止快速進出觸發動畫疊加。
  • 鏈式 animate:利用隊列特性實現「縮到 0 → 恢復」的翻牌感。
  • 多屬性同步動畫:點擊時同時動畫 height/width/top/left/opacity/lineHeight/fontSize,產生爆炸式放大效果。
  • z-index 管理:點擊瞬間提升層級避免被遮擋,動畫結束恢復。

HTML 結構

<ul id="btncell">
    <li><a href="#">1</a></li>
    <li><a href="#">2</a></li>
    ...
</ul>

<style>
#btncell { width:300px; height:100px; border:1px solid #777; margin:50px auto; }
#btncell li { width:100px; float:left; height:50px; position:relative; z-index:1; }
#btncell li a {
    display:block; width:50px; height:28px; border:2px solid #333;
    background:#888; text-decoration:none; text-align:center; line-height:28px;
    position:absolute; left:25px; top:10px; overflow:hidden;
}
</style>

現代化改造:CSS 3D 翻轉

.flip-card {
    perspective: 1000px; width: 100px; height: 50px;
}
.flip-card-inner {
    position: relative; width: 100%; height: 100%;
    transition: transform 0.6s; transform-style: preserve-3d;
}
.flip-card:hover .flip-card-inner {
    transform: rotateX(180deg);
}
.flip-card-front, .flip-card-back {
    position: absolute; width: 100%; height: 100%;
    backface-visibility: hidden; display: flex; align-items: center; justify-content: center;
}
.flip-card-back { transform: rotateX(180deg); background: yellow; }

純 CSS 實現真 3D 翻轉,無 JS 計算、GPU 加速、代碼量減 80%。

copyright © 萬盛學電腦網 all rights reserved