JS轮播图(左右箭头切换、按钮切换、自动轮播)

 const LEFT = Symbol(),
            RIGHT = Symbol(),
            FINISH_TIME = 200;
        var arr = [
            {
                date: "22/Feb.2022",
                title: "2021旅行记忆|一起点亮这个世界的1%",
                src: "./img/a.jpg",
            },
            {
                date: "21/Feb.2022",
                title: "我是周末种草官|天堂应是书店和图书馆的模样",
                src: "./img/b.jpg",
            },
            {
                date: "20/Feb.2022",
                title: "【欧亚行迹】土耳其回忆录",
                src: "./img/c.jpg",
            },
            {
                date: "19/Feb.2022",
                title: "我的2021年 | 心怀远方,热爱可抵岁月漫长",
                src: "./img/d.jpg",
            },
            {
                date: "18/Feb.2022",
                title: "哈尔滨 | 有一场看雪的跨年仪式感",
                src: "./img/e.jpg",
            },
        ];
        var list = [],
            pos = 0,
            direction = LEFT,
            x = 0,
            bool = false,
            speed = 50,
            autoBool = false, //false表示鼠标还未离开
            time = FINISH_TIME;
        var imgCon, carousel, prev;
        init();

        function init() {
            carousel = document.createElement("div");
            carousel.className = "carousel";
            carousel.innerHTML = `
            <div class='img-con clear'></div>
            <ul class='dot'></ul>
            <img src='./img/left.png' class='left'>
            <img src='./img/right.png' class='right'>
          `;
            document.body.appendChild(carousel);
            createContent();
            carousel.addEventListener("click", clickHandler);
            carousel.addEventListener("mouseenter", mouseHandler);
            carousel.addEventListener("mouseleave", mouseHandler);
            animation();
            changePrev();
        }

        function createContent() {
            imgCon = document.querySelector(".img-con");
            imgCon.nextElementSibling.innerHTML = arr.reduce(
                (v) => v + "<li></li>",
                ""
            );
            imgCon.appendChild(createImg(0));
        }

        function createImg(pos) {
            if (list[pos]) return list[pos];
            var div = document.createElement("div");
            div.className = "img-item";
            div.innerHTML = `
                <div class='title'>
                    <span>${arr[pos].date.match(/^.*(?=\/)/g)[0]}</span>${arr[pos].date.match(/\/.*$/g)[0]
                }
                    <h3>${arr[pos].title}</h3>
                </div>
                <img src='${arr[pos].src}'>
           `;
            list[pos] = div;
            return div;
        }

        function mouseHandler(e) {
            autoBool = e.type === "mouseleave";
            time = FINISH_TIME;
        }

        function clickHandler(e) {
            if (bool) return;
            if (e.target.nodeName !== "LI" && e.target.nodeName !== "IMG") return;
            switch (e.target.className) {
                case "left":
                    pos === 0 ? (pos = arr.length - 1) : pos--;
                    direction = RIGHT;
                    break;
                case "right":
                    pos === arr.length - 1 ? (pos = 0) : pos++;
                    direction = LEFT;
                    break;
                default:
                    if (e.target.nodeName === "IMG") return;
                    var index = Array.from(e.target.parentElement.children).indexOf(
                        e.target
                    );
                    if (index === pos) return;
                    direction = index > pos ? LEFT : RIGHT;
                    pos = index;
            }
            createNextImg();
        }

        function createNextImg() {
            if (direction === LEFT) {
                imgCon.appendChild(createImg(pos));
                x = 0;
            } else if (direction === RIGHT) {
                imgCon.insertBefore(createImg(pos), imgCon.firstChild);
                x = -imgCon.offsetWidth / 2;
            }
            imgCon.style.left = x + "px";
            changePrev();
            bool = true;
        }

        function changePrev() {
            if (prev) {
                prev.style.backgroundColor = "transparent";
            }
            prev = imgCon.nextElementSibling.children[pos];
            prev.style.backgroundColor = "red";
        }

        // 动画效果控制
        function animation() {
            requestAnimationFrame(animation);
            imgMove();
            autoPlay();
        }

        //自动轮播
        function autoPlay() {
            if (!autoBool) return;
            time--;
            if (time > 0) return;
            time = FINISH_TIME;
            var evt = new Event("click", {
                bubbles: true,
            });
            carousel.lastElementChild.dispatchEvent(evt);
        }

        function imgMove() {
            if (!bool) return;
            if (direction === LEFT) {
                x -= speed;
                if (x <= -imgCon.offsetWidth / 2) {
                    bool = false;
                    imgCon.firstElementChild.remove();
                    x = 0;
                }
            } else if (direction === RIGHT) {
                x += speed;
                if (x >= 0) {
                    bool = false;
                    imgCon.lastElementChild.remove();
                    x = 0;
                }
            }
            imgCon.style.left = x + "px";
        }

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值