当前位置:   article > 正文

微信小程序-侧滑删除_微信小程序 侧边滑块删除

微信小程序 侧边滑块删除

简介      

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

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

使用效果

代码实现

side-view.wtml

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

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

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

  1. <!--slide-view/slide-view.wxml-->
  2. <movable-area class="container" style="width: {{width}}rpx;height: {{height}}rpx;">
  3. <movable-view direction="horizontal" class="movable-view" style="width: {{width + slideWidth}}rpx; height: {{height}}rpx;" inertia bind:touchend="onTouchEnd"
  4. damping="100"
  5. out-of-bounds
  6. x = "{{x}}"
  7. bind:touchstart="onTouchStart" >
  8. <view class="left" >
  9. <slot name= "left"></slot>
  10. </view>
  11. <view class= "right">
  12. <slot name="right"></slot>
  13. </view>
  14. </movable-view>
  15. </movable-area>
side-view.js

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

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

  1. Component({
  2. /**
  3. * 组件的属性列表
  4. */
  5. options: {
  6. multipleSlots: true
  7. },
  8. properties: {
  9. // 组件显示区域的宽度,rpx
  10. width: {
  11. type: Number,
  12. value: 750
  13. },
  14. // 组件显示区域的高度,rpx
  15. height: {
  16. type: Number,
  17. value: 0
  18. },
  19. // 组件滑动显示区域的宽度,rpx
  20. slideWidth: {
  21. type: Number,
  22. value: 0
  23. }
  24. },
  25. /**
  26. * 组件的初始数据
  27. */
  28. data: {
  29. x: 0
  30. },
  31. /**
  32. * 组件的方法列表
  33. */
  34. ready: function ready() {
  35. this.init();
  36. },
  37. ready: function () {
  38. this.initFunc()
  39. },
  40. methods: {
  41. initFunc: function(){
  42. // 获取右侧滑动显示区域的宽度
  43. var that = this;
  44. var query = wx.createSelectorQuery().in(this);
  45. query.select('.right').boundingClientRect(function (right) {
  46. that._slideWidth = right.width * 2; // rpx
  47. that._threshold = right.width / 2; // px
  48. that.setData({
  49. width:750,
  50. height:100,
  51. slideWidth: that._slideWidth
  52. }) //触发渲染
  53. }).exec();
  54. },
  55. onTouchStart: function onTouchStart(e) {
  56. this._startX = e.changedTouches[0].pageX;
  57. console.log('startX:'+this._startX)
  58. },
  59. // 当滑动范围超过阈值自动完成剩余滑动
  60. onTouchEnd: function onTouchEnd(e) {
  61. this._endX = e.changedTouches[0].pageX; // px
  62. console.log('endX:'+this._endX)
  63. var _endX = this._endX,
  64. _startX = this._startX,
  65. _threshold = this._threshold;
  66. if (_startX - _endX >= _threshold) {
  67. this.setData({
  68. x: -this._slideWidth
  69. });
  70. } else {
  71. this.setData({
  72. x: 0
  73. });
  74. }
  75. },
  76. }
  77. });

index.wtml

  1. <!--index.wxml-->
  2. <slide-view heigth = "100" width="750">
  3. <view class="left" slot = "left">
  4. 这是一段文字
  5. </view>
  6. <view class="right" slot = "right" width = "300">
  7. <view class="read">已读</view>
  8. <view class="delete">删除</view>
  9. </view>
  10. </slide-view>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/299465
推荐阅读
相关标签
  

闽ICP备14008679号