当前位置:首页 > uni-app

uniapp拖拽填空

2026-02-05 22:35:25uni-app

uniapp拖拽填空实现方法

使用HTML5拖拽API

在uniapp中可以通过HTML5原生拖拽API实现拖拽填空功能。需要监听dragstartdragoverdrop事件。

<template>
  <view>
    <!-- 可拖拽元素 -->
    <view 
      v-for="(item, index) in items" 
      :key="index"
      draggable="true"
      @dragstart="dragStart(index)"
    >
      {{ item.text }}
    </view>

    <!-- 填空区域 -->
    <view 
      class="drop-area"
      @dragover.prevent
      @drop="handleDrop"
    >
      拖拽到这里
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { text: '选项1' },
        { text: '选项2' },
        { text: '选项3' }
      ],
      draggedIndex: null
    }
  },
  methods: {
    dragStart(index) {
      this.draggedIndex = index
    },
    handleDrop() {
      if (this.draggedIndex !== null) {
        // 处理拖拽后的逻辑
        console.log('拖拽了:', this.items[this.draggedIndex].text)
      }
    }
  }
}
</script>

使用第三方插件

uniapp社区提供了多种拖拽组件,如uni-dragvue-draggable等。这些插件封装了拖拽逻辑,使用更方便。

uniapp拖拽填空

安装vue-draggable:

npm install vuedraggable

使用示例:

uniapp拖拽填空

<template>
  <view>
    <draggable 
      v-model="items"
      @end="onDragEnd"
    >
      <view v-for="item in items" :key="item.id">
        {{ item.text }}
      </view>
    </draggable>
  </view>
</template>

<script>
import draggable from 'vuedraggable'
export default {
  components: { draggable },
  data() {
    return {
      items: [
        { id: 1, text: '选项1' },
        { id: 2, text: '选项2' },
        { id: 3, text: '选项3' }
      ]
    }
  },
  methods: {
    onDragEnd() {
      console.log('拖拽结束', this.items)
    }
  }
}
</script>

自定义拖拽实现

对于更复杂的需求,可以结合touch事件实现自定义拖拽效果。通过监听touchstarttouchmovetouchend事件,计算元素位置变化。

<template>
  <view>
    <view 
      v-for="(item, index) in items"
      :key="index"
      :style="{ transform: `translate(${item.x}px, ${item.y}px)` }"
      @touchstart="startDrag(index, $event)"
      @touchmove="onDrag($event, index)"
      @touchend="endDrag"
      class="draggable-item"
    >
      {{ item.text }}
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { text: '选项1', x: 0, y: 0 },
        { text: '选项2', x: 0, y: 0 },
        { text: '选项3', x: 0, y: 0 }
      ],
      dragStartPos: { x: 0, y: 0 },
      currentDragIndex: null
    }
  },
  methods: {
    startDrag(index, e) {
      this.currentDragIndex = index
      this.dragStartPos = {
        x: e.touches[0].clientX - this.items[index].x,
        y: e.touches[0].clientY - this.items[index].y
      }
    },
    onDrag(e, index) {
      if (this.currentDragIndex !== null) {
        this.$set(this.items, index, {
          ...this.items[index],
          x: e.touches[0].clientX - this.dragStartPos.x,
          y: e.touches[0].clientY - this.dragStartPos.y
        })
      }
    },
    endDrag() {
      this.currentDragIndex = null
    }
  }
}
</script>

填空区域检测

实现拖拽填空功能时,需要检测元素是否被拖拽到目标区域。可以通过计算元素位置与目标区域的位置关系来判断。

methods: {
  checkDropZone(position) {
    const dropZone = uni.createSelectorQuery().select('.drop-area')
    dropZone.boundingClientRect(data => {
      if (position.x > data.left && 
          position.x < data.right &&
          position.y > data.top && 
          position.y < data.bottom) {
        console.log('元素已放入填空区域')
      }
    }).exec()
  }
}

注意事项

  • 在H5端使用HTML5拖拽API效果最好,但在小程序端可能需要使用自定义touch事件实现
  • 拖拽性能优化:对于复杂列表,使用v-for时应添加合适的:key
  • 跨平台兼容性测试:不同平台对拖拽事件的支持程度不同,需要充分测试

标签: 拖拽uniapp
分享给朋友:

相关文章

vue拖拽容器实现

vue拖拽容器实现

实现 Vue 拖拽容器的基本方法 使用 HTML5 原生拖拽 API 通过 draggable 属性和拖拽事件(dragstart、dragend、dragover、drop)实现基础拖拽功能。适用于…

uniapp消息推送

uniapp消息推送

uniapp消息推送实现方法 uniapp支持多种消息推送方式,包括uniPush、个推、极光推送等。以下为常见实现方案: uniPush(官方推荐) uniapp官方提供的推送服务,基于DClou…

任务 uniapp

任务 uniapp

uniapp 简介 Uniapp 是一款基于 Vue.js 的跨平台开发框架,支持一次开发,多端发布(如微信小程序、H5、App、快应用等)。其核心优势在于代码复用率高,开发者可以通过一套代码适配多个…

uniapp 表格导入

uniapp 表格导入

uniapp 表格导入的实现方法 使用 uni.chooseFile 选择文件 在 uniapp 中,可以通过 uni.chooseFile API 让用户选择本地文件。该 API 支持多平台,包括…

uniapp 字体设置

uniapp 字体设置

uniapp 字体设置方法 uniapp 中设置字体可以通过以下几种方式实现,适用于不同场景和需求。 全局字体设置 在 App.vue 文件中通过样式定义全局字体,确保所有页面继承该字体设置。 &…

uniapp如何优化

uniapp如何优化

优化性能 减少页面层级,避免过多嵌套组件。使用v-if和v-show合理控制组件显示,减少不必要的DOM节点渲染。对于长列表,使用<scroll-view>或<list>组件并…