uniapp触摸拖动元素改变位置

<template>
  <view class="container">
    <view class="draggable" 
        @touchmove.stop.prevent="moveHandle"
		@touchmove="handleTouchMove" @touchstart="handleTouchStart"                 
        @touchend="handleTouchEnd"
        :style="{transform: 'translate(' + offset.x + 'px, ' + offset.y + 'px)'}">
      拖动我
    </view>
  </view>
</template>
 
<script>
export default {
  data() {
    return {
      offset: { x: 0, y: 0 },
      startPoint: { x: 0, y: 0 }
    };
  },
  methods: {
    handleTouchStart(event) {
      this.startPoint = { x: event.touches[0].clientX, y: event.touches[0].clientY };
    },
    handleTouchMove(event) {
      const currentPoint = { x: event.touches[0].clientX, y: event.touches[0].clientY };
      const delta = {
        x: currentPoint.x - this.startPoint.x,
        y: currentPoint.y - this.startPoint.y
      };
      this.offset = { x: this.offset.x + delta.x, y: this.offset.y + delta.y };
      this.startPoint = currentPoint;
      // 限制拖动范围
	  this.restrictDrag();
    },
    handleTouchEnd() {
      // 可以在这里处理拖动结束后的逻辑
    },
   //获取父元素宽度高度使其防止拖出页面
    restrictDrag() {
				uni.createSelectorQuery()
					.select('.container') // 选择你要获取尺寸的盒子元素,这里假设盒子元素的class为box
					.boundingClientRect((res) => {
						const maxX = res.width - 98; // 屏幕宽度,单位为px
						// console.log('宽度111', res)
						const maxY = res.height - 98; // 屏幕高度,单位为px
						const minX = 0; // 边距
						const minY = 0; // 边距
						// console.log('宽度', this.offset.x, '-----', maxX)
						// console.log('高度', this.offset.y, '-----', maxY)
						if (this.offset.x > maxX) {

							this.offset.x = maxX;
						} else if (this.offset.x < minX) {
							this.offset.x = minX;
						}

						if (this.offset.y > maxY) {
							this.offset.y = maxY;
						} else if (this.offset.y < minY) {
							this.offset.y = minY;
						}
					})
					.exec();
	},
     //阻止其他元素触摸
	moveHandle() {
	    return false;
	},
  }
};
</script>
 
<style>
.container {
  overflow: hidden;
  position: relative;
}
.draggable {
  width: 100px;
  height: 100px;
  background-color: #f0f0f0;
  position: absolute;
  cursor: move;
  user-select: none;
}
</style>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在uniapp中实现拖拽元素更换位置,可以使用以下步骤: 1. 在页面中创建需要拖拽元素,并设置其拖拽事件。 2. 在拖拽事件中获取拖拽元素位置信息,并在移动过程中实时更新其位置。 3. 在拖拽结束事件中获取拖拽元素当前位置,并计算出其应该移动到的位置。 4. 将拖拽元素移动到新位置,并更新其他元素位置以保持布局的一致性。 以下是一个简单的示例代码,以实现拖拽元素更换位置: ``` <template> <view class="container"> <view class="item" v-for="(item, index) in itemList" :key="item.id" :style="{ top: item.top + 'px', left: item.left + 'px' }" @touchstart="dragStart(index, $event)" @touchmove="dragMove(index, $event)" @touchend="dragEnd(index, $event)"> {{ item.text }} </view> </view> </template> <script> export default { data() { return { itemList: [ { id: 1, text: 'Item 1', top: 100, left: 100 }, { id: 2, text: 'Item 2', top: 200, left: 100 }, { id: 3, text: 'Item 3', top: 300, left: 100 }, ], draggingIndex: -1, dragStartX: 0, dragStartY: 0, }; }, methods: { dragStart(index, event) { this.draggingIndex = index; this.dragStartX = event.touches[0].pageX; this.dragStartY = event.touches[0].pageY; }, dragMove(index, event) { if (this.draggingIndex === index) { const offsetX = event.touches[0].pageX - this.dragStartX; const offsetY = event.touches[0].pageY - this.dragStartY; const item = this.itemList[index]; item.top += offsetY; item.left += offsetX; this.dragStartX = event.touches[0].pageX; this.dragStartY = event.touches[0].pageY; } }, dragEnd(index, event) { if (this.draggingIndex === index) { const item = this.itemList[index]; const newIndex = this.findNewIndex(item); item.top = newIndex * 100 + 100; item.left = 100; this.itemList.splice(index, 1); this.itemList.splice(newIndex, 0, item); this.draggingIndex = -1; } }, findNewIndex(item) { const newIndex = Math.round(item.top / 100); if (newIndex < 0) return 0; if (newIndex >= this.itemList.length) return this.itemList.length - 1; return newIndex; }, }, }; </script> ``` 在上述代码中,我们创建了一个包含三个元素的列表,并为每个元素添加了拖拽事件。在拖拽事件中,我们通过计算偏移量来实时更新元素位置,并在拖拽结束时计算出元素应该移动到的位置,并更新其他元素位置以保持布局的一致性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值