ES6手写移动端轮播图滑动切换

备注:为了使大家更好的理解移动端,响应pc端的代码被删了,并且未做更多的简化以及合理的代码分配,还望谅解

//复制代码并运行即可显示效果

css样式:
        *{
            padding: 0;margin: 0;
        }
        .wrap{
            width:100%;
            min-width:320px;
            max-width:980px;
            height:200px;
            position:relative;
            overflow:hidden;
            margin:50px auto;
        }
        .wrap ul,.wrap ol {
            list-style:none;
        }
        .wrap ul{
            width:700%;
            height:100%;
        }
        .wrap ul li{
            width:20%;
            height:100%;
            float:left;
        }
        .wrap ol{
            width:100%;
            height:20px;
            position:absolute;
            top:160px;
            display:flex;
            justify-content: center;
        }
        .wrap ol li{
            width: 12px;
            height: 12px;
            border-radius:50%;
            background-color:white;
            float:left;
            margin:0 4px;
        }
        .wrap ol li.active{
            background-color: black;
        }


布局:
<div class="wrap">
    <ul>
        <li style="background:rgb(68, 0, 255);"></li>
        <li style="background:red;"></li>
        <li style="background:yellow;"></li>
        <li style="background:rgb(43, 235, 107);"></li>
        <li style="background:rgb(12, 138, 60);"></li>
        <li style="background:rgb(68, 0, 255);"></li>
        <li style="background:red;"></li>
    </ul>
    <ol>
        <li class="active"></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ol>
</div>

