欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信小程序 实现列表项滑动显示删除按钮的功能

程序员文章站 2024-01-23 19:04:52
微信小程序 实现列表项滑动显示删除按钮的功能 微信小程序并没有提供列表控件,所以也没有ios上惯用的列表项左滑删除的功能,so只能自己干了。 原理很简单,用2个层,上面...

微信小程序 实现列表项滑动显示删除按钮的功能

微信小程序并没有提供列表控件,所以也没有ios上惯用的列表项左滑删除的功能,so只能自己干了。

原理很简单,用2个层,上面的层显示正常的内容,下面的层显示一个删除按钮,就是记录手指滑动的距离,动态的来移动上层元素,当然上层用绝对定位。

wxml:

<view class="container">
 <view class="record-box" data-datetime="{{record.datetime}}" wx:for="{{detaillist}}" wx:for-item="record">
  <view class="record" style="left:{{record.offsetx}}px" bindtouchstart="recordstart" catchtouchmove="recordmove" bindtouchend="recordend">
   <view class="left">
    <view>{{record.type}} {{record.count+record.unit}}</view>
    <view class="summary">{{record.remark}}</view>
   </view>
   <view class="right">
    {{record.datetime}}
   </view>
  </view>
  <view class="delete-box">
   <view>删除</view>
  </view>
 </view>
</view>

 wxss:

@import "../common/weui.wxss";
 
.container {
 box-sizing: border-box;
 padding: 0 0 0 0;
}
 
 
 
.record {
 display: flex;
 flex-direction: row;
 align-items: center;
 width:100%;
  height: 120rpx;
 position: absolute;
 justify-content: space-between;
 background-color: #fff;
}
 
.record .right{
 margin-right: 30rpx;
   color: #888888;
}
.record .left{
 margin-left: 30rpx;
  display: flex;
  flex-direction: column;
 justify-content: space-between;
}
 
.record .left .summary{
  color: #aaa;
 font-size: 30rpx;
 line-height: 30rpx;
 
}
 
.record-box{
 height: 120rpx;
 width: 100%;
 border-bottom: 1rpx solid #ddd;
 background-color: #fff;
}
 
.delete-box{
 background-color: #e64340;
 color: #ffffff;
 float: right;
 height: 100%;
 width: 80px;
 display: flex;
 align-items: center;
 justify-content: center;
}
 
.record-box:last-child {
 border-bottom: 0;
}
 
.record .r-item {
  
}
  

js代码:

var detaillist = [
  { datetime: '2017-01-01 17:00', count: 100, unit: 'ml', type: '开水', remark: '哈哈哈哈' },
  { datetime: '2017-01-01 17:01', count: 100, unit: 'ml', type: '开水' },
  { datetime: '2017-01-01 17:02', count: 100, unit: 'ml', type: '开水' }
];
var recordstartx = 0;
var currentoffsetx = 0;
page(
  {
    data: {
      detaillist: detaillist
    }
    ,
    recordstart: function (e) {
      recordstartx = e.touches[0].clientx;
      currentoffsetx = this.data.detaillist[0].offsetx;
      console.log('start x ', recordstartx);
    }
    ,
    recordmove: function (e) {
      var detaillist = this.data.detaillist;
      var item = detaillist[0];
      var x = e.touches[0].clientx;
      var mx = recordstartx - x;
      console.log('move x ', mx);

      var result = currentoffsetx - mx;
      if (result >= -80 && result <= 0) {
        item.offsetx = result;
      }
      this.setdata({
        detaillist: detaillist
      });
    }
    ,
    recordend: function (e) {
      var detaillist = this.data.detaillist;
      var item = detaillist[0];
      console.log('end x ', item.offsetx);

      if (item.offsetx < -40) {
        item.offsetx = -80;

      } else {
        item.offsetx = 0;

      }
      this.setdata({
        detaillist: detaillist
      });
    }

  }
);

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!