小程序 拖动节点

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

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


touchmove(e) { const touches
= e.touches const { pageX: onePageX, pageY: onePageY } = touches[0] const { startMove } = this.data this.setData({ translateX: onePageX - startMove.x, translateY: onePageY - startMove.y }) }, touchstart(e) { const touches = e.touches const { translateX, translateY } = this.data const { 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</

  

posted @ 2025-10-13 17:48  微宇宙  阅读(5)  评论(0)    收藏  举报