zoukankan      html  css  js  c++  java
  • 自己写的一个简单版swiper,后面再学习优化

    <template>
        <div class="swiper">
            <div id="swiper_lsc" class="swiper_lsc">
                <div class="swiper_box" v-for="(item,index) in imgArr" :key="index">
                    <img :src="item.url" alt="">
                </div>
            </div>
        </div>
    </template>
    <script>
    export default {
        name:'swiper',
        data() {
            return {
                imgArr:[
                    {url:require('@/assets/images/index/icon_code1.png')},
                    {url:require('@/assets/images/index/icon_code1.png')},
                    {url:require('@/assets/images/index/icon_code1.png')},
                    {url:require('@/assets/images/index/icon_code1.png')},
                ],
            }
        },
        created() {
            
        },
        mounted() {
            var mybody = document.getElementsByTagName('body')[0];
            var mySwiper=document.getElementById('swiper_lsc');
            var mySwiperBox=document.getElementsByClassName('swiper_box')[0].clientWidth;
            var h = document.documentElement.clientHeight;
            var w = document.documentElement.clientWidth;
            var scrollW=(w-mySwiperBox-44)/2;
            var len=this.imgArr.length;
            mySwiper.style.width=len*(mySwiperBox+22)+'px';
            mybody.style.height = h + 'px';
            //滑动处理
            var startX, startY, moveEndX, moveEndY, X, Y;   
            mySwiper.style.transform=`translate3d(${scrollW}px, 0px, 0px)`;
            mySwiper.addEventListener('touchstart', function(e) {
                e.preventDefault();
                startX = e.touches[0].pageX;
                startY = e.touches[0].pageY;
            }, false);
            mySwiper.addEventListener('touchmove', function(e) {
                e.preventDefault();
                moveEndX = e.changedTouches[0].pageX;
                moveEndY = e.changedTouches[0].pageY;
                X = moveEndX - startX;
                Y = moveEndY - startY;
                if ( Math.abs(X) > Math.abs(Y) && X > 0 ) {
                    //console.log("向右");
                    mySwiper.style.transform=`translate3d(${scrollW+X}px, 0px, 0px)`;
                    if(X>80){
                        
                    }
                }
                else if ( Math.abs(X) > Math.abs(Y) && X < 0 ) {
                    //console.log("向左");
                    mySwiper.style.transform=`translate3d(${scrollW+X}px, 0px, 0px)`;
                    if(Math.abs(X)>80){
                        
                    }
                }
                else if ( Math.abs(Y) > Math.abs(X) && Y > 0) {
                    //console.log("向下");
                }
                else if ( Math.abs(Y) > Math.abs(X) && Y < 0 ) {
                    //console.log("向上");
                }
                else{
                    //console.log("没滑动");
                }
            });
            mySwiper.addEventListener('touchend', function(e) {
                e.preventDefault();
                var isOnce=true;
                if ( Math.abs(X) > Math.abs(Y) && X > 0 ) {
                    if(X>80){
                        if(scrollW>0){
                            mySwiper.style.transform=`translate3d(${scrollW}px, 0px, 0px)`;
                            return;
                        }
                        if(isOnce){
                            scrollW+=322;
                            isOnce=false;
                        }
                        mySwiper.style.transform=`translate3d(${scrollW}px, 0px, 0px)`;
                    }else{
                        mySwiper.style.transform=`translate3d(${scrollW}px, 0px, 0px)`;
                    }
                }else if( Math.abs(X) > Math.abs(Y) && X < 0){
                    if(Math.abs(X)>80){
                        if(isOnce){
                            scrollW-=322;
                            isOnce=false;
                        }
                        //滑动到最后一个
                        if((Math.abs(scrollW)+22*len)>=mySwiper.clientWidth){
                            scrollW+=322;
                        }
                        mySwiper.style.transform=`translate3d(${scrollW}px, 0px, 0px)`;
                    }else{
                        mySwiper.style.transform=`translate3d(${scrollW}px, 0px, 0px)`;
                    }
                }
                mySwiper.removeEventListener('touchmove',this,false);
                mySwiper.removeEventListener('touchend',this,false);
            });
        },
    }
    </script>
    <style lang="scss" scoped>
    .swiper{
        100%;
        height: 100%;
        overflow: hidden;
        background-color:#d1d1d1;
    }
    .swiper_lsc{
        100%;
        height: 100%;
        display: -webkit-box;
        overflow-x: scroll;
        -webkit-overflow-scrolling:touch;
        white-space: nowrap;
        transition-duration: 400ms;
        .swiper_box{
            6rem;
            display: inline-block;
            text-align: center;
            margin:0 0 0 0.44rem;
            background-color:#fff;
            border-radius:0.25rem;
        }
        &::-webkit-scrollbar{
        display:none;
        } 
    }
    </style>
  • 相关阅读:
    MS SqlServer学习笔记(索引)
    Angular动态注册组件(controller,service...)
    如何成功发布一个MSMQ的Windows服务
    主流Web服务器一览
    .NET 创建Windows服务,及服务的安装卸载
    SQL Server 查询时间段内数据
    委托和事件
    类中实现 Dispose And Finalize
    使用 ODBC .NET 提供程序和 Visual C# .NET 执行 SQL 参数化存储过程
    Windows Form 中快捷键设置
  • 原文地址:https://www.cnblogs.com/chao202426/p/11466441.html
Copyright © 2011-2022 走看看