1、js原生实现移动端手指滑动轮播图效果(2)



html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0">
    <title>1、js原生实现移动端手指滑动轮播图效果(2)title>
    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <style>
        *{
            margin: 0;
            padding: 0;
        }

        html, body {
            height: 100%;
            width: 100%;
        }

        ul, ol {
            list-style: none;
        }

        /*清除浮动*/
        .clearfix:before, .clearfix:after {
            content: "";
            display: table;
            visibility: hidden;
            height: 0;
            clear: both;
        }
        .box{
            height: 50vh;
            width: 100vw;
            background: red;
        }
        .box1{
            background: yellow;
        }
        .box2{
            background: yellowgreen;
        }
        .box3{
            background: cyan;
        }
        .box4{
            background: orange;
        }
        /*轮播图*/
        .wrpa-w {
            padding-top: 0 !important;
        }

        .banner {
            width: 100%;
            position: relative;
            overflow: hidden;
            z-index:5;
        }
        .banner ul:nth-child(1){
            width: 1000%;
            -webkit-transform : translateX(-10%);
            transform : translateX(-10%);
        }
        .banner ul:nth-child(1) li {
            width: 10%;
            float: left;
        }
        .banner ul:nth-child(2){
            width: 150px;
            position: absolute;
            height: 6px;
            left: 50%;
            margin-left: -75px;
            bottom: 5px;

        }
        .banner ul:nth-child(2) li {
            float: left;
            width: 10px;
            height: 4px;
            background: silver;
            margin-right: 10px;
            border-radius: 2px;
        }
        .banner ul:nth-child(2) li:last-child {
            margin-right: 0;
        }
        .now {
            transition: all .5s;
            transform: scaleX(2) scaleY(1.5);
            background: white !important;
        }
        /*e 轮播图*/
        .nav .text{
            width: 100%;
            height: 1000px;
            /*background: white;*/
        }


    style>
head>
<body>
<div class="banner">
    <ul class="clearfix">          <li>
        <div class="box box4">444div>
    li>
        <li>
            <div class="box box1">1111div>
        li>
        <li>
            <div class="box box2">2222div>
        li>
        <li>
            <div class="box box3">3333div>
        li>
        <li>
            <div class="box box4">444div>
        li>
        <li>
            <div class="box box1">1111div>
        li>
    ul>
    <ul>
        <li>li>
        <li>li>
        <li>li>
        <li>li>
    ul>
div>

body>
html>
<script>
    window.onload=function () {
        banner();
    };
    //定义一个对象 承载我们封装的事件
    window.yy = {};
    yy.transitionEnd = function(dom, callback) {
        /* 需要绑定事件的dom  绑定之后  当触发了 事件的时候  执行 callback */
        if(dom && typeof dom == 'object') {
            dom.addEventListener('webkitTransitionEnd', function() {
                /*if(callback){
                 callback();
                 }*/
                callback && callback();
            });

            dom.addEventListener('transitionEnd', function() {
                callback && callback();
            });
        }
    };
    function banner() {
        var banner = document.querySelector('.banner');
        var width = banner.offsetWidth;
        var imageBox = banner.querySelector('ul:first-child');
        // console.log(imageBox);
        var pointBox = banner.querySelector('ul:last-child');
        var points = pointBox.querySelectorAll('li');
        // 1.自动轮播
        var index = 1;//当前图片索引
        points[index-1].className = 'now';

        //无缝滚动
        yy.transitionEnd(imageBox, function () {
            if (index >= 5) {
                index = 1;
                removeTransition();
                translateX(-index * width);
            } else if (index <= 0) {
                index = 4;
                removeTransition();
                translateX(-index * width);
            }
            setPoint();
        })
// 2点对应图片
        var setPoint = function () {
            for (var i = 0; i < points.length; i++) {
                points[i].className = "";
            }
            //判断索引 动画结束后设置点
            points[index - 1].className = 'now';
        }
//3图片滑动
        var startX = 0;//开始坐标
        var moveX = 0;//移动时x的坐标
        var distancex = 0;//移动距离
        var isMove = false;//是否滑动过
        imageBox.addEventListener('touchstart', function (e) {
            // e.stopPropagation();
            startX = e.touches[0].clientX;
        });
        imageBox.addEventListener('touchmove', function (e) {
            // e.stopPropagation();
            isMove = true;
            moveX = e.touches[0].clientX;
            // console.log(moveX);
            distancex = moveX - startX;
            //console.log(distancex);
            removeTransition();
            translateX(-index * width + distancex);
        });
        //谷歌浏览器toucuend可能会丢失事件 换为window
        window.addEventListener('touchend', function (e) {
            // e.stopPropagation();
            if (Math.abs(distancex) > (width / 3) && isMove) {
                if (distancex > 0) {
                    index--;
                } else {
                    index++;
                }
                addTransition();
                translateX(-index * width);
            } else {
                addTransition();
                translateX(-index * width);
            }
            isMove = false;
            startX = 0;
            moveX = 0;
            distancex = 0;

        })
        /*公用方法*/
//添加过渡
        var addTransition = function () {
            imageBox.style.webkitTransition = 'all .2s ';
            imageBox.style.transition = "all .2s";
        }
//删除过渡
        var removeTransition = function () {
            imageBox.style.webkitTransition = 'none';
            imageBox.style.transition = "none";
        }
// 设置定位
        var translateX = function (x) {
            imageBox.style.webkitTransform = 'translateX(' + x + 'px)';
            imageBox.style.transform = 'translateX(' + x + 'px)';

        }

    }


script>

你可能感兴趣的:(JS)