本文實(shí)例講述了jquery實(shí)現(xiàn)垂直無(wú)限輪播的方法。分享給大家供大家參考,具體如下:
javascript垂直輪播,依賴于jquery實(shí)現(xiàn)的,并且首尾無(wú)縫銜接。原理很簡(jiǎn)單,就不講述了,直接貼源碼。
1.HTML節(jié)點(diǎn)
<div class="banner_group"> <ul id="banner"> <!-- 緩存末項(xiàng),實(shí)現(xiàn)滑動(dòng)到最開始后,無(wú)限輪播 --> <li style="background-color: chartreuse">第四頁(yè)</li> <li style="background-color: #f6894d">第一頁(yè)</li> <li style="background-color: royalblue">第二頁(yè)</li> <li style="background-color: red">第三頁(yè)</li> <li style="background-color: chartreuse">第四頁(yè)</li> <!-- 緩存首項(xiàng),實(shí)現(xiàn)滑動(dòng)到最后過(guò)后,無(wú)限輪播 --> <li style="background-color: #f6894d">第一頁(yè)</li> </ul> <div class="scrollPageBtn"> <div style="width: 100%;height: 100%;position: relative;"> <label id="last" style="width:100%;position: absolute;top: 0;text-align: center">↑</label> <label id="next" style="width:100%;position: absolute;bottom: 0;text-align: center">↓</label> </div> </div></div>
2.CSS樣式
<style> body{ margin:0; padding: 0; } .banner_group{ width: 300px; height: 500px; overflow: hidden; position: relative; } .scrollPageBtn{ width: 30px; height: 100%; position: absolute; top: 0; left: 40%; background-color: #b2b2b2; opacity: 0.2; } ul{ list-style: none; width: 100%; height: 100%; margin: 0; padding: 0; position: relative; } ul li{ width: 100%; height: 100%; color: white; font-size: 25px; }</style>
3.JQuery準(zhǔn)備
<!-- 引入jquery --><script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
4.JavaScript代碼
<script> var index = 0; // 保存當(dāng)前所在項(xiàng) /* 是否允許點(diǎn)擊滑動(dòng)動(dòng)畫,如果正在執(zhí)行動(dòng)畫的過(guò)程中, 則禁止點(diǎn)擊,如果動(dòng)畫完成后,則允許點(diǎn)擊, 避免由于連點(diǎn),出現(xiàn)畫面不正常問題. */ var allowClick = true; // // 頁(yè)面加載完成后調(diào)用 $(function(){ index = 1; // 初始顯示第2項(xiàng) /* 注意:第一項(xiàng)是用來(lái)緩存末項(xiàng)的,實(shí)現(xiàn)無(wú)縫連接準(zhǔn)備的,所以最開始顯示的應(yīng)該是第2項(xiàng) */ $("#banner").css("bottom", "500px"); // 準(zhǔn)備初始顯示項(xiàng) // 上一頁(yè) $("#last").on("click", function(){ if(allowClick){ allowClick = false; index--; // 上一頁(yè),-- // 如果已經(jīng)到了最開始過(guò)后,動(dòng)畫完成后,定位到末項(xiàng) if(index == 0){ $("#banner").animate({bottom: (index * 500) + 'px'}, "fast", "swing", function () { index = 4; $("#banner").css("bottom", "2000px"); // 定位到末項(xiàng) allowClick = true; }); }else{ $("#banner").animate({bottom: (index * 500) + 'px'}, "fast", "swing", function () { allowClick = true; }); } } }); // 下一頁(yè) $("#next").on("click", function(){ if(allowClick){ allowClick = false; if(index <= 5){ index ++; // 下一頁(yè)++ if(index == 5){ $("#banner").animate({bottom: (index * 500) + 'px'}, "fast", "swing", function () { index = 1; $("#banner").css("bottom", "500px"); allowClick = true; }); }else{ $("#banner").animate({bottom: (index * 500) + 'px'}, "fast", "swing", function () { allowClick = true; }); } } } }); });</script>
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery圖片操作技巧大全》、《jQuery表格(table)操作技巧匯總》、《jQuery切換特效與技巧總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery常見經(jīng)典特效匯總》及《jquery選擇器用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注