jQuery制作圖片旋轉(zhuǎn)效果
以前用JQuery寫過一個(gè)縱深方向上的圖片旋轉(zhuǎn)效果,在這里拿出來跟大家分享下,貼上一張圖片看看效果是如何的:
其實(shí)現(xiàn)原理并不復(fù)雜,在數(shù)學(xué)上只用到了其中的正弦函數(shù),制作過程大致如下:
(1)先定義好圖片旋轉(zhuǎn)的半徑
(2)圖片旋轉(zhuǎn)的過程需要用到setInterval()方法,來獲取每一張圖片所在位置的的角度,角度會(huì)根據(jù)時(shí)間變化逐漸變化
(3)根據(jù)一個(gè)數(shù)學(xué)公式:x=R*SIN(deg)可以獲得圖片在X方向的位置
(4)透明度的設(shè)置其實(shí)也是根據(jù)圖片旋轉(zhuǎn)時(shí)候的角度來定的。初始設(shè)置圖片在正前方時(shí)是0度,無論是正時(shí)針還是逆時(shí)針方式旋轉(zhuǎn),當(dāng)圖片旋轉(zhuǎn)角度大于0度
小于180度時(shí)圖片的透明度是逐漸減小的,這里為了圖片在180度時(shí)不至于完全透明加了個(gè)小小的計(jì)算公式,代碼會(huì)在下面展示。
(5)圖片的縮放也是根據(jù)圖片旋轉(zhuǎn)角度而定的,相信容易理解。
(6)有了圖片的X軸位置信息,縮放信息,透明度信息后,接下來就是很簡(jiǎn)單的事情了,只要將所有的信息通過CSS樣式顯示出來就可以了。
css的樣式會(huì)通過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是圖片的深度信息,對(duì)每張圖片我都給它加了一個(gè)index屬性保存其索引值,圖片會(huì)根據(jù)這個(gè)信息通過計(jì)算得到相應(yīng)的深度值。
第三行的thisopacity是圖片的透明度信息。
每一張圖片都會(huì)被賦予一下的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用了一個(gè)簡(jiǎn)單的公式使其在最深度時(shí)不至于完全透明。
在功能上我加了左右轉(zhuǎn)的功能,其原理也就是將圖片的X軸信息的正負(fù)值轉(zhuǎn)換而已,代碼如下:
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); }
整個(gè)效果中用戶可以自定義一下參數(shù):
$.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中只需要有一個(gè)DIV包含你所需要的圖片就可以完成這個(gè)效果,例如:
<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>
代碼的調(diào)用可以這樣寫:
$('.holder').scroll3d();
寫的有點(diǎn)亂七八糟,還望各位見諒!
壓縮包:test_jb51.rar
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
- jQuery實(shí)現(xiàn)圖像旋轉(zhuǎn)動(dòng)畫效果
- 基于jQuery插件實(shí)現(xiàn)環(huán)形圖標(biāo)菜單旋轉(zhuǎn)切換特效
- jQuery+CSS3實(shí)現(xiàn)3D立方體旋轉(zhuǎn)效果
- jQuery右下角旋轉(zhuǎn)環(huán)狀菜單特效代碼
- jQuery實(shí)現(xiàn)炫麗的3d旋轉(zhuǎn)星空效果
- jquery實(shí)現(xiàn)LED廣告牌旋轉(zhuǎn)系統(tǒng)圖片切換效果代碼分享
- jQuery實(shí)現(xiàn)的模仿雨滴下落動(dòng)畫效果
- jQuery實(shí)現(xiàn)數(shù)字自動(dòng)增加或者減少的動(dòng)畫效果示例
- jQuery實(shí)現(xiàn)切換頁面過渡動(dòng)畫效果
- jQuery實(shí)現(xiàn)點(diǎn)擊旋轉(zhuǎn),再點(diǎn)擊恢復(fù)初始狀態(tài)動(dòng)畫效果示例
相關(guān)文章
jQuery插件echarts實(shí)現(xiàn)的單折線圖效果示例【附demo源碼下載】
這篇文章主要介紹了jQuery插件echarts實(shí)現(xiàn)的單折線圖效果,結(jié)合完整實(shí)例形式分析了echarts插件繪制簡(jiǎn)單折線圖的操作步驟與相關(guān)實(shí)現(xiàn)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03jQuery 1.5最新版本的改進(jìn)細(xì)節(jié)分析
jQuery 1.5 beta1出來了,從學(xué)習(xí)跟進(jìn)上來說,這一次已經(jīng)比較晚了(我竟然不知道1.5什么時(shí)候出的alpha,就這么beta了)。2011-01-01jQuery實(shí)現(xiàn)可編輯的表格實(shí)例講解(2)
這篇文章主要內(nèi)容是JQuery實(shí)現(xiàn)可編輯的表格實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-09-09基于jQuery制作小圖標(biāo)上下滑動(dòng)特效
一個(gè)小圖標(biāo)特效,非常有趣的,下面給大家分享基于jquery制作的小圖標(biāo)上下滑動(dòng)特效,代碼簡(jiǎn)單易懂,需要的朋友參考下2017-01-01jquery實(shí)現(xiàn)的一個(gè)文章自定義分段顯示功能
基于jquery實(shí)現(xiàn)的文章自定義分段顯示,如果文章內(nèi)容太多的話轉(zhuǎn)換有點(diǎn)慢,大家若喜歡的話,可以參考下2014-05-05JQuery DataTable刪除行后的頁面更新利用Ajax解決
使用Jquery的DataTable進(jìn)行數(shù)據(jù)表處理非常方便,常遇到的一個(gè)問題就是刪除一行后頁面必須進(jìn)行更新,下面與大家分享下使用Ajax的解決方法2013-05-05