溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

純js如何實現(xiàn)輪播圖效果

發(fā)布時間:2021-10-27 13:36:17 來源:億速云 閱讀:128 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹純js如何實現(xiàn)輪播圖效果,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

效果圖

純js如何實現(xiàn)輪播圖效果

代碼

1. css

 /*清除元素默認的內(nèi)外邊距  */
* {
    margin: 0;
    padding: 0
}
body{
    width: 1000px;
    margin: 0 auto;
}
/*去掉列表前面的小點*/
li {
    list-style: none;
}
/*圖片沒有邊框   去掉圖片底側(cè)的空白縫隙*/
img {
    border: 0;  /*ie6*/
    vertical-align: middle;
}
/*取消鏈接的下劃線*/
a {
    color: #666;
    text-decoration: none;
}

a:hover {
    color: #e33333;
}
.fl {
    float: left;
}
.fr {
    float: right;
}
.focus {
    position: relative;
    width: 721px;
    height: 455px;
    background-color: purple;
    overflow: hidden;
    margin-top: 20px;
}

.focus ul {
    position: absolute;
    top: 0;
    left: 0;
    width: 600%;
}

.focus ul li {
    float: left;
}

.arrow-l,
.arrow-r {
    display: none;
    position: absolute;
    top: 50%;
    margin-top: -20px;
    width: 24px;
    height: 40px;
    background: rgba(0, 0, 0, .3);
    text-align: center;
    line-height: 40px;
    color: #fff;
    font-family: 'icomoon';
    font-size: 18px;
    z-index: 2;
}

.arrow-r {
    right: 0;
}

.circle {
    position: absolute;
    bottom: 10px;
    left: 50px;
}

.circle li {
    float: left;
    width: 8px;
    height: 8px;
    /*background-color: #fff;*/
    border: 2px solid rgba(255, 255, 255, 0.5);
    margin: 0 3px;
    border-radius: 50%;
    /*鼠標經(jīng)過顯示小手*/
    cursor: pointer;
}

.current {
    background-color: #fff;
}

2. html

<div class="focus fl">
    <!-- 左側(cè)按鈕 -->
    <a href="javascript:;" class="arrow-l arrow"> < </a>
    <!-- 右側(cè)按鈕 -->
    <a href="javascript:;" class="arrow-r arrow"> > </a>
    <!-- 核心的滾動區(qū)域 -->
    <ul>
        <li>
            <a href="#" ><img src="images/focus.jpg" alt=""></a>
        </li>
        <li>
            <a href="#" ><img src="images/focus1.jpg" alt=""></a>
        </li>
        <li>
            <a href="#" ><img src="images/focus2.jpg" alt=""></a>
        </li>
        <li>
            <a href="#" ><img src="images/focus3.jpg" alt=""></a>
        </li>
    </ul>
    <!-- 小圓圈 -->
    <ol class="circle">

    </ol>
</div>

3. JavaScript

