最簡單的jQuery輪播圖(原理解析)


html:

<div class="middle_right">
    <div id="lunbobox">
        <div id="toleft">
            &lt;</div>
                <div class="lunbo">
                    <a href="#"><img src="http://www.jq22.com/img/cs/500x300a.png"></a>
                    <a href="#"><img src="http://www.jq22.com/img/cs/500x300b.png"></a>
                    <a href="#"><img src="http://www.jq22.com/img/cs/500x300c.png"></a>
                    <a href="#"><img src="http://www.jq22.com/img/cs/500x300d.png"></a>
                    <a href="#"><img src="http://www.jq22.com/img/cs/500x300.png"></a>
                </div>
                <div id="toright">&gt;</div>
                <ul>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                </ul>
                <span></span>
        </div>
    </div>

css:

#lunbobox {
    width:500px;
    height:300px;
    position:relative;
}
.lunbo {
    width:500px;
    height:300px;
}
.lunbo img {
    width:500px;
    height:300px;
    position:absolute;
    top:0px;
    left:0px;
}
#lunbobox ul {
    width:285px;
    position:absolute;
    bottom:10px;
    right:0px;
    z-index:5;
}
#lunbobox ul li {
    cursor:pointer;
    width:10px;
    height:4px;
    border:1px solid #cccccc;
    float:left;
    list-style:none;
    background:#cccccc;
    text-align:center;
    margin:0px 5px 0px 0px;
}
#toleft {
    display:none;
    width:30px;
    height:100px;
    font-size:40px;
    line-height:100px;
    text-align:center;
    color:#f4f4f4;
    /*background:#cccccc;
    */
    /*background:url("../images/toleft.jpg")no-repeat center;
    */
    position:absolute;
    top:90px;
    left:12px;
    cursor:pointer;
    z-index:99;
    opacity:0.4;
}
#toright {
    display:none;
    width:30px;
    height:100px;
    font-size:40px;
    line-height:100px;
    text-align:center;
    color:#f4f4f4;
    /*background:#cccccc;
    */
    position:absolute;
    top:90px;
    right:0px;
    cursor:pointer;
    z-index:99;
    opacity:0.4;
}

js:

///輪播
$(function() {
    //$("#toright").hide();
    //$("#toleft").hide();
    $('#toright').hover(function() {
        $("#toleft").hide()
    }, function() {
        $("#toleft").show()
    })
    $('#toleft').hover(function() {
        $("#toright").hide()
    }, function() {
        $("#toright").show()
    })
})

var t;
var index = 0;
/////自動播放
t = setInterval(play, 3000)

function play() {
    index++;
    if (index > 4) {
        index = 0
    }
    // console.log(index)
    $("#lunbobox ul li").eq(index).css({
        "background": "#999",
        "border": "1px solid #ffffff"
    }).siblings().css({
        "background": "#cccccc",
        "border": ""
    })

    $(".lunbo a ").eq(index).fadeIn(1000).siblings().fadeOut(1000);
};

///點擊鼠標 圖片切換
$("#lunbobox ul li").click(function() {

    //添加 移除樣式
    //$(this).addClass("lito").siblings().removeClass("lito"); //給當前鼠標移動到的li增加樣式 且其余兄弟元素移除樣式   可以在樣式中 用hover 來對li 實現
    $(this).css({
        "background": "#999",
        "border": "1px solid #ffffff"
    }).siblings().css({
        "background": "#cccccc"
    })
    var index = $(this).index(); //獲取索引 圖片索引與按鈕的索引是一一對應的
    // console.log(index);

    $(".lunbo a ").eq(index).fadeIn(1000).siblings().fadeOut(1000); // siblings  找到 兄弟節點(不包括自己)
});

/////////////上一張、下一張切換
$("#toleft").click(function() {
    index--;
    if (index <= 0) //判斷index<0的情況為:開始點擊#toright  index=0時  再點擊 #toleft 為負數了 會出錯
    {
        index = 4
    }
    console.log(index);
    $("#lunbobox ul li").eq(index).css({
        "background": "#999",
        "border": "1px solid #ffffff"
    }).siblings().css({
        "background": "#cccccc"
    })

    $(".lunbo a ").eq(index).fadeIn(1000).siblings().fadeOut(1000); // siblings  找到 兄弟節點(不包括自己)必須要寫
}); // $("#imgbox a ")獲取到的是一個數組集合 。所以可以用index來控制切換

$("#toright").click(function() {
    index++;
    if (index > 4) {
        index = 0
    }
    console.log(index);
    $(this).css({
        "opacity": "0.5"
    })
    $("#lunbobox ul li").eq(index).css({
        "background": "#999",
        "border": "1px solid #ffffff"
    }).siblings().css({
        "background": "#cccccc"
    })
    $(".lunbo a ").eq(index).fadeIn(1000).siblings().fadeOut(1000); // siblings  找到 兄弟節點(不包括自己)
});
$("#toleft,#toright").hover(function() {
        $(this).css({
            "color": "black"
        })
    },
    function() {
        $(this).css({
            "opacity": "0.3",
            "color": ""
        })
    })
///

///////鼠標移進  移出
$("#lunbobox ul li,.lunbo a img,#toright,#toleft ").hover(
    ////////鼠標移進
    function() {
        $('#toright,#toleft').show()
        clearInterval(t);

    },
    ///////鼠標移開
    function() {
        //$('#toright,#toleft').hide()
        //alert('aaa')
        t = setInterval(play, 3000)

        function play() {
            index++;
            if (index > 4) {
                index = 0
            }
            $("#lunbobox ul li").eq(index).css({
                "background": "#999",
                "border": "1px solid #ffffff"
            }).siblings().css({
                "background": "#cccccc"
            })
            $(".lunbo a ").eq(index).fadeIn(1000).siblings().fadeOut(1000);
        }
    })

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM