微信小程序 ----- 按钮拖拽

<view class="body-suspension" style="top: {{UDnumber}}px; left: {{LRnumber}}px;"
      @touchstart="touchstart"
      @touchmove="touchmove"
      @touchend="touchend">
  拖拽
</view>

data中  

LRnumber: 300,
UDnumber: 370,

onload中获取频幕宽高:

this.windowHeight = wx.getSystemInfoSync().windowHeight
this.windowWidth = wx.getSystemInfoSync().windowWidth

methods中:

// 点击时的初始位置
touchstart(e) {
  this.startSeat = e.touches[0]
},
// 移动时的位置
touchmove(e) {
  let vm = this
  let endPoint = e.touches[e.touches.length - 1]
  let translateX = endPoint.clientX - vm.startSeat.clientX
  let translateY = endPoint.clientY - vm.startSeat.clientY
  this.startSeat = endPoint
  let buttonTop = this.UDnumber + translateY
  let buttonLeft = this.LRnumber + translateX
  // 判断是移动否超出屏幕
  if ((buttonLeft + 63) >= this.windowWidth) {
    buttonLeft = this.windowWidth - 63
  }
  if (buttonLeft <= 0) {
    buttonLeft = 0
  }
  if (buttonTop <= 0) {
    buttonTop = 0
  }
  if (buttonTop + 63 >= this.windowHeight) {
    buttonTop = this.windowHeight - 63
  }
  this.UDnumber = buttonTop
  this.LRnumber = buttonLeft
  vm.$apply()
},
// 松开时的终末位置
touchend(e) {},

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值