1. 程式人生 > 程式設計 >純js實現輪播圖效果

純js實現輪播圖效果

本文例項為大家分享了實現輪播圖效果的具體程式碼,供大家參考,具體內容如下

結合我們前面學過的:滑鼠監聽事件(移入移出、點選),建立節點,排他思想、定時器等,就可以實現一個能手動和自動播放的輪播圖

效果圖

純js實現輪播圖效果

程式碼

1.

 /*清除元素預設的內外邊距  */
* {
    margin: 0;
    padding: 0
}
body{
    width: 1000px;
    margin: 0 auto;
}
/*去掉列表前面的小點*/
li {
    list-style: none;
}
/*圖片沒有邊框   去掉圖片底側的空白縫隙*/
imgcQsEqlgXy {
    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,.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,0.5);
    margin: 0 3px;
    border-radius: 50%;
    /*滑鼠經過顯示小手*/
    cursor: pointer;
}

.current {
    background-color: #fff;
}

2. html

<div class="focus fl">
    <!-- 左側按鈕 -->
    <a href=":;" class="arrow-l arrow"> < </a>
    <!-- 右側按鈕 -->
    <a href="script:;" class="arrow-r arrow"> > </a>
    <!-- 核心的滾動區域 -->
    <ul>
        <li>
            <a href="#" ><img src="images/focus.jpg" alt="純js實現輪播圖效果"></a>
        </li>
        <li>
            <a href="#" ><img src="images/focus1.jpg" alt="純js實現輪播圖效果"></a>
        </li>
        <li>
            <a href="#" ><img src="images/focus2.jpg" alt="純js實現輪播圖效果"></a>
        </li>
        <li>
            <a href="#" ><img src="images/focus3.jpg" alt="純js實現輪播圖效果"></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. 滑鼠經過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() {
            //手動呼叫點選事件
            arrow_r.click();
        },2000);
    });
    // 3. 動態生成小圓圈  有幾張圖片,我就生成幾個小圓圈
    var ul = focus.querySelector('ul');
    var ol = focus.querySelector('.circle');
    // console.log(ul.children.length);
    for (var i = 0; i < ul.children.length; i++) {
        // 建立一個小li 
        var li = document.createElement('li');
        // 記錄當前小圓圈的索引號 通過自定義屬性來做 
        li.setAttribute('index',i);
        // 把小li插入到ol 裡面
        ol.appendChild(li);
        // 4. 小圓圈的排他思想 我們可以直接在生成小圓圈的同時直接繫結點www.cppcns.com
擊事件 li.addEventListener('click',function() { // 幹掉所有人 把所有的小li 清除 current 類名 for (var i = 0; i < ol.children.length; i++) { http://www.cppcns.comol.children[i].className = ''; } // 留下我自己 當前的小li 設定current 類名 this.className = 'current'; // 5. 點選小圓圈,移動圖片 當然移動的是 ul // ul 的移動距離 小圓圈的索引號 乘以 圖片的寬度 注意是負值 // 當我們點選了某個小li 就拿到當前小li 的索引號 var index = this.getAttribute('index'); // 當我們點選了某個小li 就要把這個li 的索引號給 num num =http://www.cppcns.com 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. 點選右側按鈕, 圖片滾動一張 var num = 0; // circle 控制小圓圈的播放 var circle = 0; // flag 節流閥 var flag = true; arrow_r.addEventListener('click',function() { if (flag) { flag = false; // 關閉節流閥 // 如果走到了最後複製的一張圖片,此時 我們的ul 要快速復原 left 改為 0 if (num == ul.children.length - 1) { ul.style.left = 0; num = 0; } num++; animate(ul,-num * focusWidth,function() { flag = true; // 開啟節流閥 }); // 8. 點選右側按鈕,小圓圈跟隨一起變化 可以再宣告一個變數控制小圓圈的播放 circle++; // 如果circle == 4 說明走到最後我們克隆的這張圖片了 我們就復原 if (circle == ol.children.length) { circle = 0; } // 呼叫函式 circleChange(); } }); // 9. 左側按鈕做法 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,function() { flag = true; }); // 點選左側按鈕,小圓圈跟隨一起變化 可以再宣告一個變數控制小圓圈的播放 circle--; // 如果circle < 0 說明第一張圖片,則小圓圈要改為第4個小圓圈(3) // if (circle < 0) { // circle = ol.children.length - 1; // } circle = circle < 0 ? ol.children.length - 1 : circle; // 呼叫函式 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() { //手動呼叫點選事件 arrow_r.click(); },2000); })

重點!!!

用到的實現圖片移動的動畫檔案,animate.js

function animate(obj,target,callback) {
    // console.log(callback);  callback = function() {}  呼叫的時候 callback()

    // 先清除以前的定時器,只保留當前的一個定時器執行
    clearInterval(obj.timer);
    obj.timer = setInterval(function() {
        // 步長值寫到定時器的裡面
        // 把我們步長值改為整數 不要出現小數的問題
        // 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) {
            // 停止動畫 本質是停止定時器
            clearInterval(obj.timer);
            // 回撥函式寫到定時器結束裡面
            // if (callback) {
            //     // 呼叫函式
            //     callback();
            // }
            callback && callback();
        }
        // 把每次加1 這個步長值改為一個慢慢變小的值  步長公式:(目標值 - 現在的位置) / 10
        obj.style.left = obj.offsetLeft + step + 'px';

    },15);
}

以上就是www.cppcns.com本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。