小程序 拖动节点

小程序 拖动节点

设置浮动元素事件,并且可拖动的功能。

data:{
    translateX: 0, // 位移x坐标 单位px
    translateY: 0, // 位移y坐标 单位px
    distance: 0, // 双指接触点距离
    startMove: { // 起始位移距离
      x: 0,
      y: 0,
    },
    startTouches: [] // 起始点touch数组
 }
 


touchmove(e) {const touches
= e.touchesconst { pageX: onePageX, pageY: onePageY } = touches[0]const { startMove } = this.datathis.setData({translateX: onePageX - startMove.x,translateY: onePageY - startMove.y})},touchstart(e) {const touches = e.touchesconst { translateX, translateY } = this.dataconst { pageX, pageY } = touches[0]this.data.startMove = {x: pageX - translateX,y: pageY - translateY}this.data.startTouches = touches},

设置访问展示元素;

<view style ="transform: translate({{translateX}}px, {{translateY}}px)" bind:touchmove="touchmove" bind:touchstart="touchstart" style="position: fixed; top: 0">123</