DIV CSS 佈局教程網

用 jQuery 實現個性翻牌效果的導航菜單
編輯:JQuery特效代碼     

想讓導航菜單更有巧思?可以試試「翻牌」效果:平時是摺疊的方塊,滑鼠移入時翻開、移出時收起,點擊時還能放大翻轉。本文以 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 暫時提升層級,讓放大的方塊能蓋在其他元素之上,動畫結束後再還原。適度運用這些細節,就能做出耳目一新的翻牌菜單。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved