Vue3+ts、实现拖动更换位置

根据项目需求,再进行响应到修改即可 

<template>
  <div class="root">
    <transition-group tag="ul" class="container">
      <li
        class="item"
        :class="'item' + index"
        v-for="(item, index) in items"
        :key="item.key"
        :style="{ 'background-color': item.color, border: '1px solid' }"
        draggable="true"
        @dragstart="handleDragStart($event, item)"
        @dragover.prevent="handleDragOver($event, item)"
        @dragenter="handleDragEnter($event, item)"
        @dragend="handleDragEnd($event, item)"
      >
        <div>{{ item }}</div>
      </li>
    </transition-group>
  </div>
</template>

<script setup lang="ts">
  import { ref } from 'vue';

  interface Item {
    key: number;
    color: string;
  }

  const items = ref([
    { key: 1, color: '#0883a0' },
    { key: 2, color: '#1883a0' },
    { key: 3, color: '#2883a0' },
    { key: 4, color: '#3883a0' },
    { key: 5, color: '#4883a0' },
    { key: 6, color: '#5883a0' },
    { key: 7, color: '#6883a0' },
    { key: 8, color: '#7883a0' },
    { key: 9, color: '#8883a0' },
    { key: 10, color: '#9883a0' },
  ]);

  let ending: Item | null = null;
  let dragging: Item | null = null;
  // 拖拽开始时触发的事件
  const handleDragStart = (e: DragEvent, item: Item) => {
    dragging = item;
    console.log(e);
  };
  // 拖拽结束时触发的事件
  const handleDragEnd = (e: DragEvent, item: Item) => {
    if (ending && ending.key === dragging?.key) {
      return;
    }
    console.log(e, item);

    const newItems = [...items.value];
    const src = newItems.indexOf(dragging!);
    const dst = newItems.indexOf(ending!);
    newItems.splice(src, 1, ...newItems.splice(dst, 1, newItems[src]));
    console.log(newItems);

    items.value = newItems;
    dragging = null; //被拖拽元素
    ending = null; //目标元素
  };
  // 拖拽过程中在目标元素上移动时触发的事件
  const handleDragOver = (e: DragEvent, item: Item) => {
    e.dataTransfer!.dropEffect = 'move';
    console.log(e, item);
  };
  // 拖拽进入目标元素时触发的事件
  const handleDragEnter = (e: DragEvent, item: Item) => {
    e.dataTransfer!.effectAllowed = 'move';
    ending = item;
  };
</script>

<style scoped>
  /* .container {
    display: flex;
    flex-wrap: wrap;
    list-style-type: none;
    padding: 0;
  } */

  .item {
    height: 60px;
    margin: 10px;
    color: #fff;
    transition: all linear 0.3s;
  }
</style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来为您提供一个示例代码。首先,需要安装 Vue.js 3 和 TypeScript 的依赖: ``` npm install vue@next vue-router@4.0.0-beta.8 vuex@4.0.0-beta.4 typescript@4.0.3 ``` 然后,我们可以创建一个拖拽生成组件功能的 Demo,具体代码如下所示: ```html <template> <div class="app"> <div class="sidebar"> <div class="draggable" v-for="(component, index) in components" :key="index" :data-id="index" draggable="true" @dragstart="dragStart"> {{ component }} </div> </div> <div class="canvas" @drop="drop" @dragover.prevent> <component v-for="(component, index) in canvas" :key="index" :is="component"></component> </div> </div> </template> <script lang="ts"> import { defineComponent } from 'vue'; export default defineComponent({ name: 'App', data() { return { components: ['Input', 'Button', 'Image'], canvas: [], }; }, methods: { dragStart(event: DragEvent) { const target = event.target as HTMLElement; event.dataTransfer?.setData('text/plain', target.dataset.id || ''); }, drop(event: DragEvent) { const id = event.dataTransfer?.getData('text/plain'); if (id) { const component = this.components[id]; this.canvas.push(component); } }, }, }); </script> <style> .app { display: flex; height: 100vh; } .sidebar { width: 200px; padding: 20px; background-color: #f5f5f5; } .draggable { margin-bottom: 10px; cursor: move; } .canvas { flex: 1; display: flex; align-items: center; justify-content: center; background-color: #e0e0e0; height: 100%; } </style> ``` 这个 Demo 中,我们在左侧创建了三个可拖拽的组件,分别是 Input、Button 和 Image,然后在右侧的画布中可以通过拖拽生成组件。当我们从左侧的组件拖动到右侧的画布中时,会触发 `drop` 方法,将组件添加到 `canvas` 数组中,然后在页面中渲染出来。 以上是一个简单的示例代码,您可以基于此扩展实现更复杂的拖拽生成组件功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值