小程序列表左滑删除功能

原理

  • 外层一个view水平方向排列,里面包含一个内容区view,一个操作区view
  • 让你要展示的布局充满屏幕,通过css样式让超出的删除按钮隐藏
  • 监听touch事件,平移布局显示和隐藏删除按钮(列表每一项中有一个isTouchMove属性,通过监听touch改变该属性给列表不同的样式将隐藏的按钮显示出来)

代码

<view class="list-page">
  <view class="list-item {{user.isTouchMove?'list-item-touch-active':''}}" wx:for="{{list}}" wx:for-item="user" wx:for-index="index" wx:key="user.id" bindtouchstart="touchstart" bindtouchmove="touchmove" data-id="{{user.id}}">

    <view class="item-content">
      <view class="content-name">{{user.name}}</view>
      <view class="content-info"> 
        <text>{{user.phone}}</text>
        <text>{{user.sex}}</text>
      </view>
    </view>

    <view class="item-delete">删除</view>
  </view>
</view>

.list-page{
  display: flex;
  flex-direction: column;
  border-top: 2rpx solid #f0f0f0
}
.list-item{
  height: 160rpx;
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 2rpx solid #f0f0f0;
}
.item-content{
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 0 20rpx 0 20rpx;
  -webkit-transition: all 0.4s;
  transition: all 0.4s;
  -webkit-transform: translateX(180rpx);
  transform: translateX(180rpx); 
  margin-left: -200rpx;
}
.content-info{
  display: flex;
  width: 100%;
  justify-content: space-between;
  font-size: 32rpx;
  color: #999
}
.content-name{
  width: 100%;
}
.list-item-touch-active .item-content{
  margin-left: -100rpx;
}
.list-item-touch-active .item-content, .list-item-touch-active .item-delete {
  -webkit-transform: translateX(0) !important;
  transform: translateX(0) !important;
}
.item-delete{
  width: 100rpx;
  height: 160rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  background: red;
  color: #fff;
  font-size: 32rpx;
  -webkit-transform: translateX(180rpx);
  transform: translateX(180rpx);
  -webkit-transition: all 0.4s;
  transition: all 0.4s;
}
// pages/list/list.js
const App = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[
      {
        id:1,
        name:'张三',
        phone:'15955040222',
        sex:'男',
        isTouchMove:false,
      },
      {
        id: 2,
        name: '张三',
        phone: '15955040222',
        sex: '男',
        isTouchMove: false,
      },
      {
        id: 3,
        name: '张三',
        phone: '15955040222',
        sex: '男',
        isTouchMove: false,
      },
      {
        id: 4,
        name: '张三',
        phone: '15955040222',
        sex: '男',
        isTouchMove: false,
      },
      {
        id: 5,
        name: '张三',
        phone: '15955040222',
        sex: '男',
        isTouchMove: false,
      },
      {
        id: 6,
        name: '张三',
        phone: '15955040222',
        sex: '男',
        isTouchMove: false,
      },
      {
        id: 7,
        name: '张三',
        phone: '15955040222',
        sex: '男',
        isTouchMove: false,
      },
    ]
  },
  touchstart: function (e) {
    //开始触摸时 重置所有删除
    let data = App.touch._touchstart(e, this.data.list) //将修改过的list setData
    this.setData({
      list: data
    })
  },

  //滑动事件处理
  touchmove: function (e) {
    let data = App.touch._touchmove(e, this.data.list,'id')//将修改过的list setData
    this.setData({
      list: data
    })
  },
})

对于滑动事件的处理专门封装了一个touch.js文件

var startX
var startY
class touch {

  constructor() {
  }

  _touchstart(e, items) {
    //开始触摸时 重置所有删除
    items.forEach(function (v, i) {
      if (v.isTouchMove) //只操作为true的
        v.isTouchMove = false;
    })

    startX = e.changedTouches[0].clientX
    startY = e.changedTouches[0].clientY

    return items
  }

  _touchmove(e, items,key) {
      const id = e.currentTarget.dataset.id, //获取列表中每一项的唯一值,可以取id
      touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标
      touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标
      //获取滑动角度
      angle = this._angle({
        X: startX,
        Y: startY
      }, {
          X: touchMoveX,
          Y: touchMoveY
        });
    items.forEach(function (v, i) {
      v.isTouchMove = false
      //滑动超过30度角 return
      if (Math.abs(angle) > 30) return;
      if (v[key] == id) { //判断滑动的id与列表中的id是否一致,如果是的话,改变滑动这一项的isTouchMove属性
        if (touchMoveX > startX) //右滑
          v.isTouchMove = false
        else //左滑
          v.isTouchMove = true
      }
    })
    return items
  }

  _angle(start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    //返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  }
}

export default touch

引用这个touch.js文件,在app.js文件中

//app.js
import touch from './utils/touch.js'//新加
App({
  globalData: {
    userInfo: null
  },
  touch: new touch() //实例化这个touch对象
})

主要就是先使用css将删除按钮隐藏起来,然后通过监听touch事件去改变列表中每一项的一个属性,间接修改这个条目的样式将删除按钮显示出来

 

posted @   JackieDYH  阅读(10)  评论(0编辑  收藏  举报  
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示