微信小程序实现左滑修改、删除功能

作者:袖梨 2022-06-25

本文实例为大家分享了微信小程序实现左滑修改、删除的具体代码,供大家参考,具体内容如下

wxml:


  
  
  
   
    
     
      
       
        {{item.title}}党建项目报价表
       
       
      
      
       
        {{item.create_time}}
       
      
     
    
   

   
   
   
  


wxss:

css;">.offer-item {
 height: 150rpx;
 width: 100vw;
 overflow-x: hidden;
 border-bottom: 1px solid #f0f0f0;
}

.offer-item>view {
 position: absolute;
 /* width: calc(100% + 200rpx); */
 height: 150rpx; 
}

.offer-item .offer-item-top {
 height: 100%;
}

.offer-item .offer-item-top navigator {
 display: inline-block;
 width: 100vw; 
 height: 100%;
}

.offer-item .content {
 padding: 35rpx 30rpx;
 position: relative;
 height: calc(100% - 70rpx);
}

.offer-item .title .fl {
 display: inline-block;
 width: 78%;
 overflow: hidden;
 text-overflow: ellipsis;
 white-space: nowrap;
 font-size: 30rpx;
 color: #333;
}

.offer-item .title .fr {
 display: inline-block;
 width: 20rpx;
 height: 26rpx;
 margin-top: 5rpx;
}

.offer-item .note {
 position: absolute;
 left: 30rpx;
 bottom: 35rpx;
 width: calc(100vw - 60rpx);
 font-size: 24rpx;
 color: #999;
}

.offer-item .posit {
 width: 200rpx;
 height: 150rpx;
 line-height: 150rpx;
 text-align: center;
 display: none
}

.posit.isMove {
 display: inline-block;
 position: absolute;
}

.posit.isMove[hidden] {
 display: none
}

.offer-item .posit>view {
 display: inline-block;
 width: 100rpx;
}

.offer-item .posit>view:first-of-type {
 background-color: #FED847;
}

.offer-item .posit>view:last-of-type {
 background-color: #C71B1B;
}

.offer-item .posit image {
 display: inline-block;
 width: 36rpx;
 height: 36rpx;
}

js:

let len = 0;     // 初次加载长度
let hadLastPage = false; // 判断是否到最后一页

var initdata = function (that) {
 var list = that.data.offerList
 for (var i = 0; i  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.offerList;
   // 将拼接好的样式设置到当前item中
   list[index].txtStyle = txtStyle;

   list[index].isMove = true;
   // 更新列表的状态
   this.setData({
    offerList: list
   });
  }
 },
 touchE: function (e) {
  console.log( 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.offerList;
   list[index].txtStyle = txtStyle;
   // 更新列表的状态
   that.setData({
    offerList: list
   });
  }
 },

 /**
  * 
  */
 navigatorTo: function (event) {

 },

 /**
  * 删除操作
  */
 del: function (e) {
  var that = this;
  var data = {
   id: e.currentTarget.dataset.index
  };
  wx.showModal({
   title: '',
   content: '确定选择删除么?',
   confirmColor: '#C71B1B',
   cancelColor: '#666666',
   success: function (res) {
    if (res.confirm) {
     utils.requestFun("接口url", data, 'POST', function (msg) {
      console.log(msg)

      wx.showToast({
       title: '删除成功',
       icon: 'success',
       duration: 1500
      })
      var lists = that.data.offerList;
      var list1 = [];
      for (let i = 0; i 

相关文章

精彩推荐