DIV CSS 佈局教程網

jQuery 滑鼠移入時滑出標題與描述層
編輯:JavaScript綜合知識     

圖片列表要顯示標題與說明,又不希望平時就佔用畫面,可以讓資訊層藏在圖片上下緣,滑鼠移入時才滑出來。搭配 easing 外掛,動畫手感會好很多。

$(function () {
  $(".item").hover(
    function () {
      // $(this).children("img").stop().animate({ opacity: 0.8 }, 700, "easeInSine");
      $(this).children("div.title").stop().animate({ top: 0 }, 700, "easeOutBounce");
      $(this).children("div.desc").stop().animate({ bottom: 0 }, 700, "easeOutBounce");
    },
    function () {
      // $(this).children("img").stop().animate({ opacity: 1 }, 700);
      $(this).children("div.title").stop().animate({ top: -60 }, 500);
      $(this).children("div.desc").stop().animate({ bottom: -40 }, 400);
    }
  );
});

運作方式

結構上,每個 .item 內含一個 img 與兩個絕對定位的資訊層:div.title 定位在上方(top 為負值即藏在容器外),div.desc 定位在下方(bottom 為負值)。hover 時把 top / bottom 動畫到 0,資訊層就滑進畫面;移出時復位。

有兩個細節是這段程式的關鍵:

stop() 不可省。使用者快速連續移入移出時,動畫會堆疊排隊,畫面看起來就是資訊層一直接tered受、抖來抖去。stop() 會先清掉目前的動畫佇列再開新的,是 jQuery 動畫的標準配套。

easing 讓手感不同。範例用的 easeOutBounce 會帶一點彈跳收尾,比預設的 linear/swing 活潑,需要額外引入 jQuery Easing 外掛。

可以再加強的地方

被註解掉的那兩行是「滑入時把圖片變半透明」,讓文字更清楚,可依設計需求啟用。另外建議在 CSS 上預設加上 overflow: hidden 與 position: relative,確保資訊層不會溢出容器,也讓絕對定位以 .item 為基準。

最後提醒,行動裝置沒有 hover,這種效果在手機上會完全失效。建議搭配 media query,在小螢幕上直接把標題與描述常駐顯示。

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