js实现移动端触屏滑动轮播图

使用js来实现轮播图

实现功能

1.索引小圆点
2.使用定时器实现过渡动画
3.滑动图片看下一张图

代码

show.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="02test.css">
    <link rel="stylesheet" href="base.css">
</head>
<body>
    <div class="box">
        <div class="banner">
            <div class="content">
                <div class="item">
                    <a href="#">
                        <img src="images/banner4.jpg" alt="">
                    </a>
                </div>
                <div class="item">
                    <a href="#">
                        <img src="images/banner1.jpg" alt="">
                    </a>
                </div>
                <div class="item">
                    <a href="#">
                        <img src="images/banner2.jpg" alt="">
                    </a>
                </div>
                <div class="item">
                    <a href="#">
                        <img src="images/banner3.jpg" alt="">
                    </a>
                </div>
                <div class="item">
                    <a href="#">
                        <img src="images/banner4.jpg" alt="">
                    </a>
                </div>
                <div class="item">
                    <a href="#">
                        <img src="images/banner1.jpg" alt="">
                    </a>
                </div>

            </div>
            <ul class="dian">
                <li class="active"></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
    </div>
</body>
</html>
<script src="show1.js"></script>
<script src="show2.js"></script>

show1.js

function animation(ele,target) {
    clearInterval(ele.timer);

    var step = target > ele.offsetLeft? 20:-20;

    ele.timer = setInterval(function () {
        ele.style.left = ele.offsetLeft + step +"px";

        if (Math.abs(target - ele.offsetLeft) <= Math.abs(step)) {
            ele.style.left = target + "px";
            clearInterval(ele.timer);
        }
    },10);
}

show2.js

var banner = document.querySelector(".banner");
var content = document.querySelector(".content");
var bannerWidth = banner.offsetWidth;
var liArr = document.querySelectorAll(".banner .dian li");

var index = 1;//索引值
var timer = null;
var startX = 0;
//使界面自动滚动
timer = setInterval(function () {
    autoMove();
},2000)

function autoMove() {
    liArr[index - 1].className = "";
    // index++
    // 执行动画(位移+过渡)
    index++;
    content.style.transition = "1s";
    content.style.transform = "translateX("+(-index * bannerWidth)+"px)";
}

banner.addEventListener("touchstart",function (e) {
    startX = e.touches[0].clientX;
      // 停止自动
    clearInterval(timer);
})

banner.addEventListener("touchend",function (e) {
    var endX = e.changedTouches[0].clientX;
    if (startX > endX) {
            // 左滑
        autoMove();
    } else {
    		//右滑
        liArr[index - 1].className = "";

        index--;
        content.style.transition = "1s";
        content.style.transform = "translateX("+(-index * bannerWidth)+"px)";
    }

    timer = setInterval(function () {
        autoMove();
    },2000);
})
//监听过渡事件
content.addEventListener("transitionend",function () {
    if (index > 4) {
        index = 1;
        content.style.transition = "";
        content.style.transform = "translateX(" + (-index * bannerWidth) + "px)";
    }
    if (index < 1 ) {
        index = 4;
        content.style.transition = "";
        content.style.transform = "translateX(" + (-index * bannerWidth) + "px)";
    }
    liArr[index - 1].className = "active";
})
content.addEventListener("touchmove",function (e) {
    var moveX = e.changedTouches[0].clientX;
    if (moveX > startX) {
        liArr[index-1].className = "";

        content.style.transition = "";
        content.style.transform  ="translateX(" +(-index * bannerWidth + Math.abs(moveX - startX))+"px)";
    } else {
        liArr[index-1].className = "";

        content.style.transition = "";
        content.style.transform  ="translateX(" +(-index * bannerWidth - Math.abs(moveX - startX))+"px)";
    }
})

效果图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值