jQuery 寫輪播圖

jQuery 簡潔的代碼簡化了輪播圖的編寫, 效果如下:

輪播圖.png

html 部分:

<div class="container">
    <div class="box">
        <img src="img/1.jpg" alt="">
        <img src="img/2.jpg" alt="">
        <img src="img/3.jpg" alt="">
        <img src="img/4.jpg" alt="">
    </div>
    <div class="dots">
        <span class="active"></span>
        <span></span>
        <span></span>
        <span></span>
    </div>
    <div class="arrow arrow-left"><</div>
    <div class="arrow arrow-right">></div>
</div>

css 部分:

        .container{
            width: 600px;
            height: 400px;
            margin: 0 auto;
            position: relative;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .container img{
            width: 600px;
            height: 400px;
            display: none;
        }
        .dots{
            width: 200px;
            height: 10px;
            position: absolute;
            bottom: 10px;
            display: flex;
            justify-content: center;
            flex: 1;
        }
        .dots span{
            width: 10px;
            height: 10px;
            border-radius: 50%;
            margin: 0 10px;
            background-color: #fff;
        }
        .dots span.active{
            background-color: red;
        }
        .arrow{
            width: 20px;
            height: 50px;
            position: absolute;
            background-color: #FFFF00;
            color: black;
            text-align: center;
            line-height: 50px;
            font-size: 30px;
            font-weight: bold;
            cursor: pointer;
        }
        .arrow.arrow-left{
            left: -30px;
        }
        .arrow.arrow-right{
            right: -30px;
        }

JS 部分:

   $(function () {
        let i = 0;
        let timer;
    
        function runStyle(i) {   // 圖片切換效果
            $('.box img').eq(i).show().siblings().hide();
            $('.dots span').eq(i).addClass('active').siblings('.active').removeClass('active');
        }

        function run(){  // 自動切換
            timer = setInterval(function () {
                if (++i === 4){i = 0}
                runStyle(i);
            }, 1000);
        }
        run();


        $('.arrow-left').click(function () {  // 左箭頭
            clearInterval(timer);
            i--;
            if (i<0){i = 3}
            runStyle(i);
            run();
        });

        $('.arrow-right').click(function () {  // 右箭頭
            clearInterval(timer);
            i++;
            if (i > 3){i = 0}
            runStyle(i);
            run();
        });

        $('.dots span').mouseenter(function () {  // 鼠標移動切換
            clearInterval(timer);
            i = $(this).index();
            runStyle(i);
            run();
        })

    })

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

友情鏈接更多精彩內(nèi)容