本文實(shí)例講述了jQuery實(shí)現(xiàn)的網(wǎng)頁(yè)3D輪播圖封裝功能。分享給大家供大家參考,具體如下:
成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站建設(shè)、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的龍門(mén)網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
網(wǎng)頁(yè)偽3D輪播圖,其實(shí)就是輪播圖旋轉(zhuǎn)木馬效果。其實(shí)在jquery插件庫(kù)也有很多旋轉(zhuǎn)木馬的插件,但是博主封裝的這個(gè)新的插件比起以上的都適應(yīng)性更好。其適應(yīng)性好表現(xiàn)在:調(diào)用靈活性高用法更簡(jiǎn)單,css樣式都封裝好了基本不用寫(xiě),在body里面寫(xiě)ul>li>img標(biāo)簽即可,可設(shè)置參數(shù)多,甚至不同圖片的大小都可以自適應(yīng)輪播,各個(gè)瀏覽器兼容性好(包括IE,雖然我沒(méi)測(cè)過(guò)IE8以下瀏覽器,不過(guò)IE8以上都沒(méi)問(wèn)題),好了,以下看代碼和用法。
head引入兩個(gè)文件,第一個(gè)是jquery的插件(這是1.11.0版本,當(dāng)然其他版本也可以哦,不過(guò)低版本的相對(duì)IE兼容性更好),第二個(gè)是我封裝好的javascript腳本
<script src="js/jquery-1.11.0.js" type="text/javascript" charset="utf-8"></script> <script src="js/Figure_3D.js" type="text/javascript" charset="utf-8"></script>
body里面插入輪播圖,ul的id要為pic_play,當(dāng)然我這里是7張圖,你也可以增加減少圖片,但是這里有一個(gè)重要問(wèn)題是,圖片數(shù)量要為單數(shù),不能為偶數(shù),否則會(huì)出問(wèn)題,這是一個(gè)小小的bug。當(dāng)然你也可以在li里面寫(xiě)<a><img src/></a>也是可以的。
<ul id="pic_play"> <li><img src="img/dc1.jpg"/></li> <li><img src="img/dc2.jpg"/></li> <li><img src="img/dc3.jpg"/></li> <li><img src="img/dc4.jpg"/></li> <li><img src="img/dc5.jpg"/></li> <li><img src="img/dc6.jpg"/></li> <li><img src="img/dc7.jpg"/></li> </ul>
css基本不用寫(xiě),不過(guò)要習(xí)慣性給padding和margin歸0
*{ margin: 0; padding: 0; list-style: none; }
重要封裝腳本Figure_3D.js
// ulwidth:父節(jié)點(diǎn)ul總寬度(number), // height:ul和圖片的初始化最大高度(中間)(number), // liwidth:圖片的初始化最大寬度(中間)(number), // minopacity:圖片最小透明度(0~1), // minscale:圖片最小縮放系數(shù)(0~1), // direction:默認(rèn)輪播方向("left","right"), // speed:動(dòng)畫(huà)速度(number), // delayed:每張圖片延時(shí)停留時(shí)間(number)(注:delayed>=speed), // mousestop:鼠標(biāo)經(jīng)過(guò)是否停止(false,true), function init(ulwidth,height,liwidth,minopacity,minscale,direction,speed,delayed,mousestop){ $("#pic_play").css({"position":"relative","width":ulwidth,"height":height}); $("#pic_play>li").css({"position":"absolute","width":liwidth,"height":height}); if($("#pic_play>li>a").size()>0){ $("#pic_play>li>a").css("display","block"); } if($("#pic_play>li>a>img").size()>0){ $("#pic_play>li>a>img").css({"display":"block","width":"100%","height":"100%"}); }else if($("#pic_play>li>img").size()>0){ $("#pic_play>li>img").css({"display":"block","width":"100%","height":"100%"}); }else return; var len=$("#pic_play>li").size(); jsonstyle=[]; var display=-1; var minopacity=minopacity; var opacitystep=(1-minopacity)/(Math.floor(len/2)-1); var minscale=minscale; var scalestep=(1-minscale)/(Math.floor(len/2)); var Dvalue=Math.round((($("#pic_play").innerWidth()-$("#pic_play>li").eq(0).outerWidth())/2)/Math.floor(len/2)); $("#pic_play>li").each(function(i){ if(i<Math.floor(len/2)){ var realoff=i*Dvalue; display++; var realwidth=Math.round(minscale*$("#pic_play>li").eq(i).outerWidth()); var realheight=Math.round(minscale*$("#pic_play>li").eq(i).outerHeight()); var realtop=Math.round(($("#pic_play>li").eq(Math.floor(len/2)).outerHeight()-realheight)/2); jsonstyle[i]={"realOff":realoff,"realIndex":display,"realOpacity":minopacity,"realWidth":realwidth,"realHeight":realheight,"realTop":realtop}; $("#pic_play>li").eq(i).css({"left":realoff,"z-index":display,"opacity":minopacity,"width":realwidth,"height":realheight,"top":realtop}); minopacity+=opacitystep; if(minopacity>=1){ minopacity=1.0; } minscale+=scalestep; }else if(i==Math.floor(len/2)){ display++ var realwidth=Math.round(minscale*$("#pic_play>li").eq(i).outerWidth()); var realheight=Math.round(minscale*$("#pic_play>li").eq(i).outerHeight()); var realoff=Math.round(($("#pic_play").innerWidth()-$("#pic_play>li").eq(i).outerWidth())/2); jsonstyle[i]={"realOff":realoff,"realIndex":display,"realOpacity":1,"realWidth":realwidth,"realHeight":realheight,"realTop":0}; $("#pic_play>li").eq(i).css({"left":realoff,"z-index":display,"opacity":1,"width":realwidth,"height":realheight,"top":0}); }else{ display--; minscale-=scalestep; var realwidth=Math.round(minscale*$("#pic_play>li").eq(i).outerWidth()); var realheight=Math.round(minscale*$("#pic_play>li").eq(i).outerHeight()); var realoff=Math.round($("#pic_play").innerWidth()-(realwidth+(len-1-i)*Dvalue)); var realtop=Math.round(($("#pic_play>li").eq(Math.floor(len/2)).outerHeight()-realheight)/2); jsonstyle[i]={"realOff":realoff,"realIndex":display,"realOpacity":minopacity,"realWidth":realwidth,"realHeight":realheight,"realTop":realtop}; $("#pic_play>li").eq(i).css({"left":realoff,"z-index":display,"opacity":minopacity,"width":realwidth,"height":realheight,"top":realtop}); minopacity-=opacitystep; } }); animationPlay(direction,speed,delayed); if(mousestop==true){ animationStop(direction,speed,delayed); } } //offset:左右按鈕分別距離ul左右邊距(number), //top:左右按鈕距離ul上邊距(number), //direction:默認(rèn)輪播方向("left","right"),一般和init里的一樣,如果你不想點(diǎn)擊后改變運(yùn)動(dòng)方向, //speed:動(dòng)畫(huà)速度(number),一般和init里的一樣,如果你不想點(diǎn)擊后改變動(dòng)畫(huà)速度, //delayed:每張圖片延時(shí)停留時(shí)間(number)(注:delayed>=speed),一般和init里的一樣,如果你不想點(diǎn)擊后改變延時(shí)停留時(shí)間, function btn(offset,top,direction,speed,deleyed){ var leftbtn=$("<span></span>"); leftbtn.css({"width":32,"height":32,"display":"inline-block","position":"absolute","left":offset,"top":top,"background":"url(img/slider-arrow.png) no-repeat -100px 0","cursor":"pointer","z-index":100}); var rightbtn=$("<span></span>"); rightbtn.css({"width":32,"height":32,"display":"inline-block","position":"absolute","right":offset,"top":top,"background":"url(img/slider-arrow.png) no-repeat 0 0","cursor":"pointer","z-index":100}); $("#pic_play").append(leftbtn); $("#pic_play").append(rightbtn); leftbtn.hover(function(){ $(this).css("background-position","-160px 0"); },function(){ $(this).css("background-position","-100px 0"); }); rightbtn.hover(function(){ $(this).css("background-position","-60px 0"); },function(){ $(this).css("background-position","0 0"); }); leftbtn.click(function(){ clearInterval(timeplay); $("#pic_play>li").stop(true); var li=$("#pic_play>li").first(); $("#pic_play").append(li); $("#pic_play>li").each(function(list){ $("#pic_play>li").eq(list).css("z-index",jsonstyle[list]["realIndex"]).animate({"left":jsonstyle[list]["realOff"],"opacity":jsonstyle[list]["realOpacity"],"width":jsonstyle[list]["realWidth"],"height":jsonstyle[list]["realHeight"],"top":jsonstyle[list]["realTop"]},speed); }); animationPlay(direction,speed,deleyed); }); rightbtn.click(function(){ clearInterval(timeplay); $("#pic_play>li").stop(true); var li=$("#pic_play>li").last(); $("#pic_play").prepend(li); $("#pic_play>li").each(function(list){ $("#pic_play>li").eq(list).css("z-index",jsonstyle[list]["realIndex"]).animate({"left":jsonstyle[list]["realOff"],"opacity":jsonstyle[list]["realOpacity"],"width":jsonstyle[list]["realWidth"],"height":jsonstyle[list]["realHeight"],"top":jsonstyle[list]["realTop"]},speed); }); animationPlay(direction,speed,deleyed); }); } //JSON動(dòng)畫(huà) function animationPlay(direction,speed,deleyed){ timeplay=setInterval(function(){ if(direction.toLowerCase()=="left"){ var li=$("#pic_play>li").first(); $("#pic_play").append(li); }else if(direction.toLowerCase()=="right"){ var li=$("#pic_play>li").last(); $("#pic_play").prepend(li); }else return; $("#pic_play>li").each(function(list){ $("#pic_play>li").eq(list).css("z-index",jsonstyle[list]["realIndex"]).animate({"left":jsonstyle[list]["realOff"],"opacity":jsonstyle[list]["realOpacity"],"width":jsonstyle[list]["realWidth"],"height":jsonstyle[list]["realHeight"],"top":jsonstyle[list]["realTop"]},speed); }); },deleyed); } function animationStop(direction,speed,delayed){ $("#pic_play").mouseenter(function(){ clearInterval(timeplay); }); $("#pic_play").mouseleave(function(){ animationPlay(direction,speed,delayed) }); }
腳本的調(diào)用方法,調(diào)用一個(gè)init函數(shù)初始化,定義的參數(shù)意義是:自定父節(jié)點(diǎn)ul總寬度(number),自定ul和圖片的初始化最大高度(中間那圖)(number),圖片的初始化最大寬度(中間那圖)(number),圖片最小透明度(0~1),圖片最小縮放系數(shù)(0~1),默認(rèn)輪播方向("left","right"),動(dòng)畫(huà)速度ms(number),輪播圖片延時(shí)停留時(shí)間ms(number)(注:這個(gè)參數(shù)必須大于動(dòng)畫(huà)速度),鼠標(biāo)經(jīng)過(guò)是否停止(false,true)。
$(function(){ init(1300,600,600,0.7,0.5,"right",500,3000,false); });
為了運(yùn)行效果更為顯眼,給ul加個(gè)邊框居中吧。
#pic_play{ position: relative; border: 1px solid black; margin: 30px auto; }
看以下運(yùn)行效果:這里其實(shí)每張圖的大小都不一樣啊,都可以自適應(yīng)輪播了
大家會(huì)發(fā)現(xiàn)沒(méi)有左右切換的按鈕?沒(méi)關(guān)系,我們可以調(diào)用btn()函數(shù)。btn里面的參數(shù)意義分別是:左右按鈕分別距離ul左右邊距(number),左右按鈕距離ul上邊距(number),默認(rèn)輪播方向("left","right")一般和init里的一樣如果你不想點(diǎn)擊后改變運(yùn)動(dòng)方向,動(dòng)畫(huà)速度ms(number)一般和init里的一樣如果你不想點(diǎn)擊后改變動(dòng)畫(huà)速度,每張圖片延時(shí)停留時(shí)間ms(number)(注:這個(gè)參數(shù)必須大于動(dòng)畫(huà)速度)一般和init里的一樣如果你不想點(diǎn)擊后改輪播停留時(shí)間。
$(function(){ init(1000,460,460,0.7,0.5,"right",500,3000,false); btn(40,230,"right",500,3000); });
下面來(lái)看效果:
如果想用自己的按鈕款式,可以直接在我的btn封裝函數(shù)里面換啊。
附:這里給出了一個(gè)完整測(cè)試實(shí)例(圖片替換成了養(yǎng)眼的美女圖片),可點(diǎn)擊此處本站下載。
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《jQuery圖片操作技巧大全》、《jQuery表格(table)操作技巧匯總》、《jQuery切換特效與技巧總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery常見(jiàn)經(jīng)典特效匯總》及《jquery選擇器用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
新聞標(biāo)題:jQuery實(shí)現(xiàn)經(jīng)典的網(wǎng)頁(yè)3D輪播圖封裝功能【附源碼下載】
網(wǎng)頁(yè)地址:http://aaarwkj.com/article40/ipddho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、軟件開(kāi)發(fā)、商城網(wǎng)站、自適應(yīng)網(wǎng)站、建站公司、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)