JavaScript—轮播图

概念

轮播图(Carousel)是一种常见的网页设计元素,用于展示多张图片或信息。它通常由一个容器和一组水平排列的图片或内容组成。轮播图中的图片会按照一定的规律(例如自动轮播、点击切换或滑动切换等)进行切换,以便在有限的空间内展示多个内容。

HTML元素

网页元素

<div class="wrap">
        <ul class="list">
            <li class="item active">0</li>
            <li class="item">1</li>
            <li class="item">2</li>
            <li class="item">3</li>
            <li class="item">4</li>
        </ul>
        <ul class="pointlist">
            <li class="point active"></li>
            <li class="point"></li>
            <li class="point"></li>
            <li class="point"></li>
            <li class="point"></li>
        </ul>
        <button type="button" class="btn" id="gopre">&lt;</button>
        <button type="button" class="btn" id="gonext">&gt;</button>
    </div>

CSS样式

样式布局
         * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        li {
            list-style: none;
        }

        .wrap {
            width: 800px;
            height: 400px;
            background: pink;
            position: relative;
        }

        .item {
            width: 100%;
            height: 400px;
            background: red;
            position: absolute;
            opacity: 0;
            font-size: 100px;
        }

        .item.active {
            opacity: 1;
            z-index: 1;
        }

        .item:nth-child(1) {
            background-color: blueviolet;
        }

        .item:nth-child(2) {
            background-color: red;
        }

        .item:nth-child(3) {
            background-color: goldenrod;
        }

        .item:nth-child(4) {
            background-color: green;
        }

        .item:nth-child(5) {
            background-color: pink;
        }

        .pointlist {
            width: 110px;
            position: absolute;
            right: 50%;
            margin-right: -55px;
            z-index: 2;
            bottom: 20px;
            background: red;
        }

        .point {
            width: 8px;
            height: 8px;
            background-color: rgba(255, 255, 255, 1);
            border-radius: 50%;
            float: left;
            margin-right: 14px;
            border-style: solid;
            border-width: 2px;
            border-color: rgba(255, 255, 255, 0.6);
            cursor: pointer;
        }

        .point.active {
            background: #000;
        }

        .btn {
            width: 50px;
            height: 100px;
            position: absolute;
            top: 150px;
            z-index: 3;
        }

        #gopre {
            left: 0;
        }

        #gonext {
            right: 0;
        }

js功能

脚本功能
        let items = document.querySelectorAll(".item");
        let points = document.querySelectorAll(".point");

        let time = 0;
        let index = 0;

        var goprebtn = document.getElementById('gopre');
        var gonextbtn = document.getElementById('gonext');

        // 遍历轮播图数组与point数组并恢复到默认状态取消它们的活动状态。
        let clearactive = function () {
            for (let i = 0; i < items.length; i++) {
                items[i].className = "item";
            }
            for (let i = 0; i < points.length; i++) {
                points[i].className = "point";
            }
        };
        // 将对应索引的数组元素添加一个active类,以将其设置为活动状态。
        let goindex = function () {
            clearactive();
            points[index].classList.add('active');
            items[index].classList.add('active');
        };
        // 点击下一个按钮:判断索引是否为最后一个,是则跳到第一个,否则索引加一后跳转
        let gonext = function () {
            if (index === points.length - 1) {
                index = 0;
            } else {
                index++;
            }
            goindex();
        };
        // 点击上一个按钮与上同理
        var gopre = function () {
            if (index === 0) {
                index = points.length - 1;
            } else {
                index--;
            }
            goindex();
        };
        // 为每个点元素添加点击功能,当用户点击不同的点时,可以更新对应的项和点的活动状态。
        for (let i = 0; i < points.length; i++) {
            points[i].addEventListener('click', function () {
                index = Array.prototype.indexOf.call(points, this);
                goindex();
                time = 0;
            });
        }
        // 添加监听
        gonextbtn.addEventListener('click', function () {
            gonext();
        });

        goprebtn.addEventListener('click', function () {
            gopre();
        });
        setInterval(function () {
            time++;
            if (time == 20) {
                gonext();
                time = 0;
            }
        }, 100)//2000ms跳转一次

 

posted @ 2023-09-01 19:28  Dr丶云幕  阅读(15)  评论(0编辑  收藏  举报