1. 程式人生 > >jQuery實現簡單的輪播圖

jQuery實現簡單的輪播圖

jQuery

先看效果,如果是你想要的,可以看看
技術分享圖片
總體思路:

1.自動輪播
2.指定輪播
3.左右翻動


詳細步驟:jQuery部分

  • 設置第一張圖片顯示,其他的兄弟元素隱藏
  • 自動輪播的時候,指定第一張的索引為 i=0,然後 i++, 使其對應的 i 索引的圖片顯示,其他的隱藏,當 i=5(即第六張圖片的時候,使其為第一張即可,否則我們沒有第六張圖片就不顯示了)。
  • 指定圖片輪播,就是鼠標移動到白圈上顯示對應的圖片,並計時器停止,我們可以看看圖片上的內容,有的輪播圖計時器還是繼續的,看不了什麽內容;鼠標離開則繼續輪播。
  • 左右翻動,左就是往左輪播,當 i = -1的時候使其為 4 (應該能知道什麽意思吧,道理同自動輪播的括號裏的內容),往右輪播同左。

好了不多說了,直接上代碼
html

    <div id="banner">
            <div class="pic">
                <div class="picImg"><img src="images/1.jpg" width="520" height="280" /></div>
                <div class="picImg"><img src="images/2.jpg" width="520" height="280" /></div>
                <div class="picImg"><img src="images/3.jpg" width="520" height="280" /></div>
                <div class="picImg"><img src="images/4.jpg" width="520" height="280" /></div>
                <div class="picImg"><img src="images/5.jpg" width="520" height="280" /></div>
            </div>
            <ul class="tabs">
                <li class="bg"></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
            <div class="btn btn1">&lt;</div>
            <div class="btn btn2">&gt;</div>
        </div>

css

*{
    margin:0px;
    padding:0px;
    list-style-type:none;
}
#banner{
    width:520px;
    height:280px;
    position:absolute;
    top:50%;
    margin-top:-140px;
    left:50%;
    margin-left:-260px;
}
.pic img{
    position:absolute;
}
.tabs{
    position:absolute;
    bottom:10px;
    left:200px;
}
.tabs li{
    width:20px;
    height:20px;
    border:2px solid #fff;
    float:left;
    margin-left:5px;
    border-radius:100%;

}
.btn{
    width:30px;
    height:50px;
    background-color:rgba(0,0,0,.5);
    color:#fff;
    font-size:30px;
    line-height:50px;
    text-align:center;
    position:absolute;
    top:50%;
    margin-top:-25px;
    cursor:pointer;
}
.btn:hover{
    background-color:rgba(0,0,0,.8);
    color:red;
}
.btn1{
    left:0;
}
.btn2{
    right:0;
}
.bg{
    background-color:red;
}

js


var i=0;
var Timer;
$(function(){
    $(".picImg").eq(0).show().siblings().hide();   //默認第一張圖片顯示,其他的隱藏
    //自動輪播
    TimerBanner();

    //點擊紅圈

    $(".tabs li").hover(function(){  //鼠標移動上去
        clearInterval(Timer); //讓計時器暫時停止   清除計時器
        i=$(this).index();   //獲取該圈的索引
        showPic();           //調用顯示圖片的方法,顯示該索引對應的圖片
    },function(){  //鼠標離開
        TimerBanner();    //繼續輪播   計時器開始
    });

    //點擊左右按鈕
    $(".btn1").click(function(){
        clearInterval(Timer);
        i--;   //往左
        if(i==-1){
            i=4;
        }
        showPic();
        TimerBanner();
    });
    $(".btn2").click(function(){
        clearInterval(Timer);
        i++;   //往右
        if(i==5){
            i=0;
        }
        showPic();
        TimerBanner();
    });
});
//輪播部分
function TimerBanner(){
    Timer = setInterval(function(){
        i++;
        if(i==5){
            i=0;
        }
        showPic()
    },1000);
}
//顯示圖片
function showPic(){
    $(".picImg").eq(i).show().siblings().hide();
    $(".tabs li").eq(i).addClass("bg").siblings().removeClass("bg");
}

思路也數說了,這個實例很簡單,代碼也有詳細的註釋,有不懂得隨時呼叫我,看到即回,
下載鏈接:
鏈接:https://pan.baidu.com/s/1Ar7vP_cPUAwmw6UipnnzFg 密碼:ypol

jQuery實現簡單的輪播圖