DIV CSS 佈局教程網

用 jQuery 打造鼠標懸停翻轉的酷炫圖片效果
編輯:JavaScript綜合知識     

只要思路正確,短短十幾行程式碼就能做出相當酷炫的圖片翻滾效果。本文示範當鼠標移到圖片上時,圖片向上收合、顯示說明文字,移開後再還原的流暢動畫。

HTML 結構

以清單包裹圖片與一個帶文字的連結:

<ul class="list">
  <li><img src="images/10.jpg" alt="" /><a href="#"><span>1</span></a></li>
  <li><img src="images/11.jpg" alt="" /><a href="#"><span>2</span></a></li>
  ...
</ul>

CSS 樣式

關鍵在於 li 設為 position:relative,連結 a 以絕對定位疊在圖片上、高度初始為 0:

.list li{ float:left; width:150px; height:80px; overflow:hidden; position:relative; margin:5px;}
.list li a{ position:absolute; left:0; top:0; width:150px; height:0; background:#999; margin-top:40px; line-height:80px; text-align:center; color:#fff; text-decoration:none; overflow:hidden;}

jQuery 動畫

利用 hover 的兩個回呼,讓圖片與文字的高度此消彼長:

$(function () {
  $('.list li').hover(function () {
    $(this).children('a,img').stop();
    $(this).children('img').stop().animate({ 'marginTop': 40, 'height': 0 }, 200, function () {
      $(this).siblings('a').stop().animate({ 'height': 80, 'marginTop': 0 }, 200);
    });
  }, function () {
    $(this).children('a,img').stop();
    $(this).children('a').stop().animate({ 'height': 0, 'marginTop': 40 }, 200, function () {
      $(this).siblings('img').stop().animate({ 'marginTop': 0, 'height': 80 }, 200);
    });
  });
});

這裡用 stop() 中止上一段動畫避免抖動,再以 animate 讓圖片收合、文字展開,營造出俐落的翻滾感。是不是幾句 jQuery 就搞定了呢?不妨複製下來看實際效果。

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