欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JavaScript实现简易轮播图最全代码解析(ES6面向对象)

程序员文章站 2022-03-16 20:51:59
本文实例为大家分享了javascript实现简易轮播图的具体代码,供大家参考,具体内容如下完整代码: &...

本文实例为大家分享了javascript实现简易轮播图的具体代码,供大家参考,具体内容如下

JavaScript实现简易轮播图最全代码解析(ES6面向对象)

完整代码:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>es6轮播图</title>
    <script></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .box {
            width: 500px;
            height: 300px;
            border: 1px solid silver;
            overflow: hidden;
            margin: auto;
            margin-top: 50px;
            position: relative;
            top: 0;
            left: 0;
        }
        .outer {
            list-style: none;
            position: absolute;
            top: 0;
            left: 0;
            transition: .3s all linear;
        }
        .img {
            width: 500px;
            height: 300px;
            float: left;
        }
  .btns span {
   position: absolute;
   width: 25px;
   height: 40px;
   top: 50%;
   margin-top: -20px;
   line-height: 40px;
   text-align: center;
   font-weight: bold;
   font-family: simsun;
   font-size: 30px;
   border: 1px solid silver;
   opacity: 0.5;
   cursor: pointer;
   color: #fff;
   background: black;
  }
  .btns .left {
   left: 5px;
  }
  .btns .right {
   left: 100%;
   margin-left: -32px;
  }
        .right > :first-child, .left > :first-child {
            width: 35px;
            height: 35px;
        }
        .ool {
            width: 163px;
            position: absolute;
            right: 0;
            left: 0;
            margin: auto;
            bottom: 10px;
            list-style: none;
        }
        .oli {
            width: 25px;
            height: 25px;
            background: white;
            border-radius: 50%;
            float: left;
        }
        .color {
            background: black;
        }
    </style>
</head>
<body>
<div class="box">
    <ul class="outer">
        <li class="img" style="background-image:url(img/0.jpeg)"></li>
        <li class="img" style="background-image:url(img/1.jpeg)"></li>
        <li class="img" style="background-image:url(img/2.jpeg)"></li>
        <li class="img" style="background-image:url(img/3.jpeg)"></li>
        <li class="img" style="background-image:url(img/4.jpeg)"></li>
    </ul>
 <div class="btns">
  <span class="left">&lt;</span>
  <span class="right">&gt;</span>
 </div>
</div>
</body>
<script>
    class chart{
        constructor(name, json) {
   //获取盒子名
            this.box = document.queryselector(name);
            this.json = json;
            //获取轮播图的属性
            this.outer = document.queryselector(name + ' .outer');  //注意加空格
            this.left = document.queryselector(name + ' .left');
            this.right = document.queryselector(name + ' .right');
            //初始化
            this.timer = null;  //自动播放
            this.inow = 0;
            this.init();
        }
        init() {
            const that = this; //保存一个this
            console.log(this.json.a);
            if (this.json.a){
                console.log(1);
            }
            //克隆第一张放到最后
            let uli = that.outer.children[0].clonenode(true);
            that.outer.appendchild(uli);
            that.outer.style.width = that.outer.children.length * that.outer.children[0].offsetwidth + 'px';
            //点击左右滑动
            if (that.json.slide) {
                that.left.style.display = 'block';
                that.right.style.display = 'block';
                this.left.onclick = () => that.rightgo();
                this.right.onclick = () => that.leftgo();
            }
            //自动播放
            if (that.json.move) {
                that.movego();
                //鼠标移入移出
                if (that.json.loop) {
                    that.box.onmousemove = () => clearinterval(that.timer);
                    that.box.onmouseout = () => that.movego();
                }
            }
            //展示小圆点
            if (that.json.nav) {
                let ool = document.createelement('ol');
                ool.classname = 'ool';
                ool.style.left = that.json.distanceleft + 'px';
                that.box.appendchild(ool);
                for (let i = 0; i < that.outer.children.length - 1; i++) {
                    let oli = document.createelement('li');
                    oli.classname = 'oli';
                    oli.style.marginleft = that.json.distance + 'px';
                    ool.appendchild(oli);
                }
                ool.style.width = ((that.outer.children.length - 1) * document.queryselector('.oli').offsetwidth) + (that.json.distance * that.outer.children.length) + 'px';
                that.alike();
            }
        };
        rightgo() {
            this.inow++;
            if (this.inow >= this.outer.children.length) {
                this.inow = 1;
                this.outer.style.transition = '0s all linear';
                this.outer.style.left = 0;
            }
            this.outer.style.left = -this.inow * this.outer.children[0].offsetwidth + 'px';
            this.outer.style.transition = '0.3s all linear';
            this.alike();
        };
        leftgo() {
            this.inow--;
            if (this.inow <= -1) {
                this.inow = this.outer.children.length - 1;
                this.outer.style.transition = '0s all linear';
                this.outer.style.left = -(this.outer.children.length - 1) * this.outer.children[0].offsetwidth + 'px';
                this.inow = this.outer.children.length - 2;
            }
            this.outer.style.left = -this.inow * this.outer.children[0].offsetwidth + 'px';
            this.outer.style.transition = '0.3s all linear';
   this.alike();
        };
        movego() {
            const that = this;
            this.timer = setinterval(() => that.rightgo(), that.json.speed || 1500)
        };
        //圆点对应每张图片
        alike() {
            let li = document.queryselectorall('.oli');
            for (let i = 0; i < li.length; i++) {
                li[i].classlist.remove('color');
                if (i == this.inow) {
                    li[i].classlist.add('color');
                } else {
                    li[i].classlist.remove('color');
                }
                //特殊:最后一张的时候是第一个
                if (this.inow == li.length) {
                    li[0].classlist.add('color');
                }
                //小圆点点击事件
                if (this.json.event) {
                    li[i].onmouseover = () => {
                        for (let i = 0; i < li.length; i++) {
                            li[i].classlist.remove('color');
                        }
                        li[i].classlist.add('color');
                        this.outer.style.left = -i * this.outer.children[0].offsetwidth + 'px';
                    }
                }
            }
        }
    }
    new chart('.box', {
        move: true,  //自动轮播
        speed: 1500,  //轮播速度
        loop: true,  //鼠标移入移出效果
        slide: true,  //点击左右滑动效果
        nav: true,  //展示小圆点
        distance: 20,  //小圆点间距
        event: true  //小圆点事件
    })
</script>
</html>

图片:

JavaScript实现简易轮播图最全代码解析(ES6面向对象)

JavaScript实现简易轮播图最全代码解析(ES6面向对象)

JavaScript实现简易轮播图最全代码解析(ES6面向对象)

JavaScript实现简易轮播图最全代码解析(ES6面向对象)

JavaScript实现简易轮播图最全代码解析(ES6面向对象)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: js 轮播图