這是本人第一次在博客園寫點東西,初出茅廬,東西寫的不好還請各位看官,各位兄弟姐妹不要見笑。
以前用JQuery寫過一個縱深方向上的圖片旋轉效果,在這里拿出來跟大家分享下,貼上一張圖片看看效果是如何的:
其實現原理並不復雜,在數學上只用到了其中的正弦函數,制作過程大致如下:
(1)先定義好圖片旋轉的半徑
(2)圖片旋轉的過程需要用到setInterval()方法,來獲取每一張圖片所在位置的的角度,角度會根據時間變化逐漸變化
(3)根據一個數學公式:x=R*SIN(deg)可以獲得圖片在X方向的位置
(4)透明度的設置其實也是根據圖片旋轉時候的角度來定的。初始設置圖片在正前方時是0度,無論是正時針還是逆時針方式旋轉,當圖片旋轉角度大於0度
小於180度時圖片的透明度是逐漸減小的,這里為了圖片在180度時不至於完全透明加了個小小的計算公式,代碼會在下面展示。
(5)圖片的縮放也是根據圖片旋轉角度而定的,相信容易理解。
(6)有了圖片的X軸位置信息,縮放信息,透明度信息后,接下來就是很簡單的事情了,只要將所有的信息通過CSS樣式顯示出來就可以了。
css的樣式會通過setInterval()方法逐漸改變。
下面來看下主要代碼:
1 var thisleft=-(o.radius)*Math.sin((360/imgnum)*$(this).data("index")*(Math.PI*2/360))+(holderwidth/2);
2 var thiszindex=360/imgnum*$(this).data("index")>180?360/imgnum*$(this).data("index")-360:-360/imgnum*$(this).data("index");
3 var thisopacity=360/imgnum*$(this).data("index")<=180?(180-360/imgnum*$(this).data("index"))/180*1.2:
4 (360/imgnum*$(this).data("index")-180)/180*1.2;
第二行的thiszindex是圖片的深度信息,對每張圖片我都給它加了一個index屬性保存其索引值,圖片會根據這個信息通過計算得到相應的深度值。
第三行的thisopacity是圖片的透明度信息。
每一張圖片都會被賦予一下的CSS樣式:
1 $(this).css({
2 "left":thisleft-(o.width*thisopacity)/2+"px",
3 "top":(holderheight/2)-o.width*(thisopacity+1)/4,
4 "z-index":thiszindex+180,
5 "opacity":(thisopacity+0.2)/1.2,
6 "width":o.width*(thisopacity+1)/2,
7 "height":o.height*(thisopacity+1)/2
8 });
第五行的opacity用了一個簡單的公式使其在最深度時不至於完全透明。
在功能上我加了左右轉的功能,其原理也就是將圖片的X軸信息的正負值轉換而已,代碼如下:
1 if(dir=='left'){
2 thisleft=-(o.radius)*Math.sin(xx*(Math.PI*2/360))+(holderwidth/2);
3 }else{
4 thisleft=(o.radius)*Math.sin(xx*(Math.PI*2/360))+(holderwidth/2);
5 }
整個效果中用戶可以自定義一下參數:
$.fn.scroll3d.defaults={
speed:25,
radius:100,
width:200,
height:150,
direction:'left'
}
下面附上效果的完整代碼:
1 (function($) { 2 $.fn.scroll3d = function(options) { 3 var opts = $.extend({}, $.fn.scroll3d.defaults, options); 4 var $this = $(this); 5 var o = $.meta ? $.extend({}, opts, $(this).data()) : opts; 6 var radius = o.radius; 7 var timer = 0; 8 var xx = 0; 9 var speed = o.speed; 10 var dir = o.direction; 11 $(this).hide(); 12 13 return this.each(function() { 14 var $img = $(this).find('img').css({'position': 'absolute'}), num = 0; 15 var imgnum = $img.length; 16 var holderwidth = $(this).width(), 17 holderheight = $(this).height(); 18 19 $img.each(function(i) { 20 var imgsrc = $(this).attr("src"); 21 $(this).data({ 22 "index": i 23 }); 24 25 $(this).load(function() { 26 ++num; 27 if (num == imgnum) { 28 $this.show(); 29 } 30 }).attr({ 31 "src": imgsrc 32 }); 33 34 var thisleft = -(o.radius) * Math.sin((360 / imgnum) * $(this).data("index") * (Math.PI * 2 / 360)) + (holderwidth / 2); 35 var thiszindex = 360 / imgnum * $(this).data("index") > 180 ? 360 / imgnum * $(this).data("index") - 360 : -360 / imgnum * $(this).data("index"); 36 var thisopacity = 360 / imgnum * $(this).data("index") <= 180 ? (180 - 360 / imgnum * $(this).data("index")) / 180 * 1.2 : 37 (360 / imgnum * $(this).data("index") - 180) / 180 * 1.2; 38 39 $(this).attr({ 40 "nowdeg": (360 / imgnum) * $(this).data("index") 41 }); 42 43 $(this).css({ 44 "left": thisleft - (o.width * thisopacity) / 2 + "px", 45 "top": (holderheight / 2) - o.width * (thisopacity + 1) / 4, 46 "z-index": thiszindex + 180, 47 "opacity": (thisopacity + 0.2) / 1.2, 48 "width": o.width * (thisopacity + 1) / 2, 49 "height": o.height * (thisopacity + 1) / 2 50 }); 51 }); 52 53 function scrollimg() { 54 $img.each(function() { 55 var thisdeg = $(this).attr('nowdeg'); 56 var thisleft; 57 xx = thisdeg; 58 59 if (dir == 'left') { 60 thisleft = -(o.radius) * Math.sin(xx * (Math.PI * 2 / 360)) + (holderwidth / 2); 61 } else { 62 thisleft = (o.radius) * Math.sin(xx * (Math.PI * 2 / 360)) + (holderwidth / 2); 63 } 64 65 var thiszindex = xx > 180 ? xx - 360 : -xx; 66 var thisopacity = xx <= 180 ? (180 - xx) / 180 : ($(this).attr('nowdeg') - 180) / 180; 67 68 $(this).css({ 69 "left": thisleft - (o.width * thisopacity) / 2 + "px", 70 "top": (holderheight / 2) - o.width * (thisopacity + 1) / 4, 71 "z-index": thiszindex + 180, 72 "opacity": (thisopacity + 0.2) / 1.2, 73 "width": o.width * (thisopacity + 1) / 2, 74 "height": o.height * (thisopacity + 1) / 2 75 }); 76 77 xx++; 78 if (xx > 360) xx = 0; 79 $(this).attr({ 80 "nowdeg": xx 81 }); 82 }); 83 }; 84 85 var tt = setInterval(scrollimg, speed); 86 87 $img.hover(function() { 88 clearInterval(tt); 89 }, function() { 90 tt = setInterval(scrollimg, speed); 91 }); 92 93 }); 94 }; 95 96 $.fn.scroll3d.defaults = { 97 speed: 25, 98 radius: 300, 99 width: 200, 100 height: 150, 101 direction: 'left' 102 } 103 })(jQuery);
在HTML中只需要有一個DIV包含你所需要的圖片就可以完成這個效果,例如:
<div class="holder" style="width:550px;height:300px;position:relative;">
<img src="img/1.jpg" />
<img src="img/2.jpg" />
<img src="img/3.jpg" />
<img src="img/1.jpg" />
<img src="img/2.jpg" />
</div>
代碼的調用可以這樣寫:
$('.holder').scroll3d();
寫的有點亂七八糟,還望各位見諒!
壓縮包:test.zip