微信小程序-侧滑删除

简介

movable-view和movable-area是可移动的视图容器,在页面中可以拖拽滑动。

本篇文章将会通过该容器实现一个常用的拖拽按钮功能。

使用效果

代码实现

side-view.wtml

布局见下面代码,left view为内容区域,right view为操作按钮,在使用时候只需要替换left和right 对应slot即可。

movable-area 宽度 为left宽度,movable-view宽度为left + right宽度

监听事件为鼠标点击和鼠标弹起事件。

html 复制代码
<!--slide-view/slide-view.wxml-->
<movable-area class="container" style="width: {{width}}rpx;height: {{height}}rpx;">
  <movable-view direction="horizontal" class="movable-view" style="width: {{width + slideWidth}}rpx; height: {{height}}rpx;" inertia bind:touchend="onTouchEnd" 
  damping="100"
  out-of-bounds
  x = "{{x}}" 
  bind:touchstart="onTouchStart" >
    <view class="left" >
      <slot name= "left"></slot>
    </view>
    <view class= "right">
      <slot name="right"></slot>
    </view>
  </movable-view>
</movable-area>
side-view.js

组件的state 为 x, x表示movable-view 水平位置,在鼠标弹起时候,若移动距离大于threshold,则设置x为-threshold,否则为0。

注意: 事件event 对应位置,大小单位为px,而我们设置的参数都是rpx,若对其计算时候,注意两者之间转换。

javascript 复制代码
Component({
  /**
   * 组件的属性列表
   */
  options: {
    multipleSlots: true
  },
  properties: {
    //  组件显示区域的宽度,rpx
    width: {
      type: Number,
      value: 750
    },
    //  组件显示区域的高度,rpx
    height: {
      type: Number,
      value: 0
    },
    //  组件滑动显示区域的宽度,rpx
    slideWidth: {
      type: Number,
      value: 0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
   
    x: 0
   
  },

  /**
   * 组件的方法列表
   */
  ready: function ready() {
    this.init();
  },
  ready: function () {
    this.initFunc()
  },

  methods: {
    initFunc: function(){
      // 获取右侧滑动显示区域的宽度
      var that = this;
      var query = wx.createSelectorQuery().in(this);
      query.select('.right').boundingClientRect(function (right) {    
        that._slideWidth = right.width * 2; // rpx
        that._threshold = right.width / 2; // px
        that.setData({
          width:750,
          height:100,
          slideWidth: that._slideWidth
        }) //触发渲染
      }).exec();
    },
    onTouchStart: function onTouchStart(e) {
      this._startX = e.changedTouches[0].pageX;
      console.log('startX:'+this._startX)
    },

    //  当滑动范围超过阈值自动完成剩余滑动
    onTouchEnd: function onTouchEnd(e) {
      this._endX = e.changedTouches[0].pageX; // px
      console.log('endX:'+this._endX)
      var _endX = this._endX,
          _startX = this._startX, 
          _threshold = this._threshold;
    
      if (_startX - _endX >= _threshold) {
        this.setData({
          x: -this._slideWidth
        });
      } else {
        this.setData({
          x: 0
        });
      } 
    },

    
  }
});

index.wtml

html 复制代码
<!--index.wxml-->

<slide-view heigth = "100" width="750">
  <view class="left" slot = "left">
    这是一段文字
  </view>
  <view class="right" slot = "right" width = "300">
    <view class="read">已读</view>
    <view class="delete">删除</view>
  </view>
</slide-view>
相关推荐
游戏开发爱好者88 小时前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063210 小时前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063210 小时前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone
宠友信息11 小时前
2025社交+IM及时通讯社区APP仿小红书小程序
java·spring boot·小程序·uni-app·web app
“负拾捌”12 小时前
python + uniapp 结合腾讯云实现实时语音识别功能(WebSocket)
python·websocket·微信小程序·uni-app·大模型·腾讯云·语音识别
换日线°1 天前
NFC标签打开微信小程序
前端·微信小程序
光影少年1 天前
AIGC + Taro / 小程序
小程序·aigc·taro
2501_915918411 天前
在 iOS 环境下查看 App 详细信息与文件目录
android·ios·小程序·https·uni-app·iphone·webview
2501_916007471 天前
没有 Mac 用户如何上架 App Store,IPA生成、证书与描述文件管理、跨平台上传
android·macos·ios·小程序·uni-app·iphone·webview
天空属于哈夫克31 天前
Go 语言实战:构建一个企微外部群“技术贴收藏夹”小程序后端
小程序·golang·企业微信