模拟吹制手指滑动轮播以及自动轮播效果

本文详细介绍了如何使用JavaScript实现一个手动滑动和自动轮播的效果。通过HTML结构、CSS样式以及JavaScript逻辑,创建了一个可上下滑动的轮播组件。在用户上滑和下滑时,轮播图片会根据滑动方向动态切换,并在滑动结束后恢复自动轮播。此外,代码还包含了节流锁机制,以避免快速滑动时的过度渲染。
摘要由CSDN通过智能技术生成

代码如下

html

1.首先创建骨架 

<div id="swiper" class="swiper">
        <div id="swiperContent" class="swiper-content">
            <div class="swiper-item">2</div>
            <div class="swiper-item">3</div>
            <div class="swiper-item">1</div>
            <div class="swiper-item">2</div>
            <div class="swiper-item">3</div>
            <div class="swiper-item">1</div>
            <div class="swiper-item">2</div>
        </div>
    </div>

2.样式

 .swiper {
            width: 500px;
            height: 300px;
            margin: 0 auto;
            background: #666;
            overflow: hidden;
        }

        .swiper-content {
            width: 100%;
            transition: transform 0.5s ease 0s;
            transform: translateY(-225px);
        }

        .swiper-item {
            width: 450px;
            height: 200px;
            margin: 25px;
            position: relative;
            top: 25px;
            background: #999;
        }

3.实现逻辑

 (function () {
            var swiper = document.getElementById('swiper')
            var swiperContent = document.getElementById('swiperContent')
            var swiperItems = document.getElementsByClassName('swiper-item')
            var idy = 2
            //函数节流锁
            var lock = true
            var timer = null

            //自动轮播实现
            timer = setInterval(() => {
                idy++
                console.log(idy)
                swiperContent.style.transform = "translateY(" + -225 * idy + "px)"
                if (idy === 5) {
                    setTimeout(function () {
                        swiperContent.style.transition = "none"
                        swiperContent.style.transform = "none"
                        idy = 2
                        swiperContent.style.transform = "translateY(" + -225 * idy + "px)"
                    }, 500);
                }
                swiperContent.style.transition = "transform 0.5s ease 0s"
            }, 3000)

            swiperContent.style.transform = "translateY(" + -225 * idy + "px)"
            //判断向上滑动还是向下滑动
            var startY, distance, direction
            swiper.onmousedown = function (e) {
                startY = e.clientY
            }
            swiper.onmousemove = function (e) {
                e.preventDefault()
            }
            swiper.onmouseup = function (e) {
                var endY = e.clientY
                distance = parseInt(endY - startY)
                // console.log(distance)
                direction = 0
                if (distance > 0 && distance > 50) {
                    direction = -1
                }
                if (distance < 0 && distance < -50) {
                    direction = 1
                }

                //根据滑动方向来控制图片运动
                play(direction)

            }

            function play(direction) {
                clearInterval(timer)
                swiperContent.style.transition = "transform 0.5s ease 0s"
                if (direction === 1) {
                    if (!lock) {
                        return;
                    }
                    lock = false;
                    idy++
                    console.log(idy)
                    swiperContent.style.transform = "translateY(" + -225 * idy + "px)"
                    if (idy === 5) {
                        setTimeout(function () {
                            swiperContent.style.transition = "none"
                            swiperContent.style.transform = "none"
                            idy = 2
                            swiperContent.style.transform = "translateY(" + -225 * idy + "px)"
                        }, 500);
                    }
                    setTimeout(function () {
                        lock = true;
                    }, 500)
                }

                if (direction === -1) {
                    if (!lock) {
                        return;
                    }
                    lock = false;
                    idy--
                    console.log(idy)
                    swiperContent.style.transform = "translateY(" + -225 * idy + "px)"
                    if (idy === 1) {
                        setTimeout(function () {
                            swiperContent.style.transition = "none"
                            swiperContent.style.transform = "none"
                            idy = 4
                            swiperContent.style.transform = "translateY(" + -225 * idy + "px)"
                        }, 500);
                    }
                    setTimeout(function () {
                        lock = true;
                    }, 500)
                }
                timer = setInterval(() => {
                    idy++
                    console.log(idy)
                    swiperContent.style.transform = "translateY(" + -225 * idy + "px)"
                    if (idy === 5) {
                        setTimeout(function () {
                            swiperContent.style.transition = "none"
                            swiperContent.style.transform = "none"
                            idy = 2
                            swiperContent.style.transform = "translateY(" + -225 * idy + "px)"
                        }, 500);
                    }
                    swiperContent.style.transition = "transform 0.5s ease 0s"
                }, 3000)
            }
        })()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值