DIV CSS 佈局教程網

用 jQuery 製作帶動畫的二級下拉導覽選單
編輯:JavaScript綜合知識     

二級選單要做得不廉價,關鍵在動畫細節:展開時高度與透明度一起變化、主選單的配色同步反轉、子項 hover 有獨立回饋。本文用 animate 把這些堆疊起來。

樣式

body { font-size: 12px; margin: 0; padding: 10px; }
.nav_list { list-style: none; margin: 0; padding: 0; }
.nav_list li {
  float: left;
  text-align: center;
  margin-right: 10px;
  position: relative;      /* 讓子選單得以它為定位基準 */
}
.nav_list li a {
  width: 80px;
  text-align: center;
  padding: 4px 0;
  text-decoration: none;
  color: #ccc;
  background: #996666;
  display: block;
  float: left;
}
.nav_list li a.current {
  width: 80px;
  text-align: center;
  padding: 4px 0;
  text-decoration: none;
  color: #fff;
  background: #f00;
  display: block;
  float: left;
}
.nav_list li div.second {
  color: #999999;
  height: 0px;             /* 起始高度 0,展開才有空間感 */
  overflow: hidden;
  position: absolute;
  left: 0;
  top: 22px;
  width: 80px;
}
.nav_list li div.second a {
  background: #666666;
  color: #fff;
  border: none;
  width: 80px;
  border-top: 1px solid #FFFFFF;
  padding: 5px 0;
}

div.second 的 height:0 加上 overflow:hidden 是整個效果的基礎:內容存在但不佔空間,展開時只需把高度動畫回來。

腳本

<script type="text/javascript">
$(function () {
  Menu(".nav_list");
});

function Menu(ul_class_name) {
  Second(".nav_list");

  var li_name = ul_class_name + " " + "li";
  $("div.second").css("opacity", "0.1");     // 初始近乎透明

  $(li_name).hover(
    function () {
      // 依子項數量推算展開後的總高度
      var a_height = $(this).children("div.second a:first").css("height");
      var a_count = $(this).children("div.second").children("a").length;
      var slide_hieght = (parseInt(a_height) + 11) * a_count;

      $(this).children("div.second").stop()
        .animate({ height: slide_hieght + "px", opacity: "0.9" }, 600);

      // 主選單同步換色,回應目前所在位置
      $(this).children("a:not(.current)").stop()
        .animate({ backgroundColor: "#f00", color: "#fff" }, 800);
    },
    function () {
      $(this).children("div.second").stop()
        .animate({ height: "0px", opacity: "0.1" }, 600);

      $(this).children("a:not(.current)").stop()
        .animate({ backgroundColor: "#996666", color: "#ccc" }, 800);
    }
  );
}

function Second(ul_class_name) {
  var second_menu = ul_class_name + " " + "li" + " " + "div.second" + " " + ">" + " " + "a";
  $(second_menu).hover(
    function () {
      $(this).stop().animate({ backgroundColor: "#000", opacity: "1" }, 400);
    },
    function () {
      $(this).stop().animate({ backgroundColor: "#666", opacity: "0.9" }, 400);
    }
  );
}
</script>

三個關鍵手法

動態推算展開高度

不寫死高度,而是讀取單一子項的 height、加上間距(這裡用 11px)再乘以子項數量。這樣各選單的子項數量不同也能正確展開,不必為每一項客製。

.stop() 防止動畫堆疊

滑鼠快速進出時,若不加 stop,animate 會把每次請求排入佇列,動畫就會「跑不完」。每個 animate 前都掛 .stop() 是 jQuery 動畫的必要習慣。

a:not(.current) 的用意

帶有 current 的項目代表當前所在頁面,顏色不該被 hover 覆蓋。用:not() 排除它,選單的狀態資訊才不會被交互效果洗掉。

結構

<ul class="nav_list">
  <li>
    <a href="#" class="current">Test_1</a>
    <div class="second">
      <a href="#">menu_1</a>
      <a href="#">menu_1</a>
      <a href="#">menu_1</a>
    </div>
  </li>
  <li>
    <a href="#">Test_2</a>
    <div class="second">
      <a href="#">menu_1</a>
      <!-- 子項數量可與其他項目不同 -->
    </div>
  </li>
</ul>

實務上的建議

動畫顏色用 animate 逐格插值,效能不如直接切換 class;改用 CSS transition 搭配 classList,動畫交給瀏覽器、程式碼也更短。另外有兩點務必補上:hover 無法被鍵盤觸發,應同時監聽 focusin/focusout,或改用 CSS 的 :focus-within;觸控裝置沒有 hover,二級選單在手機上需要改用點擊展開的 disclosure 模式,否則子選單永遠打不開。若選單有階層語意,標記上也建議加上 aria-haspopup 與 aria-expanded。

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