DIV CSS 佈局教程網

jQuery 傾斜風格動畫導航菜單(相容 IE、Chrome、Firefox)
編輯:JQuery常見問題     

效果預覽:https:///keleyi/phtml/jqmenu/index.htm

這個菜單的視覺關鍵是「傾斜的色塊」:每個 li 裡塞一個 p 當底色、一個 b 當左側的小尖角,兩者都用絕對定位藏在 li 左側(left:-164px)。滑鼠移入時把 p 動畫推到 left:0,看起來就像色塊從左邊斜插入位;移出再退回。再用第二個 ready 區塊依序累加 margin-left,讓整排項目呈現輕微的階梯錯位。

CSS 結構

.keleyimenu{width:355px;background:url(https:///keleyi/phtml/jqmenu/index/nav-bg.png) no-repeat;}
.keleyimenu ul{margin:0 0 0 42px;}
.keleyimenu ul li{width:164px;height:29px;padding-bottom:8px;overflow:hidden;position:relative;float:left;display:inline;}
.keleyimenu ul li a{display:block;width:164px;height:29px;line-height:26px;font-size:16px;color:#FFFFFF;text-indent:62px;position:relative;z-index:100000;}
.keleyimenu ul li p{position:absolute;z-index:100;top:0;left:-164px;width:164px;height:29px;background:url(https:///keleyi/phtml/jqmenu/index/nav-libg.png) no-repeat;}
.keleyimenu ul li b{position:absolute;z-index:100000;top:0;left:0;display:block;width:11px;height:29px;background:url(https:///keleyi/phtml/jqmenu/index/nav-jiao2.png) no-repeat;}
.keleyimenu ul li.current p{left:0;}
.keleyimenu ul li.current a{color:#0072d2;}

JavaScript

$(function () {
  $(".ke"+"leyimenu ul li").append("<b class='pngFix'></b><p class='pngFix'></p>");

  $(".keleyimenu ul li").hover(function () {
    if ($(this).attr("class") != "current") {
      $(this).children("p").stop().animate({ left: "0px" }, 200);
      $(this).children("a").css({ color: "#0072d2" }, 900);
    }
  }, function () {
    if ($(this).attr("class") != "current") {
      $(this).children("p").stop().animate({ left: "-164px" }, 300);
      $(this).children("a").css({ color: "#FFFFFF" }, 900);
    }
  });

  $(".kele"+"yimenu ul li.current").unbind("hover");
});

$(function ($) {
  var left = 0;
  var length = $(".keleyimenu li").length;
  for (length; length > 0; length--) {
    left += 12.60;
    $(".keleyimenu li").eq(length - 1).css("margin-left", left)
  }
});

function setNav(id) {
  var navList = $(".keleyimenu ul li");
  $(navList).eq(id - 1).addClass("current");
}

幾個值得學的技巧

  • 用 append 補上裝飾節點:p 與 b 不必寫在 HTML 裡,交給 jQuery 動態產生,結構更乾淨。
  • animate 前先 stop():避免連續滑動時動畫堆疊,是滑入效果不卡頓的關鍵。
  • 用 eq() 定位當前項:setNav(id) 靠 eq(id - 1) 切換高亮,因為 eq 的 index 從 0 起算。
  • unbind 排除 current 項:當前所在的分類不需要 hover 效果,直接解除綁定。

程式碼中用到的 eq 函數詳解:https:///a/bjac/tmx4mq76.htm

菜單大全:https:///a/bjac/veugsmw9.htm

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