DIV CSS 佈局教程網

jQuery 同心圓圖片放大特效實作
編輯:JQuery特效代碼     

這個效果把三張不同尺寸的同心圓形圖片疊在一起,滑鼠移入時它們同時放大並向左上方偏移,營造出「由中心向外推開」的視覺層次。適合用在首頁的視覺焦點區。

結構構想

三個圓形容器(.water1/2/3)用絕對定位排在同一個背景上,各自裝入大、中、小三張圖。border-radius 設為寬高的一半讓容器變成正圓,overflow:hidden 則確保圖片不會溢出圓外。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<script src="js/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(e) {
 $(".water1").mouseover(function(){
  $("#img1").stop(true,true).animate({top:"-32.5px",left:"-32.5px", width:"400px",height:"400px"},"slow");
  $("#img2").stop(true,true).animate({top:"-10px",left:"-10px", width:"115px",height:"115px"},"slow");
  $("#img3").stop(true,true).animate({top:"-4px",left:"-4px", width:"41px",height:"41px"},"slow");
 })
 $(".water1").mouseout(function(){
  $("#img1").stop(true,true).animate({top:"0px",left:"0px",width:"335px",height:"335px"},"slow");
  $("#img2").stop(true,true).animate({top:"0px",left:"0px", width:"95px",height:"95px"},"slow");
  $("#img3").stop(true,true).animate({top:"0px",left:"0px", width:"33px",height:"33px"},"slow");
 })
});
</script>
<style>
.show{ width:1440px; height:474px; position:relative; background-color:#3d9abc;}
.water1{ height: 335px; width:335px; border-radius:167.5px; overflow:hidden; position:absolute; left:186px; top:69px;}
.water2{ height: 95px; width:95px; border-radius:47.5px; overflow:hidden; position:absolute; left:545px; top:294px;}
.water3{ height: 33px; width:33px; border-radius:16.5px; overflow:hidden; position:absolute; left:549px; top:220px;}
#img1{ position:absolute;}
#img2{ position:absolute; }
#img3{ position:absolute;}
</style>
</head>
<body>
<div class="show">
<div class="water1"><img id="img1" src="images/big.jpg" /></div>
<div class="water2"><img id="img2" src="images/middle.jpg" /></div>
<div class="water3"><img id="img3" src="images/small.jpg" /></div>
</div>
</body>
</html>

數字背後的規律

把放大前後的數據並排看,規律就出來了:

· img1:335 → 400(+65),偏移 -32.5 ← 正好是差值的一半

· img2:95 → 115(+20),偏移 -10 ← 同樣是差值的一半

· img3:33 → 41(+8),偏移 -4 ← 依然吻合

這個「偏移量 = 尺寸增量 ÷ 2」的關係不是巧合。因為元素預設以左上角為定位原點,放大時若不做補償,它就會往右下「長」出去。把 top 與 left 各減去增量的一半,等於讓元素以自身中心為基準均勻向外擴展,視覺上才會有「由中心放大」的效果。

兩個實務提醒

stop(true, true) 為什麼重要:滑鼠快速進出時,若不清除佇列中的動畫,放大與縮小的動畫會互相堆疊,畫面就會出現抖動與延遲。第一個參數 true 清除排隊中的動畫,第二個 true 讓目前的動畫直接跳到終點,兩者搭配能給出最俐落的手感。

hover 的觸發範圍:範例只綁在 .water1 上,所以只有移入大圓才會觸發。若想讓整個 .show 區域都能觸發,把選擇器換掉即可。另外建議用 .hover() 或 .on("mouseenter mouseleave") 取代分離的 mouseover/mouseout——後者會在元素內部移動時反覆觸發,前者不會。

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