想讓導航菜單更有巧思?可以試試「翻牌」效果:平時是摺疊的方塊,滑鼠移入時翻開、移出時收起,點擊時還能放大翻轉。本文以 jQuery 的 animate 搭配 hover 來實作這個生動的互動。
核心思路是用兩個狀態變數(nummove、numout)避免動畫重複觸發,並以 animate 改變高度與 top 值模擬翻牌:
var nummove = 0;
var numout = 0;
$(function(){
$("#btncell li").hover(
function(){
if(nummove == 0) {
nummove = 1;
$("a",this).animate({ height: "0px", top: "25px" }, 80, function(){ nummove = 0; });
$("a",this).animate({ height: "28px", top: "10px" }, 80);
$("a",this).css("background","yellow");
}
},
function(){
if(numout == 0) {
numout = 1;
$("a",this).animate({ height: "0px", top: "25px" }, 80, function(){ numout = 0; });
$("a",this).animate({ height: "28px", top: "10px" }, 80);
$("a",this).css("background","#888");
}
}
);
$("#btncell li a").click(function(){
$(this).parents("li").css("z-index","2");
$(this).animate({ height: "558px", top: "-255px", width: "1000px", left: "-460px",
opacity: 'toggle', lineHeight: '558px', fontSize: '500px' }, 1000);
$(this).animate({ height: "28px", top: "10px", width: "50px", left: "25px",
opacity: 'toggle', lineHeight: '28px', fontSize: '12px' }, 400);
$(this).parents("li").css("z-index","1");
});
});
這裡的關鍵技巧有兩個:一是用 nummove/numout 這類「開關變數」防止滑鼠快速移動時動畫堆疊錯亂;二是點擊時透過 z-index 暫時提升層級,讓放大的方塊能蓋在其他元素之上,動畫結束後再還原。適度運用這些細節,就能做出耳目一新的翻牌菜單。