jquery實現(xiàn)垂直無限輪播的方法分析
本文實例講述了jquery實現(xiàn)垂直無限輪播的方法。分享給大家供大家參考,具體如下:
javascript垂直輪播,依賴于jquery實現(xiàn)的,并且首尾無縫銜接。原理很簡單,就不講述了,直接貼源碼。
1.HTML節(jié)點
<div class="banner_group">
<ul id="banner">
<!-- 緩存末項,實現(xiàn)滑動到最開始后,無限輪播 -->
<li style="background-color: chartreuse">第四頁</li>
<li style="background-color: #f6894d">第一頁</li>
<li style="background-color: royalblue">第二頁</li>
<li style="background-color: red">第三頁</li>
<li style="background-color: chartreuse">第四頁</li>
<!-- 緩存首項,實現(xiàn)滑動到最后過后,無限輪播 -->
<li style="background-color: #f6894d">第一頁</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)前所在項
/* 是否允許點擊滑動動畫,如果正在執(zhí)行動畫的過程中,
則禁止點擊,如果動畫完成后,則允許點擊,
避免由于連點,出現(xiàn)畫面不正常問題. */
var allowClick = true; //
// 頁面加載完成后調(diào)用
$(function(){
index = 1; // 初始顯示第2項
/* 注意:第一項是用來緩存末項的,實現(xiàn)無縫連接準(zhǔn)備的,所以最開始顯示的應(yīng)該是第2項 */
$("#banner").css("bottom", "500px"); // 準(zhǔn)備初始顯示項
// 上一頁
$("#last").on("click", function(){
if(allowClick){
allowClick = false;
index--; // 上一頁,--
// 如果已經(jīng)到了最開始過后,動畫完成后,定位到末項
if(index == 0){
$("#banner").animate({bottom: (index * 500) + 'px'}, "fast", "swing", function () {
index = 4;
$("#banner").css("bottom", "2000px"); // 定位到末項
allowClick = true;
});
}else{
$("#banner").animate({bottom: (index * 500) + 'px'}, "fast", "swing", function () {
allowClick = true;
});
}
}
});
// 下一頁
$("#next").on("click", function(){
if(allowClick){
allowClick = false;
if(index <= 5){
index ++; // 下一頁++
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é)》
希望本文所述對大家jQuery程序設(shè)計有所幫助。
相關(guān)文章
全面解析jQuery $(document).ready()和JavaScript onload事件
這篇文章主要介紹了全面解析jQuery $(document).ready()和JavaScript onload事件的相關(guān)資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-06-06
jQuery+css實現(xiàn)百度百科的頁面導(dǎo)航效果
這篇文章主要介紹了jQuery+css實現(xiàn)百度百科的頁面導(dǎo)航效果,需要的朋友可以參考下2014-12-12

