【开发记录】vue3实现列表拖拽排序,如何阻止放下后,元素回落?

一、问题描述

写了一个列表的拖拽排序,但遇到一个问题,就是拖拽放下后,html元素总会有一个回落的默认效果,这个会影响排序效果。

二、解决

其实,最后解决的办法,也很简单,就是在dragover事件处理中,去除默认,也就是e.preventDefault();

js 复制代码
 const onDragOver = (e: any, index: number) => {
    e.preventDefault();
    // ....
 }

三、最后,记录一下完整的代码实现

vue3 复制代码
<template>
  <div>
    <div class="card-list">
      <div
        class="card-item"
        :class="{'drag-over': dragOverIndex === index, 'active': dragData.index == index}"
        v-for="(item, index) in dataList"
        :key="item.cocosData.id"
        :id="item.cocosData.id"
        draggable="true"
        @dragstart.stop="onDragStart($event, index)"
        @dragover.stop="onDragOver($event, index)"
        @dragend.stop="onDragend"
      >
        <span>{{ item }}</span>
      </div>
    </div>
  </div>
</template>

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

export default defineComponent({
    name: "DragSort",
    setup(props) {
        const dataList = ref<string[]>([
            "字母A",
            "字母B",
            "字母D",
            "p",
        ]);
        
        const dragData = ref<any>({});
        const onDragStart = (e: any, index: number) => {
          dragData.value = {
            index,
          }
        }
        const dragOverIndex = ref(-1);
        const onDragOver = (e: any, index: number) => {
          e.preventDefault();
          if (dragData.value.index != index) {
            dragOverIndex.value = index;
          } else {
            dragOverIndex.value = -1;
          }
        }
        const onDragend = (e: any) => {
          e.preventDefault();
          const dragIndex = dragData.value.index;
          console.log('onDragend', dragIndex, dragOverIndex.value);
          if (dragIndex != dragOverIndex.value && dragOverIndex.value != -1) {
            const dragItem = _.cloneDeep(cocosdata.value["children"][dragIndex]);
            console.log('dragItem', dragItem);
            if (dragOverIndex.value > dragIndex) {
              cocosdata.value["children"].splice(dragOverIndex.value + 1, 0, dragItem);
              cocosdata.value["children"].splice(dragIndex, 1);
            } else {
              cocosdata.value["children"].splice(dragIndex, 1);
              cocosdata.value["children"].splice(dragOverIndex.value, 0, dragItem);
            }
          }
          dragOverIndex.value = -1;
          dragData.value = {};
        }
        
        return {
          dataList,
          onDragStart,
          onDragOver,
          dragOverIndex,
          onDragend,
          dragData,
        };
    },
});

</script>

<style lang="less" scoped>
.card-list {
  margin-top: 10px;
  .card-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border: 1px solid transparent;
    position: relative;
    &.active {
      background-color: #f2f2f2;
    }
    &.drag-over {
      background-color: #ddd;
      border: 1px solid #aaa;
    }
    span {
      cursor: pointer;
    }
  }
}
</style>

四、最后

欢迎各位来用下我用了近2年,搞的一款网页编辑器,一定会让你感到惊讶的,哈哈!

相关推荐
南囝coding29 分钟前
做付费社群,强烈建议大家做这件事!
前端·后端
我是若尘29 分钟前
Axios 如何跨域携带 Cookie?
前端
子林super35 分钟前
主从数据全量迁移到分片集群测试
前端
Spider_Man38 分钟前
🚀 TypeScript从入门到React实战:前端工程师的类型安全之旅
前端·typescript
TheRedAce41 分钟前
Select对于onChange的prop进行警告
前端·javascript
Mishi41 分钟前
Cursor前端初体验-不懂MCP?那就做一个MCP
前端
归于尽43 分钟前
从按钮 "跳帧" 到 3D 翻书:CSS 动画进阶的 "三级跳"
前端·css
Cache技术分享1 小时前
131. Java 泛型 - 目标类型与泛型推断
前端·后端
浅墨momo1 小时前
介绍一下Shopify App 的 Theme App Extensions
前端·创业
1_2_3_1 小时前
利用高德地图怎么单独显示某个省份的轮廓
前端