当前位置:首页 > VUE

vue实现图标拖拽

2026-01-08 07:33:45VUE

Vue 实现图标拖拽

使用 HTML5 拖放 API

在 Vue 中实现图标拖拽可以利用 HTML5 的拖放 API。通过 draggable 属性标记可拖拽元素,并监听 dragstartdragoverdrop 事件。

<template>
  <div>
    <div 
      v-for="(icon, index) in icons" 
      :key="index" 
      class="icon" 
      draggable="true" 
      @dragstart="dragStart(index)"
    >
      {{ icon }}
    </div>
    <div 
      class="drop-area" 
      @dragover.prevent 
      @drop="drop"
    >
      拖拽到这里
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      icons: ['图标1', '图标2', '图标3'],
      draggedIndex: null
    };
  },
  methods: {
    dragStart(index) {
      this.draggedIndex = index;
    },
    drop() {
      if (this.draggedIndex !== null) {
        const draggedIcon = this.icons[this.draggedIndex];
        this.icons.splice(this.draggedIndex, 1);
        this.icons.push(draggedIcon);
        this.draggedIndex = null;
      }
    }
  }
};
</script>

<style>
.icon {
  width: 50px;
  height: 50px;
  background: #eee;
  margin: 5px;
  display: inline-block;
  cursor: move;
}
.drop-area {
  width: 200px;
  height: 200px;
  border: 2px dashed #ccc;
  margin-top: 20px;
}
</style>

使用第三方库(如 Vue.Draggable)

Vue.Draggable 是基于 Sortable.js 的 Vue 拖拽组件,适合更复杂的拖拽需求。

安装 Vue.Draggable:

npm install vuedraggable

示例代码:

<template>
  <div>
    <draggable v-model="icons" group="icons" @start="drag=true" @end="drag=false">
      <div v-for="(icon, index) in icons" :key="index" class="icon">
        {{ icon }}
      </div>
    </draggable>
  </div>
</template>

<script>
import draggable from 'vuedraggable';
export default {
  components: { draggable },
  data() {
    return {
      icons: ['图标1', '图标2', '图标3']
    };
  }
};
</script>

<style>
.icon {
  width: 50px;
  height: 50px;
  background: #eee;
  margin: 5px;
  display: inline-block;
}
</style>

自定义拖拽逻辑

如果需要更灵活的拖拽逻辑,可以结合鼠标事件(mousedownmousemovemouseup)实现。

<template>
  <div>
    <div 
      v-for="(icon, index) in icons" 
      :key="index" 
      class="icon" 
      @mousedown="startDrag(index, $event)"
      :style="{ left: positions[index].x + 'px', top: positions[index].y + 'px' }"
    >
      {{ icon }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      icons: ['图标1', '图标2', '图标3'],
      positions: [
        { x: 0, y: 0 },
        { x: 60, y: 0 },
        { x: 120, y: 0 }
      ],
      dragging: null,
      offset: { x: 0, y: 0 }
    };
  },
  methods: {
    startDrag(index, event) {
      this.dragging = index;
      this.offset.x = event.clientX - this.positions[index].x;
      this.offset.y = event.clientY - this.positions[index].y;
      document.addEventListener('mousemove', this.onDrag);
      document.addEventListener('mouseup', this.stopDrag);
    },
    onDrag(event) {
      if (this.dragging !== null) {
        this.positions[this.dragging].x = event.clientX - this.offset.x;
        this.positions[this.dragging].y = event.clientY - this.offset.y;
      }
    },
    stopDrag() {
      this.dragging = null;
      document.removeEventListener('mousemove', this.onDrag);
      document.removeEventListener('mouseup', this.stopDrag);
    }
  }
};
</script>

<style>
.icon {
  width: 50px;
  height: 50px;
  background: #eee;
  margin: 5px;
  position: absolute;
  cursor: move;
}
</style>

注意事项

  • 拖拽性能优化:对于大量图标,建议使用虚拟滚动或限制拖拽范围。
  • 移动端支持:HTML5 拖放 API 在移动端兼容性较差,可能需要使用触摸事件(touchstarttouchmovetouchend)替代。
  • 数据同步:拖拽后及时更新数据状态,避免视图与数据不同步。

vue实现图标拖拽

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

相关文章

vue实现拖拽计算器

vue实现拖拽计算器

Vue 拖拽计算器实现方案 核心功能分析 可拖拽组件:计算器整体或按钮需要支持拖拽 2.计算逻辑:实现基本四则运算功能 布局系统:支持动态调整计算器位置 基础实现步骤 安装依赖 需要vue-dragg…

vue 实现拖拽

vue 实现拖拽

Vue 实现拖拽功能 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。 <template> &l…

vue拖拽实现方式

vue拖拽实现方式

Vue拖拽实现方式 使用HTML5原生拖拽API HTML5提供了原生的拖拽API,可以通过draggable属性、dragstart、dragend、dragover和drop等事件实现基础拖拽功能…

vue 实现卡片拖拽

vue 实现卡片拖拽

Vue 实现卡片拖拽 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 drop…

vue实现拖拽div

vue实现拖拽div

Vue 实现拖拽 Div 的方法 使用原生 HTML5 拖拽 API HTML5 提供了原生的拖拽 API,结合 Vue 的事件绑定可以快速实现拖拽功能。以下是实现步骤: <template&…

vue拖拽容器实现

vue拖拽容器实现

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