js代码:
let wrap = document.querySelector('.wrap');
let wrapul = wrap.querySelector('ul');
let ulchildren = wrapul.children;
let wrapol = wrap.querySelector('ol');
let list = wrapol.querySelectorAll('li');
console.log(wrap,wrapul,ulchildren,wrapol,list)
class Sport{
    constructor(wrap,wrapul,ulchildren,wrawpol,list){
        this.wrap = wrap;//最大父级
        this.wrapul = wrapul;//滚动父级
        this.ulchildren = ulchildren;//滚动父级内的li
        this.lengths = this.ulchildren.length;//滚动父级内的li的长度
        this.wrapol = wrapol;//小圆点的父级
        this.list = list;//所有小圆点
        this.length = this.list.length;//所有小圆点的长度
        this.currentIndex = 1;//初始化下标
        this.timer = null;//定时器
        this.wrapWidth = this.wrap.clientWidth;//最大父级的宽
        this.init();
        this.listIndex = 0;//初始化小圆点的下标
        this.touchX= 0;//触摸开始位置
        this.moveX = 0;//触摸结束位置
        this.deltaX = 0;//触摸开始位置 - 触摸结束位置
    }
    //初始化函数
    init() {
        this.Size();
        this.sport();
        this.touch();
        this.Resize();
    }
    //设置width和height
    Size() {
        for(let i = 0;i < this.ulchildren.length;i ++){
            this.ulchildren[i].style.width = this.wrapWidth+'px'
        }
        this.wrapul.style.width = this.wrapWidth * this.ulchildren.length + 'px'
        this.wrapul.style.transform = `translate(${-this.wrapWidth}px)`
    }
    Resize() {
        //在这里可以检测pc端时窗口width和height,从而重置所有的样式以此来响应pc端
        window.addEventListener('resize',() => {
            
        })
    }
    sport() {
        //定时器
        this.timer = setInterval(()=>{
            this.list[this.listIndex].classList.remove('active')
            this.currentIndex ++
            this.listIndex ++ 
            if(this.listIndex >= 5){
                this.listIndex = 0
            }
            this.list[this.listIndex].classList.add('active')
            this.wrapul.style.webkitTransition = 'transform 0.5s ease-in-out';
            this.wrapul.style.transform = `translate(${-this.wrapWidth*this.currentIndex}px)`
            if(this.currentIndex >= 6){
                this.currentIndex = 1
                setTimeout(()=>{
                    this.wrapul.style.webkitTransition = 'none';
                    this.wrapul.style.transform = `translate(${-this.wrapWidth}px)`
                },500)
            }
        },1000)
    }
    touch() {
        //触摸开始事件
        this.wrapul.addEventListener('touchstart',(ev)=>{
            clearInterval(this.timer)
            this.touchX = ev.touches[0].clientX
            wrapul.style.webkitTransition = 'none'
        })
        //触摸移动事件
        this.wrap.addEventListener('touchmove',(ev)=> {
            this.moveX = ev.touches[0].clientX;
            this.deltaX = this.moveX - this.touchX;
            this.wrapul.style.webkitTransform = 
            `translateX(${-this.wrapWidth * this.currentIndex + this.deltaX}px)`
        })
        //触摸结束事件
        this.wrap.addEventListener('touchend',(ev)=> {
            clearInterval(this.timer)
            this.sport();
            //如果触摸开始到移动结束,所移动的距离大于盒子wrap的一半,则wrapul移动一个wrap的距离,否则回到当前位置
            if(Math.abs(this.deltaX) >= this.wrapWidth/2){
                this.list[this.listIndex].classList.remove('active')
                if(this.deltaX <0){
                    this.listIndex ++
                    this.currentIndex ++
                    if(this.listIndex >= 5){
                        this.listIndex = 0
                        setTimeout(()=>{
                            this.wrapul.style.webkitTransition = 'none';
                            this.wrapul.style.transform = `translate(${-this.wrapWidth}px)`
                            this.currentIndex = 1
                        },500)
                    }
                    this.list[this.listIndex].classList.add('active')
                }else{
                    this.listIndex --
                    this.currentIndex --
                    if(this.listIndex < 0){
                        this.listIndex = 4
                        setTimeout(()=>{
                            this.wrapul.style.webkitTransition = 'none';
                            this.wrapul.style.transform = `translate(${-this.wrapWidth*5}px)`;
                            this.currentIndex = 5
                            console.log(this.currentIndex)
                        },500)
                        
                    }
                    this.list[this.listIndex].classList.add('active')
                }
                this.wrapul.style.webkitTransition = 'transform .4s ease-in-out'
                this.wrapul.style.webkitTransform = `translateX(${-this.currentIndex * this.wrapWidth}px)`
            }
            else{
                this.currentIndex = this.currentIndex;
                this.listIndex = this.listIndex;
                this.wrapul.style.webkitTransition = 'transform .4s ease-in-out'
                this.wrapul.style.webkitTransform = `translateX(${-this.currentIndex * this.wrapWidth}px)`
            }
        })
    }
}
new Sport(wrap,wrapul,ulchildren,wrapol,list);
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在ES6中,可以采用以下几种方法来手写数组去重: 1. 使用Set数据结构: 使用Set数据结构是一种简单且高效的方法,由于Set不允许重复值,因此可以利用这一特性快速实现数组去重: ```javascript const arr = [1, 2, 3, 4, 5, 1, 2, 3]; const uniqueArr = [...new Set(arr)]; ``` 2. 使用filter方法: 利用数组的filter方法结合indexOf可以实现数组去重。 ```javascript const arr = [1, 2, 3, 4, 5, 1, 2, 3]; const uniqueArr = arr.filter((item, index) => arr.indexOf(item) === index); ``` 3. 使用reduce方法: 利用reduce方法遍历数组,将不重复的值保存在一个新的数组中。 ```javascript const arr = [1, 2, 3, 4, 5, 1, 2, 3]; const uniqueArr = arr.reduce((acc, cur) => { if (!acc.includes(cur)) { acc.push(cur); } return acc; }, []); ``` 这些方法都可以在ES6手写实现数组去重,具体选择哪种方法取决于实际的需求和性能要求。 ### 回答2: ES6提供了几种去重数组的方法,下面我将手写几个常用的数组去重方法。 方法一:使用Set对象 ```javascript let arr = [1, 2, 3, 3, 4, 4, 5]; let uniqueArr = Array.from(new Set(arr)); console.log(uniqueArr); // [1, 2, 3, 4, 5] ``` 方法二:使用filter函数和indexOf方法 ```javascript let arr = [1, 2, 3, 3, 4, 4, 5]; let uniqueArr = arr.filter((item, index) => arr.indexOf(item) === index); console.log(uniqueArr); // [1, 2, 3, 4, 5] ``` 方法三:使用reduce函数 ```javascript let arr = [1, 2, 3, 3, 4, 4, 5]; let uniqueArr = arr.reduce((prev, curr) => prev.includes(curr) ? prev : [...prev, curr], []); console.log(uniqueArr); // [1, 2, 3, 4, 5] ``` 方法四:使用利用Map对象 ```javascript let arr = [1, 2, 3, 3, 4, 4, 5]; let map = new Map(); arr.forEach(item => map.set(item, item)); let uniqueArr = [...map.values()]; console.log(uniqueArr); // [1, 2, 3, 4, 5] ``` 以上就是我手写的几个利用ES6实现数组去重的方法。这些方法都能有效地去除数组中的重复元素。 ### 回答3: 在ES6中,有许多方法可以用来手写数组去重。以下是其中几种常见的方法: 1. 使用Set对象:ES6中新增的Set对象可以用来存储唯一的值。可以将数组转换为Set对象,然后将Set对象再转换回数组,即可实现数组的去重。 ```javascript const arr = [1, 2, 2, 3, 4, 4, 5]; const uniqueArr = Array.from(new Set(arr)); console.log(uniqueArr); // [1, 2, 3, 4, 5] ``` 2. 使用reduce方法:使用reduce方法遍历数组,将每个元素添加到一个空数组中,但之前先检查该元素是否已存在于新数组中。 ```javascript const arr = [1, 2, 2, 3, 4, 4, 5]; const uniqueArr = arr.reduce((acc, cur) => { if (!acc.includes(cur)) { acc.push(cur); } return acc; }, []); console.log(uniqueArr); // [1, 2, 3, 4, 5] ``` 3. 使用filter方法:使用filter方法遍历数组,根据元素在数组中的索引位置进行判断,只保留第一次出现的元素。 ```javascript const arr = [1, 2, 2, 3, 4, 4, 5]; const uniqueArr = arr.filter((value, index, self) => { return self.indexOf(value) === index; }); console.log(uniqueArr); // [1, 2, 3, 4, 5] ``` 这些方法都可以有效地实现数组的去重,根据实际情况选择适合的方法使用即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值