仿京東首頁輪播圖編寫

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>輪播圖</title>
    <link rel="stylesheet" href="./css/07.css">
</head>

<body>
    <div class="round">
        <!-- 圖片 -->
        <ul class="img">
            <li><img src="./img/23d7dbb1b96a146a.jpg.webp"></li>
            <li><img src="./img/4fe0dcb5ab6dc357.jpg.webp"></li>
            <li><img src="./img/590x470.jpg"></li>
            <li><img src="./img/bb571108797afb8c.jpg.webp"></li>
            <li><img src="./img/fe799a940b971ec9.jpg.webp"></li>
        </ul>
        <!-- 圓點 -->
        <ul class="point">
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>

    <script src="./js/07.js"></script>
    <script>
    new Round('.round').autoplay(3000);
    </script>
</body>
</html>

js編寫

class Round{
    constructor(selector){
        // 事件源
        this.points = document.querySelectorAll(`${selector}>.point>li`);
        // 拿到所有圖片
        this.imgs = document.querySelectorAll(`${selector}>.img>li`);
        // 定時器id
        this.sid = 0;
        // 當前顯示的圖片的id
        this.showid = 0;
        this.init();
        // 當鼠標移入當前區域,自動輪播暫停
        let _this = this;
        document.querySelector(selector).onmouseover = function(){
            clearInterval(_this.sid);
        }
        document.querySelector(selector).onmouseout =  function(){
            console.log(_this.sid);
            // 是否開啓了自動輪播
            if(_this.sid){
                _this.autoplay();
            }
        }
    }
    init(){
        
        // 循環的方式給每個事件源綁定事件
       
        
        let _this = this;
        
        for (let i = 0; i < this.points.length; i++) {
            this.points[i].onclick = function(){
                console.log(i);
                // 隱藏所有的圖片,並把對應的圖片顯示出來
                // Cannot read property 'length' of undefined
                for (let j = 0; j < _this.imgs.length; j++) {
                    _this.imgs[j].style.display = 'none';
                }
                // 需要記錄當前現實的圖片的id
                _this.showid = i;
                _this.imgs[i].style.display = 'block';
            }
        }
    }
    autoplay(step = 1000){
        let _this = this;
        let n = _this.showid;
        this.sid = setInterval(function(){
            _this.points[++n % _this.points.length].click();
        }, step);
    }
}

css編寫

*{
    margin: 0;
    padding: 0;
}
li{
    list-style: none;
}

.round{
    width: 590px;
    height: 470px;
    background-color: #f02;
    margin: 50px auto;
    font-size: 0;
    /* 超出部分隱藏 */
    overflow: hidden;
    /* 子絕父相 */
    position: relative;
}

.round li{
    display: inline-block;
    
}
.point{
    display: flex;
    justify-content: center;
    position: absolute;
    bottom: 10px;
    width: 100%;
    left: 0;
}
.point>li{
    width: 10px;
    height: 10px;
    background-color: #cfcccc;
    border-radius: 50%;
    -webkit-border-radius: 50%;
    -moz-border-radius: 50%;
    -ms-border-radius: 50%;
    -o-border-radius: 50%;
    margin-left: 5px;
    cursor: pointer;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章