可复用轮播图方法

HTML部分
<div class="banner">
        <!-- 承载所有 li 的 图片大盒子 -->
        <ul class="imgBox">
            <li style="background-color: skyblue;">1</li>
            <li style="background-color: orange;">2</li>
            <li style="background-color: purple;">3</li>
            <li style="background-color: green;">4</li>
            <li style="background-color: pink;">5</li>
        </ul>

        <!-- 承载所有焦点的 焦点大盒子 -->
        <ol class="pointBox"></ol>

        <!-- 左右切换的按钮 -->
        <div class="left">&lt;</div>
        <div class="right">&gt;</div>
    </div>
css部分

        * {
            margin: 0;
            padding: 0;
        }
        
        .banner {
            width: 600px;
            height: 400px;
            border: 2px solid #333;
            border-radius: 10px;
            margin: 50px auto;
            position: relative;
            overflow: hidden;
        }
        
        .banner>.imgBox {
            width: 500%;
            height: 100%;
            position: absolute;
            left: 0;
            top: 0;
            display: flex;
        }
        
        .banner>.imgBox>li {
            flex: 1;
            height: 100%;
            border-radius: 10px;
            overflow: hidden;
            display: flex;
            justify-content: center;
            align-items: center;
            color: #fff;
            font-size: 100px;
            font-weight: 700;
        }
        
        .banner>.pointBox {
            width: 200px;
            height: 30px;
            background-color: rgba(255, 255, 255, .5);
            position: absolute;
            left: 50%;
            bottom: 30px;
            transform: translateX(-50%);
            border-radius: 15px;
            display: flex;
            justify-content: space-evenly;
            align-items: center;
        }
        
        .banner>.pointBox>li {
            width: 20px;
            height: 20px;
            background-color: #fff;
            border-radius: 50%;
            list-style: none;
            cursor: pointer;
        }
        
        .banner>.pointBox>li.active {
            background-color: red;
        }
        
        .banner>div {
            width: 30px;
            height: 50px;
            background-color: rgba(255, 255, 255, .3);
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            display: none;
            justify-content: center;
            align-items: center;
            font-size: 22px;
            color: #333;
            cursor: pointer;
        }
        
        .banner:hover>div {
            display: flex;
        }
        
        .banner>.left {
            left: 0;
            border-radius: 0 30px 30px 0;
        }
        
        .banner>.right {
            right: 0;
            border-radius: 30px 0 0 30px;
        }
        
        .banner>div:hover {
            background-color: rgba(255, 255, 255, .5);
        }

JavaScript部分


class Swiper{
    // 传入的三个参数 最外层的大盒子 图片所在的ul 焦点所在的ol
    constructor(banner, imgs = 'ul', btns = 'ol') {
        this.banner = document.querySelector(banner);
        this.imgBox = this.banner.querySelector(imgs);
        this.pointBox = this.banner.querySelector(btns);
        this.index = 1;
        this.banner_width = parseInt(window.getComputedStyle(this.banner).width);
        this.timer = null;
        this.flag = true;
    }
    copy() {
        let first = this.imgBox.firstElementChild.cloneNode(true);
        let last = this.imgBox.lastElementChild.cloneNode(true);
        this.imgBox.appendChild(first);
        this.imgBox.insertBefore(last, this.imgBox.children[0]);
        this.imgBox.style.width = this.imgBox.children.length * 100 + '%';
        this.imgBox.style.left = -this.index * this.banner_width + 'px';
    }
    setpoint() {
        for (let i = 0; i < this.imgBox.children.length - 2; i++) {
            let li = document.createElement('li');
            li.classList.add('item');
            li.dataset.id = i;
            this.pointBox.appendChild(li);
            if (i == 0) li.classList.add('active');
        }
    }

    animation(ele, obj) {
        for (let k in obj) {
            let type = k;
            let target = obj[k];
            if (type == 'opacity') target *= 100;
            let timere = setInterval(() => {
                    let init;
                    if (type == 'opacity') {
                        init = window.getComputedStyle(ele)[type] * 100;
                    } else {
                        init = parseInt(window.getComputedStyle(ele)[type]);
                    }
                    let duration = (target - init) / 10;
                    duration = duration >= 0 ? Math.ceil(duration) : Math.floor(duration);
                    if (init == target) {
                        clearInterval(timere);
                        this.movend();
                    } else {
                        if (type == 'opacity') {
                            ele.style[type] = (duration + init) / 100;
                        } else {
                            ele.style[type] = duration + init + 'px';
                        }
                    }
                },
                20)
        }
    }

    autoplay() {
        this.timer = setInterval(() => {
            this.index++;
            this.animation(this.imgBox, { left: -this.index * this.banner_width });
        }, 3000)
    }

    movend() {
        if (this.index == this.imgBox.children.length - 1) {
            this.index = 1;
            this.imgBox.style.left = -this.index * this.banner_width + 'px';
        }
        if (this.index == 0) {
            this.index = this.imgBox.children.length - 2;
            this.imgBox.style.left = -this.index * this.banner_width + 'px';
        }

        for (let i = 0; i < this.pointBox.children.length; i++) {
            this.pointBox.children[i].classList.remove('active');
        }
        this.pointBox.children[this.index - 1].classList.add('active');
        this.flag = true;
    }

    over_out() {
        this.banner.addEventListener('mouseover', () => {
            clearInterval(this.timer);
        })
        this.banner.addEventListener('mouseout', () => {
            this.autoplay();
        })
    }

    left_right_focus() {
        this.banner.addEventListener('click', (e) => {
            if (e.target.className == 'left') {
                if (this.flag == false) return;
                this.flag = false;
                this.index--;
                this.animation(this.imgBox, { left: -this.index * this.banner_width });
            }
            if (e.target.className == 'right') {
                if (this.flag == false) return;
                this.flag = false;
                this.index++;
                this.animation(this.imgBox, { left: -this.index * this.banner_width });
            }
            if (e.target.className == 'item') {
                if (this.flag == false) return;
                this.flag = false;
                let id = parseInt(e.target.dataset.id) + 1;
                this.index = id;
                this.animation(this.imgBox, { left: -this.index * this.banner_width });
            }
        })
    }

    changepage() {
        document.addEventListener('visibilitychange', () => {
            if (document.visibilityState == "hidden") {
                clearInterval(this.timer);
            } else {
                this.autoplay();
            }
        })
    }

    all() {
        this.copy();
        this.setpoint();
        this.animation();
        this.autoplay();
        this.movend();
        this.over_out();
        this.left_right_focus();
        this.changepage();
    }
}
// 把方法封装到对象里面 提高代码可复用性

let obj = new Swiper('第一个参数为外层最大的盒子','第二个参数为图片所在的列表 默认值ul','第三个参数为焦点所在的列表 默认值为ol');
把函数方法封装到对象里面 增加了代码的复用性

详细可见

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值