window.addEventListener('load', function() {
    // 1. 獲取元素
    var arrow_l = document.querySelector('.arrow-l');
    var arrow_r = document.querySelector('.arrow-r');
    var focus = document.querySelector('.focus');
    var focusWidth = focus.offsetWidth;
    // 2. 鼠標經(jīng)過focus 就顯示隱藏左右按鈕
    focus.addEventListener('mouseenter', function() {
        arrow_l.style.display = 'block';
        arrow_r.style.display = 'block';
        clearInterval(timer);
        timer = null; // 清除定時器變量
    });
    focus.addEventListener('mouseleave', function() {
        arrow_l.style.display = 'none';
        arrow_r.style.display = 'none';
        timer = setInterval(function() {
            //手動調(diào)用點擊事件
            arrow_r.click();
        }, 2000);
    });
    // 3. 動態(tài)生成小圓圈  有幾張圖片,我就生成幾個小圓圈
    var ul = focus.querySelector('ul');
    var ol = focus.querySelector('.circle');
    // console.log(ul.children.length);
    for (var i = 0; i < ul.children.length; i++) {
        // 創(chuàng)建一個小li 
        var li = document.createElement('li');
        // 記錄當前小圓圈的索引號 通過自定義屬性來做 
        li.setAttribute('index', i);
        // 把小li插入到ol 里面
        ol.appendChild(li);
        // 4. 小圓圈的排他思想 我們可以直接在生成小圓圈的同時直接綁定點擊事件
        li.addEventListener('click', function() {
            // 干掉所有人 把所有的小li 清除 current 類名
            for (var i = 0; i < ol.children.length; i++) {
                ol.children[i].className = '';
            }
            // 留下我自己  當前的小li 設置current 類名
            this.className = 'current';
            // 5. 點擊小圓圈,移動圖片 當然移動的是 ul 
            // ul 的移動距離 小圓圈的索引號 乘以 圖片的寬度 注意是負值
            // 當我們點擊了某個小li 就拿到當前小li 的索引號
            var index = this.getAttribute('index');
            // 當我們點擊了某個小li 就要把這個li 的索引號給 num  
            num = index;
            // 當我們點擊了某個小li 就要把這個li 的索引號給 circle  
            circle = index;
            // num = circle = index;
            console.log(focusWidth);
            console.log(index);

            animate(ul, -index * focusWidth);
        })
    }
    // 把ol里面的第一個小li設置類名為 current
    ol.children[0].className = 'current';
    // 6. 克隆第一張圖片(li)放到ul 最后面
    var first = ul.children[0].cloneNode(true);
    ul.appendChild(first);
    // 7. 點擊右側(cè)按鈕, 圖片滾動一張
    var num = 0;
    // circle 控制小圓圈的播放
    var circle = 0;
    // flag 節(jié)流閥
    var flag = true;
    arrow_r.addEventListener('click', function() {
        if (flag) {
            flag = false; // 關(guān)閉節(jié)流閥
            // 如果走到了最后復制的一張圖片,此時 我們的ul 要快速復原 left 改為 0
            if (num == ul.children.length - 1) {
                ul.style.left = 0;
                num = 0;
            }
            num++;
            animate(ul, -num * focusWidth, function() {
                flag = true; // 打開節(jié)流閥
            });
            // 8. 點擊右側(cè)按鈕,小圓圈跟隨一起變化 可以再聲明一個變量控制小圓圈的播放
            circle++;
            // 如果circle == 4 說明走到最后我們克隆的這張圖片了 我們就復原
            if (circle == ol.children.length) {
                circle = 0;
            }
            // 調(diào)用函數(shù)
            circleChange();
        }
    });

    // 9. 左側(cè)按鈕做法
    arrow_l.addEventListener('click', function() {
        if (flag) {
            flag = false;
            if (num == 0) {
                num = ul.children.length - 1;
                ul.style.left = -num * focusWidth + 'px';

            }
            num--;
            animate(ul, -num * focusWidth, function() {
                flag = true;
            });
            // 點擊左側(cè)按鈕,小圓圈跟隨一起變化 可以再聲明一個變量控制小圓圈的播放
            circle--;
            // 如果circle < 0  說明第一張圖片,則小圓圈要改為第4個小圓圈(3)
            // if (circle < 0) {
            //     circle = ol.children.length - 1;
            // }
            circle = circle < 0 ? ol.children.length - 1 : circle;
            // 調(diào)用函數(shù)
            circleChange();
        }
    });

    function circleChange() {
        // 先清除其余小圓圈的current類名
        for (var i = 0; i < ol.children.length; i++) {
            ol.children[i].className = '';
        }
        // 留下當前的小圓圈的current類名
        ol.children[circle].className = 'current';
    }
    // 10. 自動播放輪播圖
    var timer = setInterval(function() {
        //手動調(diào)用點擊事件
        arrow_r.click();
    }, 2000);

})

重點?。?!

用到的實現(xiàn)圖片移動的動畫文件,animate.js

function animate(obj, target, callback) {
    // console.log(callback);  callback = function() {}  調(diào)用的時候 callback()

    // 先清除以前的定時器,只保留當前的一個定時器執(zhí)行
    clearInterval(obj.timer);
    obj.timer = setInterval(function() {
        // 步長值寫到定時器的里面
        // 把我們步長值改為整數(shù) 不要出現(xiàn)小數(shù)的問題
        // var step = Math.ceil((target - obj.offsetLeft) / 10);
        var step = (target - obj.offsetLeft) / 10;
        step = step > 0 ? Math.ceil(step) : Math.floor(step);
        if (obj.offsetLeft == target) {
            // 停止動畫 本質(zhì)是停止定時器
            clearInterval(obj.timer);
            // 回調(diào)函數(shù)寫到定時器結(jié)束里面
            // if (callback) {
            //     // 調(diào)用函數(shù)
            //     callback();
            // }
            callback && callback();
        }
        // 把每次加1 這個步長值改為一個慢慢變小的值  步長公式:(目標值 - 現(xiàn)在的位置) / 10
        obj.style.left = obj.offsetLeft + step + 'px';

    }, 15);
}

以上是“純js如何實現(xiàn)輪播圖效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

js
AI