移动端轮播图特效(原生js)

哎!本来想放个视频出来的,算了,动画自己脑补吧。

触摸列表说明
touchstart手指触摸
touchmove手指移动
touchend手指离开
 window.addEventListener('load', function () {
            //首先给最左边添加最后一张图片和最右边添加第一张图片
            const div = document.querySelector('div');
            const ul = document.querySelector('ul');
            const li = document.querySelectorAll('ul li');
            const ol = document.querySelector('ol');

            //给右边添加第一张图片
            ul.appendChild(li[0].cloneNode(true));
            //给左边添加最后一张图片
            ul.insertBefore(li[li.length - 1].cloneNode(true), li[0]);
            //给 ul 定义宽度
            ul.style.width = document.querySelectorAll('ul li').length * 100 + '%';


            //动态生成小圆点的个数
            for (let i = 0, length = document.querySelectorAll('ul li').length - 2; i < length; i++) {
                let li = document.createElement('li');
                ol.appendChild(li);
            }
            //图片索引
            let index = 0;
            //图片的宽度
            let imageWidth = li[0].offsetWidth;
            //与图片索引对应的小圆点
            ol.children[index].classList.add('current');

            //设置定时器
            let time = setInterval(function () {
                index++;
                ul.style.transition = "all 0.3s";
                ul.style.transform = `translateX(${-index * imageWidth}px)`;
            }, 2000)

            //当过渡完成之后,再去判断   实现无缝滚动
            ul.addEventListener('transitionend', function () {
                //当index 大于等于图片的长度时 立即回到第一张图片
                if (index >= li.length) {
                    index = 0;
                    ul.style.transition = "none";
                    ul.style.transform = `translateX(${-index * imageWidth}px)`;
                } else if (index < 0) { ///当index 小于图片长度时  立即回到最后一张图片
                    index = li.length - 1;
                    ul.style.transition = "none";
                    ul.style.transform = `translateX(${-index * imageWidth}px)`;
                }
                //为小圆点添加样式
                ol.querySelector(".current").classList.remove('current');
                ol.children[index].classList.add('current');
            })

            let startX = 0; //手指初始的距离
            let moveX = 0;    //手指移动的距离
            //手指触摸
            ul.addEventListener('touchstart', function (e) {
                clearInterval(time);
                startX = e.targetTouches[0].pageX;
            })
            //手指移动
            ul.addEventListener('touchmove', function (e) {
                moveX = e.targetTouches[0].pageX - startX;
                ul.style.transition = "none";
                ul.style.transform = `translateX(${-index * imageWidth + moveX}px)`;
            })
            //手指离开
            ul.addEventListener('touchend', function (e) {
                if (Math.abs(moveX) > 50) {
                    if (moveX > 0) {
                        index--;
                    } else if (moveX < 0) {
                        index++;
                    }
                } else {  //回弹效果
                    ul.style.transition = "all 0.1s";
                    ul.style.transform = `translateX(${-index * imageWidth}px)`;
                }
                ul.style.transition = "all 0.3s";
                ul.style.transform = `translateX(${-index * imageWidth}px)`;
            })

        })
 <div class="swiper">
        <ul>
            <li><img src="./image/timg (1).jpg" alt=""></li>
            <li><img src="./image/timg (3).jpg" alt=""></li>
            <li><img src="./image/timg4).jpg" alt=""></li>
        </ul>
        <ol>
        </ol>
    </div>
  * {
            margin: 0;
            padding: 0;
        }
        .swiper {
            position: relative;
            overflow: hidden;
        }
        ul {
            height: 200px;
            margin-left: -100%;
        }
        ul li {
            list-style: none;
            float: left;
            width: 100vw;
            height: 100%;
        }
        li img {
            width: 100%;
            height: 100%;
        }
        ol {
            position: absolute;
            right: 10px;
            bottom: 0;
            z-index: 99;
        }
        ol li {
            margin: 5px;
            padding: 5px;
            border-radius: 10px;
            background-color: white;
            float: left;
            list-style: none;
            transition: all 0.1s;
        }
        .current {
            width: 15px;
        }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值