欢迎光临,了解微信小程序开发,就上易用通!

列表左右滑动,左滑删除功能实现

发布:2018-01-25 11:39浏览: 来源:网络 作者:tianshu

列表左右滑动,左滑删除功能实现(图1) 

关键js代码如下:


  1. //关闭所有列表的的active
  2.    closeLeftActive : function(sendUserId){
  3.        var front__lists = this.data.front__lists,
  4.            lists = this.data.lists,
  5.            result;
  6.        const fllen = front__lists.length,
  7.              llen = lists.length;
  8.         
  9.        for(var i = 0; i < fllen; i++){
  10.            front__lists[i]['isMoveLeft'] = false;
  11.            front__lists[i]['moveLeftX'] = 0;
  12.            if(sendUserId && sendUserId == front__lists[i]['sendUserId']){
  13.                this.touchElement = {
  14.                    t : 'front__lists',
  15.                    i : i
  16.                }
  17.            }
  18.        }
  19.        for(var i = 0; i < llen; i++){
  20.            lists[i]['isMoveLeft'] = false;
  21.            lists[i]['moveLeftX'] = 0;
  22.            if(sendUserId && sendUserId == lists[i]['sendUserId']){
  23.                this.touchElement = {
  24.                    t : 'lists',
  25.                    i : i
  26.                }
  27.            }
  28.        }
  29.        this.setData({
  30.            front__lists : front__lists,
  31.            lists : lists
  32.        });       
  33.    },
  34.    moveX : 0,//全局点击按下时的X坐标
  35.    touchElement : {},//当前点击的列表元素
  36.    isRedirect : true,//是否松开时跳转,默认如果滑动未超过10像素,跳转至详情页
  37.    //点击按下时
  38.    listtouchStart : function(e){
  39.        this.moveX = e.changedTouches[0].clientX;//获取当前点击按下时的的X坐标
  40.        const senduserid = e.currentTarget.dataset.senduserid;//获取当前列表元素的ID
  41.        this.closeLeftActive(senduserid);//关闭所有元素的滑动样式,并且获取到当前点击的元素在this.data中的位置
  42.        this.isRedirect = true;//点击默认改成能跳转
  43.    },
  44.    //点击移动时
  45.    listtouchMove : function(e){
  46.        const moveX = e.changedTouches[0].clientX;//获取移动到新位置时的坐标
  47.        var mx = this.moveX - moveX;//计算得到与按下时的X坐标移动多少像素
  48.        if(mx > 10){//如果像素大于10像素
  49.            this.isRedirect = false;//则不跳转      
  50.            /**将新的位置赋值到全局data渲染页面, */   
  51.            this.setData((function(ele,val,bool){
  52.                var obj = {};
  53.                obj[ele+'moveLeftX'] = val;//左移的值
  54.                obj[ele+'isMoveLeft'] = bool;//控制是否超过40像素,则直接添加active样式类
  55.                return obj;
  56.            })(this.touchElement.t + '[' + this.touchElement.i +']', mx, (mx >= 40)));
  57.        }else if(mx < 0){//如果是右滑,也不跳转
  58.            this.isRedirect = false;
  59.        }
  60.  
  61.    },
  62.    //点击松开时
  63.    listtouchEnd : function(e){
  64.        if(this.isRedirect){//如果能跳转则跳转至新的详情页面
  65.            const senduserid = e.currentTarget.dataset.senduserid;//获取参数
  66.            wx.navigateTo({
  67.               url: 'dialog?sendUserId=' + senduserid
  68.            });
  69.            this.closeLeftActive();//初始化关闭所有打开的滑动效果
  70.            return;
  71.        }
  72.        const moveX = e.changedTouches[0].clientX;
  73.        var mx = this.moveX - moveX;
  74.        if(mx < 40){//如果松开时位移小于40像素则回弹关闭
  75.            this.closeLeftActive();
  76.        }
  77.    },
  78.    //点击被取消时,如中途来电话了等,初始化关闭所有打开的滑动效果
  79.    listtouchcancel : function(e){
  80.        this.closeLeftActive();
  81.    }


页面wxml文档
  1. <view class="lists">
  2.     <view class="contain">
  3.         <block wx:for="{{lists}}" wx:key="{{item.sendUserId}}">
  4.             <view class="list">
  5.                 <view class="list__content{{item.isMoveLeft ? ' active' : ''}}" style="left:-{{item.moveLeftX}}px;right:{{item.moveLeftX}}px;" data-sendUserId="{{item.sendUserId}}" catchtouchstart="listtouchStart" catchtouchend="listtouchEnd" catchtouchcancel="listtouchcancel" catchtouchmove="listtouchMove">
  6.                     <view class="list__hd">
  7.                         <image src="{{item.img}}"/>
  8.                         <view class="list__msgnumber" wx:if="{{item.silence}}"></view>
  9.                         <view wx:elif="{{item.msgNumber > 0}}" class="list__messagenumber">
  10.                             <text hidden="{{item.silence}}" style="font-size:{{item.msgNumber > 99 ? '17' : '22'}}rpx;">{{item.msgNumber > 99 ? '99+' : item.msgNumber}}</text>
  11.                         </view>
  12.                     </view>
  13.                     <view class="list__bd">
  14.                         <view class="list__h3">
  15.                             <text class="list__name{{item.isteam ? ' list__teamname' : ''}}">{{item.name}}</text>
  16.                             <text class="list__sendtime">{{item.sendtime}}</text>
  17.                         </view>
  18.                         <view class="list__p{{item.silence ? ' silence' : ''}}">{{(item.silence && item.msgNumber > 0) ? '['+item.msgNumber +'条]' : ''}}{{item.desc}}</view><image wx:if="{{item.silence}}" class="list__silence" src="/image/silence.png"></image>
  19.                     </view>
  20.                     <view class="list__ft">
  21.                      
  22.                     </view>
  23.                 </view>
  24.                 
  25.                 <view class="list__bg_btns">
  26.                     <view class="list__bg_btn_gray">{{item.isteam ? '取消关注' : '标为已读'}}</view>
  27.                     <view class="list__bg_btn_red">删除</view>
  28.                 </view>
  29.             </view>
  30.         </block>
  31.     </view>
  32. </view>





免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。