当前位置:首页 > VUE

vue实现拖动排序

2026-02-18 13:00:17VUE

Vue 实现拖动排序的方法

使用原生 HTML5 拖放 API

HTML5 提供了原生的拖放 API,可以通过 draggable 属性、dragstartdragenddragoverdrop 事件实现拖动排序。

<template>
  <div>
    <div
      v-for="(item, index) in items"
      :key="item.id"
      draggable="true"
      @dragstart="handleDragStart(index)"
      @dragover.prevent="handleDragOver(index)"
      @drop="handleDrop(index)"
    >
      {{ item.name }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' },
        { id: 3, name: 'Item 3' },
      ],
      draggedIndex: null,
    };
  },
  methods: {
    handleDragStart(index) {
      this.draggedIndex = index;
    },
    handleDragOver(index) {
      if (this.draggedIndex !== index) {
        const draggedItem = this.items[this.draggedIndex];
        this.items.splice(this.draggedIndex, 1);
        this.items.splice(index, 0, draggedItem);
        this.draggedIndex = index;
      }
    },
    handleDrop(index) {
      this.draggedIndex = null;
    },
  },
};
</script>

使用第三方库 Vue.Draggable

Vue.Draggable 是基于 Sortable.js 的 Vue 组件,提供了更简单的方式实现拖动排序。

安装 Vue.Draggable:

vue实现拖动排序

npm install vuedraggable

使用示例:

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

<script>
import draggable from 'vuedraggable';

export default {
  components: {
    draggable,
  },
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' },
        { id: 3, name: 'Item 3' },
      ],
    };
  },
  methods: {
    onDragEnd() {
      console.log('Items reordered:', this.items);
    },
  },
};
</script>

使用 Sortable.js 直接操作 DOM

Sortable.js 是一个功能强大的拖拽排序库,可以直接操作 DOM 元素。

vue实现拖动排序

安装 Sortable.js:

npm install sortablejs

使用示例:

<template>
  <div ref="sortableList">
    <div v-for="item in items" :key="item.id">
      {{ item.name }}
    </div>
  </div>
</template>

<script>
import Sortable from 'sortablejs';

export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' },
        { id: 3, name: 'Item 3' },
      ],
    };
  },
  mounted() {
    this.initSortable();
  },
  methods: {
    initSortable() {
      const el = this.$refs.sortableList;
      new Sortable(el, {
        animation: 150,
        onEnd: (evt) => {
          const movedItem = this.items.splice(evt.oldIndex, 1)[0];
          this.items.splice(evt.newIndex, 0, movedItem);
        },
      });
    },
  },
};
</script>

注意事项

  • 使用原生 HTML5 拖放 API 时,需要手动处理数据的更新和 DOM 的操作。
  • Vue.Draggable 提供了更简洁的 API,适合大多数 Vue 项目。
  • Sortable.js 提供了更多的配置选项和功能,适合复杂的拖拽需求。
  • 在移动端设备上,可能需要额外的处理来支持触摸事件。

标签: 拖动vue
分享给朋友:

相关文章

vue实现矩阵

vue实现矩阵

Vue 实现矩阵的方法 在 Vue 中实现矩阵操作可以通过多种方式完成,以下是一些常见的方法: 使用二维数组表示矩阵 矩阵可以用二维数组来表示,每个元素对应矩阵中的一个值。在 Vue 的 data…

vue实现div

vue实现div

Vue 实现 DIV 元素的方法 在 Vue 中实现 DIV 元素可以通过多种方式,以下是几种常见的实现方法: 使用模板语法 在 Vue 的模板中直接使用 HTML 的 <div> 标签…

vue实现 treegrid

vue实现 treegrid

Vue 实现 TreeGrid 的方法 使用现成组件库 Element UI、Ant Design Vue 等流行组件库提供了现成的 TreeTable/TreeGrid 组件,可直接使用。 Ele…

vue实现checkbox

vue实现checkbox

Vue 实现 Checkbox 在 Vue 中实现 Checkbox 可以通过原生 HTML 的 <input type="checkbox"> 或使用 Vue 的 v-model 指令绑…

vue 实现hover

vue 实现hover

Vue 实现 Hover 效果的方法 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 伪类 :hover 最简单的方式是直接使用 CSS 的 :hov…

vue sku 实现

vue sku 实现

Vue SKU 实现方案 在电商系统中,SKU(Stock Keeping Unit)是商品的最小库存单位,通常由多个属性组合而成(如颜色、尺寸等)。以下是基于 Vue 的实现方案。 数据结构设计…