轮播——左右轮播(加移动端的左右滑动)

  • html
  • -
    <div class="small_screen ">
      <div class="d1_small">
           <div><img src="../img/index/anli1.png" alt=""></div>
           <div><img src="../img/index/anli2.png" alt=""></div>
           <div><img src="../img/index/anli3.png" alt=""></div>
           <div><img src="../img/index/anli4.png" alt=""></div>
           <div><img src="../img/index/anli5.png" alt=""></div>
           <div><img src="../img/index/anli6.png" alt=""></div>
       </div>
    </div>
  • css
.small_screen{margin-top: .5rem;text-align: center;width: 100%;touch-action: none;}
section .d1_small{width: 600%;overflow: hidden;display: inline-block;transition: all linear .3s;-moz-transition: all linear .3s; -webkit-transition: all linear .3s; -o-transition: all linear .3s;}
section .d1_small>div{display: inline-block;transition: all linear .3s;-moz-transition: all linear .3s; -webkit-transition: all linear .3s; -o-transition: all linear .3s;width: calc(100%/6)}
section .d1_small>div img{width: 70%}
  • js
$('.small_screen>div div').on('touchstart',function(event){
        //screenWidth:屏幕分辨率宽度
        var screenWidth = $(window).width();
        var that = this;
        //获取ul下的li总数
        var liCount = $('.small_screen>div div').length;
        //index:获取当前被点击的图片的索引值
        var index = $(that).index();
        //最小滑动距离,当左右滑动距离小于这个值时,图片返回原位置,不产生向左或者向右切换图片的效果
        var minMoveDis = 100;
        //获取点击x坐标
        var _touch = event.originalEvent.targetTouches[0];
        var clickX = _touch.pageX;
        $(".small_screen>div div").on('touchmove',function(event){
            //移动过程中,距离最开始点击位置的X距离
            var _sectouch = event.originalEvent.targetTouches[0];
            var distance = _sectouch.pageX - clickX ;
            var moveX = distance*(-1) + screenWidth * index * (-1);
            //滑动事件结束时
            $('.small_screen>div div').on('touchend',function(){
                $(".d2_small>span").removeClass('s_ac')
                if (0 < distance < minMoveDis ) {
                    moveX = index *screenWidth*(-1);
                }
                if(distance >=minMoveDis ){
                    if(index!=0){
                        moveX = (index-1) *screenWidth*(-1);
                        $(".d2_small>span").eq(index-1).addClass('s_ac')
                    }else{
                        $(".d2_small>span").eq(0).addClass('s_ac')
                    }
                }
                if(distance <=0){
                    if(index<liCount-1){
                        moveX = (index+1) *screenWidth*(-1);
                        $(".d2_small>span").eq(index+1).addClass('s_ac')
                    }else{
                        $(".d2_small>span").eq(liCount-1).addClass('s_ac')
                    }
                }
                $(".small_screen>div").css('margin-left',moveX+"px");

            });
        });
    });

这里写图片描述

在Web开发中,实现响应触摸滑动事件的轮播图通常需要使用JavaScript库来帮助处理触摸事件和动画。以下是使用jQuery和一个流行的触摸滑动插件如Swiper来实现响应触摸滑动事件的轮播图的基本步骤: 1. 引入jQuery库和Swiper插件的CSS和JS文件到你的HTML页面中。 2. 创建轮播图的HTML结构,通常包括一个包含多个子项的容器。 3. 初始化Swiper实例,并设置其配置选项,确保将`touchEventsTarget`设置为轮播图容器,以便插件能够捕获触摸事件。 4. Swiper会自动处理触摸滑动事件,并根据用户的滑动方向更新轮播图的显示状态。 示例代码如下: ```html <!-- 引入Swiper的CSS和JS文件 --> <link rel="stylesheet" href="swiper.min.css"> <script src="swiper.min.js"></script> <!-- 引入jQuery --> <script src="jquery.min.js"></script> <!-- 创建轮播图结构 --> <div class="swiper-container"> <div class="swiper-wrapper"> <!-- 轮播图项 --> <div class="swiper-slide">内容 1</div> <div class="swiper-slide">内容 2</div> <div class="swiper-slide">内容 3</div> <!-- ... 更多轮播图项 --> </div> <!-- 如果需要,添分页器 --> <div class="swiper-pagination"></div> </div> <script> // 初始化Swiper实例 var swiper = new Swiper('.swiper-container', { touchEventsTarget: '.swiper-container', // 确保这里设置为轮播图容器 // 其他配置项... }); </script> ``` 在上述代码中,`touchEventsTarget` 设置为轮播图容器 `.swiper-container`,这样Swiper插件就能监听并响应该容器上的触摸事件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值