jQuery製作圖片旋轉效果,jquery製作旋轉

來源:互聯網
上載者:User

jQuery製作圖片旋轉效果,jquery製作旋轉

以前用JQuery寫過一個縱深方向上的圖片旋轉效果,在這裡拿出來跟大家分享下,貼上一張圖片看看效果是如何的:

其實現原理並不複雜,在數學上只用到了其中的正弦函數,製作過程大致如下:

(1)先定義好圖片旋轉的半徑

(2)圖片旋轉的過程需要用到setInterval()方法,來擷取每一張圖片所在位置的的角度,角度會根據時間變化逐漸層化

(3)根據一個數學公式:x=R*SIN(deg)可以獲得圖片在X方向的位置

(4)透明度的設定其實也是根據圖片旋轉時候的角度來定的。初始設定圖片在正前方時是0度,無論是正時針還是逆時針方式旋轉,當圖片旋轉角度大於0度

    小於180度時圖片的透明度是逐漸減小的,這裡為了圖片在180度時不至於完全透明加了個小小的計算公式,代碼會在下面展示。

(5)圖片的縮放也是根據圖片旋轉角度而定的,相信容易理解。

(6)有了圖片的X軸位置資訊,縮放資訊,透明度資訊後,接下來就是很簡單的事情了,只要將所有的資訊通過CSS樣式顯示出來就可以了。

   css的樣式會通過setInterval()方法逐漸改變。

下面來看下主要代碼:

var thisleft=-(o.radius)*Math.sin((360/imgnum)*$(this).data("index")*(Math.PI*2/360))+(holderwidth/2); var thiszindex=360/imgnum*$(this).data("index")>180?360/imgnum*$(this).data("index")-360:-360/imgnum*$(this).data("index"); var thisopacity=360/imgnum*$(this).data("index")<=180?(180-360/imgnum*$(this).data("index"))/180*1.2:  (360/imgnum*$(this).data("index")-180)/180*1.2;

第二行的thiszindex是圖片的深度資訊,對每張圖片我都給它加了一個index屬性儲存其索引值,圖片會根據這個資訊通過計算得到相應的深度值。

第三行的thisopacity是圖片的透明度資訊。

每一張圖片都會被賦予一下的CSS樣式:

$(this).css({  "left":thisleft-(o.width*thisopacity)/2+"px",  "top":(holderheight/2)-o.width*(thisopacity+1)/4,  "z-index":thiszindex+180,  "opacity":(thisopacity+0.2)/1.2,  "width":o.width*(thisopacity+1)/2,  "height":o.height*(thisopacity+1)/2  });

第五行的opacity用了一個簡單的公式使其在最深度時不至於完全透明。

在功能上我加了左右轉的功能,其原理也就是將圖片的X軸資訊的正負值轉換而已,代碼如下:

if(dir=='left'){  thisleft=-(o.radius)*Math.sin(xx*(Math.PI*2/360))+(holderwidth/2);  }else{  thisleft=(o.radius)*Math.sin(xx*(Math.PI*2/360))+(holderwidth/2);  }

整個效果中使用者可以自訂一下參數:

$.fn.scroll3d.defaults={ speed:25, radius:100, width:200, height:150, direction:'left'  }

下面附上效果的完整代碼:

(function($) { $.fn.scroll3d = function(options) { var opts = $.extend({}, $.fn.scroll3d.defaults, options); var $this = $(this); var o = $.meta ? $.extend({}, opts, $(this).data()) : opts; var radius = o.radius; var timer = 0; var xx = 0; var speed = o.speed; var dir = o.direction; $(this).hide(); return this.each(function() { var $img = $(this).find('img').css({'position': 'absolute'}), num = 0; var imgnum = $img.length; var holderwidth = $(this).width(), holderheight = $(this).height(); $img.each(function(i) { var imgsrc = $(this).attr("src"); $(this).data({ "index": i }); $(this).load(function() { ++num; if (num == imgnum) { $this.show(); } }).attr({ "src": imgsrc }); var thisleft = -(o.radius) * Math.sin((360 / imgnum) * $(this).data("index") * (Math.PI * 2 / 360)) + (holderwidth / 2); var thiszindex = 360 / imgnum * $(this).data("index") > 180 ? 360 / imgnum * $(this).data("index") - 360 : -360 / imgnum * $(this).data("index"); var thisopacity = 360 / imgnum * $(this).data("index") <= 180 ? (180 - 360 / imgnum * $(this).data("index")) / 180 * 1.2 : (360 / imgnum * $(this).data("index") - 180) / 180 * 1.2; $(this).attr({ "nowdeg": (360 / imgnum) * $(this).data("index") }); $(this).css({ "left": thisleft - (o.width * thisopacity) / 2 + "px", "top": (holderheight / 2) - o.width * (thisopacity + 1) / 4, "z-index": thiszindex + 180, "opacity": (thisopacity + 0.2) / 1.2, "width": o.width * (thisopacity + 1) / 2, "height": o.height * (thisopacity + 1) / 2 }); }); function scrollimg() { $img.each(function() { var thisdeg = $(this).attr('nowdeg'); var thisleft; xx = thisdeg; if (dir == 'left') { thisleft = -(o.radius) * Math.sin(xx * (Math.PI * 2 / 360)) + (holderwidth / 2); } else { thisleft = (o.radius) * Math.sin(xx * (Math.PI * 2 / 360)) + (holderwidth / 2); } var thiszindex = xx > 180 ? xx - 360 : -xx; var thisopacity = xx <= 180 ? (180 - xx) / 180 : ($(this).attr('nowdeg') - 180) / 180; $(this).css({ "left": thisleft - (o.width * thisopacity) / 2 + "px", "top": (holderheight / 2) - o.width * (thisopacity + 1) / 4, "z-index": thiszindex + 180, "opacity": (thisopacity + 0.2) / 1.2, "width": o.width * (thisopacity + 1) / 2, "height": o.height * (thisopacity + 1) / 2 }); xx++; if (xx > 360) xx = 0; $(this).attr({ "nowdeg": xx }); }); }; var tt = setInterval(scrollimg, speed); $img.hover(function() { clearInterval(tt); }, function() { tt = setInterval(scrollimg, speed); }); }); }; $.fn.scroll3d.defaults = { speed: 25, radius: 300, width: 200, height: 150, direction: 'left' } })(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_jb51.rar

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的協助,同時也希望多多支援幫客之家!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.