当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序 向左滑动删除功能的实现

微信小程序 向左滑动删除功能的实现

2019年06月11日  | 移动技术网IT编程  | 我要评论

微信小程序 向左滑动删除功能的实现

实现效果图:

实现代码:

1、wxml touch-item元素绑定了bindtouchstart、bindtouchmove事件

<view class="container">
 <view class="touch-item {{item.istouchmove ? 'touch-move-active' : ''}}" data-index="{{index}}" bindtouchstart="touchstart" bindtouchmove="touchmove" wx:for="{{items}}" wx:key="">
  <view class="content">{{item.content}}</view>
  <view class="del" catchtap="del" data-index="{{index}}">删除</view>
 </view>
</view>

2、wxss flex布局、css3动画

.touch-item {
 font-size: 14px;
 display: flex;
 justify-content: space-between;
 border-bottom:1px solid #ccc;
 width: 100%;
 overflow: hidden
}
.content {
 width: 100%;
 padding: 10px;
 line-height: 22px;
 margin-right:0;
 -webkit-transition: all 0.4s;
 transition: all 0.4s;
 -webkit-transform: translatex(90px);
 transform: translatex(90px);
 margin-left: -90px
}
.del {
 background-color: orangered;
 width: 90px;
 display: flex;
 flex-direction: column;
 align-items: center;
 justify-content: center;
 color: #fff;
 -webkit-transform: translatex(90px);
 transform: translatex(90px);
 -webkit-transition: all 0.4s;
 transition: all 0.4s;
}
.touch-move-active .content,
.touch-move-active .del {
 -webkit-transform: translatex(0);
 transform: translatex(0);
}

3、js 注释很详细

var app = getapp()
page({
 data: {
  items: [],
  startx: 0, //开始坐标
  starty: 0
 },
 onload: function () {
  for (var i = 0; i < 10; i++) {
   this.data.items.push({
    content: i + " 向左滑动删除哦,向左滑动删除哦,向左滑动删除哦,向左滑动删除哦,向左滑动删除哦",
    istouchmove: false //默认全隐藏删除
   })
  }
  this.setdata({
   items: this.data.items
  })
 },
 //手指触摸动作开始 记录起点x坐标
 touchstart: function (e) {
  //开始触摸时 重置所有删除
  this.data.items.foreach(function (v, i) {
   if (v.istouchmove)//只操作为true的
    v.istouchmove = false;
  })
  this.setdata({
   startx: e.changedtouches[0].clientx,
   starty: e.changedtouches[0].clienty,
   items: this.data.items
  })
 },
 //滑动事件处理
 touchmove: function (e) {
  var that = this,
   index = e.currenttarget.dataset.index,//当前索引
   startx = that.data.startx,//开始x坐标
   starty = that.data.starty,//开始y坐标
   touchmovex = e.changedtouches[0].clientx,//滑动变化坐标
   touchmovey = e.changedtouches[0].clienty,//滑动变化坐标
   //获取滑动角度
   angle = that.angle({ x: startx, y: starty }, { x: touchmovex, y: touchmovey });
  that.data.items.foreach(function (v, i) {
   v.istouchmove = false
   //滑动超过30度角 return
   if (math.abs(angle) > 30) return;
   if (i == index) {
    if (touchmovex > startx) //右滑
     v.istouchmove = false
    else //左滑
     v.istouchmove = true
   }
  })
  //更新数据
  that.setdata({
   items: that.data.items
  })
 },
 /**
  * 计算滑动角度
  * @param {object} start 起点坐标
  * @param {object} end 终点坐标
  */
 angle: function (start, end) {
  var _x = end.x - start.x,
   _y = end.y - start.y
  //返回角度 /math.atan()返回数字的反正切值
  return 360 * math.atan(_y / _x) / (2 * math.pi);
 },
 //删除事件
 del: function (e) {
  this.data.items.splice(e.currenttarget.dataset.index, 1)
  this.setdata({
   items: this.data.items
  })
 }
})

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网