Home > WeChat Applet > Mini Program Development > Implementation code for left-swipe deletion effect in WeChat mini-program

Implementation code for left-swipe deletion effect in WeChat mini-program

高洛峰
Release: 2017-02-23 15:32:37
Original
2659 people have browsed it

This article mainly introduces the implementation code of the left-swipe deletion effect of the WeChat applet. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.

Today we will talk about the implementation of the left-swipe deletion effect of the WeChat applet. Many APPs on the market are now using this effect. On a listView page, when an item is swiped left, A delete or other option will appear on the right side. The user experience is very good and the operation is very convenient. Today we use the WeChat applet to achieve this effect...

Let’s see the effect first

Implementation code for left-swipe deletion effect in WeChat mini-program

The effect to be achieved:

1. When sliding to the left, the item will follow the finger to move left, and two will appear on the right side. Clickable button

2. When the sliding distance is greater than half the width of the button and you release your finger, the item automatically slides to the left to display the button. When the sliding distance is less than half the width of the button, the item automatically returns to its original position and hides the button. .

Idea:

1. First, each item on the page is divided into upper and lower layers. The upper layer is placed with normal content, and the lower layer is placed with buttons displayed by sliding left. , this can use z-index to achieve layering.
2. The upper layer of the item uses absolute positioning. We manipulate the value of the left attribute to move it to the left.

3, we use the touch object provided by the WeChat applet api and three functions related to finger touch (touchstart, touchmove, touchend) to realize the item moving with the finger.

Implementation code for left-swipe deletion effect in WeChat mini-program

Implementation code for left-swipe deletion effect in WeChat mini-program

Since it is relatively simple, I will go directly to the code. Detailed explanations are in the comments of the code. First, look at the layout of the page.

<!--这是一个item的代码>
  <view class="address-item" wx:for="{{addressList}}" >
  <!--这里绑定了刚才说的3个函数分别为 touchS,touchM touchE-->
  <!--这里注意这个 style="{{item.txtStyle}}" ,这是我们一会再js中 将要设置的样式 -->
    <view style="{{item.txtStyle}}" bindtouchstart="touchS" bindtouchmove="touchM" bindtouchend="touchE" data-index="{{index}}" class="address-item-top" >
     <!--中间无关的代码已被我删除-->
    </view>

     <!--这里是左滑按钮部分----start-->
    <view bindtap="delItem" class="posit">
      <view class="editor" data-addressid="{{item.address.ID}}" catchtap="setDefaultAddress">设为默认地址</view>
      <view class="del" data-addressid="{{item.address.ID}}" data-index="{{index}}" catchtap="delAddress">删除</view>
    </view>
     <!--这里是左滑按钮部分----end-->
  </view>
Copy after login

Look at the js code again

Page({
 data:{
  addressList:[{"Contact":"钟诚","Mobile":13888888888,"Address":"江苏省苏州市工业园区创意产业园"},
{"Contact":"凹凸曼","Mobile":13666666666,"Address":"江苏省苏州市工业园区独墅湖体育馆"},
{"Contact":"图傲曼","Mobile":13666666666,"Address":"江苏省苏州市工业园区独墅湖体育馆"}],
  editIndex:0,
  delBtnWidth:150//删除按钮宽度单位(rpx)
 },
 onLoad:function(options){},
 //手指刚放到屏幕触发
 touchS:function(e){
  console.log("touchS"+e);
  //判断是否只有一个触摸点
  if(e.touches.length==1){
   this.setData({
    //记录触摸起始位置的X坐标
    startX:e.touches[0].clientX
   });
  }
 },
 //触摸时触发,手指在屏幕上每移动一次,触发一次
 touchM:function(e){
   console.log("touchM:"+e);
  var that = this
  if(e.touches.length==1){
   //记录触摸点位置的X坐标
   var moveX = e.touches[0].clientX;
   //计算手指起始点的X坐标与当前触摸点的X坐标的差值
   var disX = that.data.startX - moveX;
   //delBtnWidth 为右侧按钮区域的宽度
   var delBtnWidth = that.data.delBtnWidth;
   var txtStyle = "";
   if(disX == 0 || disX < 0){//如果移动距离小于等于0,文本层位置不变
    txtStyle = "left:0px";
   }else if(disX > 0 ){//移动距离大于0,文本层left值等于手指移动距离
    txtStyle = "left:-"+disX+"px";
    if(disX>=delBtnWidth){
     //控制手指移动距离最大值为删除按钮的宽度
     txtStyle = "left:-"+delBtnWidth+"px";
    }
   }
   //获取手指触摸的是哪一个item
   var index = e.currentTarget.dataset.index;
   var list = that.data.addressList;
   //将拼接好的样式设置到当前item中
   list[index].txtStyle = txtStyle; 
   //更新列表的状态
   this.setData({
    addressList:list
   });
  }
 },
 touchE:function(e){
   console.log("touchE"+e);
  var that = this
  if(e.changedTouches.length==1){
   //手指移动结束后触摸点位置的X坐标
   var endX = e.changedTouches[0].clientX;
   //触摸开始与结束,手指移动的距离
   var disX = that.data.startX - endX;
   var delBtnWidth = that.data.delBtnWidth;
   //如果距离小于删除按钮的1/2,不显示删除按钮
   var txtStyle = disX > delBtnWidth/2 ? "left:-"+delBtnWidth+"px":"left:0px";
   //获取手指触摸的是哪一项
   var index = e.currentTarget.dataset.index;
   var list = that.data.addressList;
   list[index].txtStyle = txtStyle; 
   //更新列表的状态
   that.setData({
    addressList:list
   });
  }
 }
Copy after login

The above is the entire content of this article, I hope It will be helpful to everyone's learning, and I hope everyone will support the PHP Chinese website.

For more articles related to the implementation code of the left-swipe deletion effect of the WeChat applet, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template