zoukankan      html  css  js  c++  java
  • js 原生轮播图插件

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <style>
            #swiper{
                height: 200px;
                 200px;
                border:  1px solid #cccccc;
                margin: 0 auto;
                overflow: hidden;
                position: relative;
            }
            #swiper div{
                height: 200px;
                 200px;
                float: left;
                text-align: center;
    
            }
            #swiper div:nth-child(1){
                background: pink;
            }
            #swiper div:nth-child(2){
                height: 200px;
                 200px;
                float: left;
                text-align: center;
                background: gray;
            }
            #swiper div:nth-child(3){
                height: 200px;
                 200px;
                float: left;
                text-align: center;
                background: yellow;
            }
            #swiper div:nth-child(4){
                height: 200px;
                 200px;
                float: left;
                text-align: center;
                background: pink;
            }
            #swiper div:nth-child(5){
                height: 200px;
                 200px;
                float: left;
                text-align: center;
                background: gray;
            }
            #left{
                    position: absolute;
                left: 0;
                top: 50%;
                transform: translate3d(0,-50%,0);
                z-index: 999;
            }
            #right{
               position: absolute;
                right: 0;
                top: 50%;
                transform: translate3d(0,-50%,0);
                z-index: 999;
            }
            #swiperson{
                position: relative;
            }
        </style>
        <title>Document</title>
    </head>
    <body>
    <div id="swiper">
        <button id="left">左</button>
        <button id="right">右</button>
        <div id="swiperson">
            <div>3</div>
            <div>1</div>
            <div>2</div>
            <div>3</div>
            <div>1</div>
        </div>
    </div>
    <script>
        // 轮播图插件
    
        // 样式布局
        let allWidth = document.getElementById('swiperson').children.length * document.getElementById('swiper').clientWidth;
        let oneWidth = document.getElementById('swiper').clientWidth;
        document.getElementById('swiperson').style.width = allWidth + 'px';
        document.getElementById('swiperson').style.transform = 'translateX(-'+ oneWidth +'px)';
        // 点击逻辑
        let index = 1;
        let length = document.getElementById('swiperson').children.length;
        let flg = true;
    
        document.getElementById("swiperson").addEventListener("transitionend", function () {
            flg = true;
            if(index === length-1){
                document.getElementById('swiperson').style.transition = 'none';
                document.getElementById('swiperson').style.transform = 'translateX(-'+ oneWidth +'px)';
                index = 1
            }
            if(index === 0){
                document.getElementById('swiperson').style.transition = 'none';
                document.getElementById('swiperson').style.transform = 'translateX(-'+ oneWidth * (length-2)+'px)';
                index = length - 2
            }
        });
    
        function right() {
            if(flg === true){
                flg = false;
                index++;
                document.getElementById('swiperson').style.transition = 'all 1s';
                document.getElementById('swiperson').style.transform = 'translateX(-'+ oneWidth*index +'px)'
            }
        }
    
        function left() {
            if(flg === true){
                flg = false;
                index--;
                document.getElementById('swiperson').style.transition = 'all 1s';
                document.getElementById('swiperson').style.transform = 'translateX(-'+ oneWidth*index +'px)'
            }
        }
    
    // 点击
        document.getElementById('left').onclick = function () {
             left()
        };
        document.getElementById('right').onclick = function () {
            right();
        };
    
        // 自动轮播
        var time =  setInterval(function () {
            right();
        },2000)
    
        //手动滑动逻辑
        var a;
         document.getElementById('swiper').ontouchstart = function (ev) {
             clearInterval(time)
              a = ev.changedTouches[0].pageX;
         };
    
        document.getElementById('swiper').ontouchmove = function (ev) {
            let b = ev.changedTouches[0].pageX;
            console.log(b)
            if(b - a > 40){
                console.log('右滑动')
                left()
            }
            if(a - b > 40){
                console.log('左滑动')
                right();
    
            }
        }
    
    
    
        //PC 端  滑动逻辑
        var a;
        document.getElementById('swiper').onmousedown = function (ev) {
            clearInterval(time)
            console.log(ev)
            a = ev.pageX;
    
            document.getElementById('swiper').onmousemove = function (ev) {
                console.log('move');
                let b = ev.pageX;
                if(b - a > 0){
                    console.log('右滑动')
                    left()
                }
                if(a - b > 0){
                    console.log('左滑动')
                    right();
                }
            };
        }
    
        document.getElementById('swiper').onmouseup = function (ev) {
            document.getElementById('swiper').onmousemove = null
        }
    
    
    
    
    
    </script>
    </body>
    </html>
    

      

  • 相关阅读:
    shell 实现word count
    jvm 参数调优
    Java注解处理器(转)
    JVM -XX: 参数介绍(转)
    如何在Linux下重命名多个文件
    Kafka学习之broker配置(0.8.1版)(转)
    linux 历史命令用法(转)
    hive-site.xml 参数设置
    Hadoop-2.2.0 + Hbase-0.96.2 + Hive-0.13.1(转)
    正则表达式通过Unicode属性匹配
  • 原文地址:https://www.cnblogs.com/tiangeng/p/11264568.html
Copyright © 2011-2022 走看看