我是靠谱客的博主 落后咖啡,这篇文章主要介绍vue实现页面div盒子拖拽排序功能,现在分享给大家,希望可以做个参考。

vue 实现页面div盒子拖拽排序功能前言:目前市面上有很多实现拖拽排序功能的插件和方法,本节不过多累述,只讲一种:css3的transition-group方法

效果图:

1. DOM中使用:

复制代码
1
2
3
4
5
6
7
8
9
10
11
<transition-group class="container" name="sort"> <div class="app-item" v-for="app in customApps" :key="app.id" :draggable="true" @dragstart="dragstart(app)" @dragenter="dragenter(app,$event)" @dragend="getDragend(customApps, 'customer', $event)"> <div> <img class="icon_a" v-if="app.logo" :src="app.logo" > <div class="ellipsis" >{{app.name}}</div> </div> </div> </transition-group>

2. data中定义数据

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { APi } from '@/api/enterpriseAPi' <script> export default { data() { return { oldData: [], newData: [], customApps: [], dragStartId: '', dragEndId: '' } } } </script>

3. methods方法中使用

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
dragstart(value) { this.oldData = value this.dragStartId = value.id }, dragenter(value) { this.newData = value this.dragEndId = value.id }, getDragend(listData, type) { if (this.oldData !== this.newData) { let oldIndex = listData.indexOf(this.oldData) let newIndex = listData.indexOf(this.newData) let newItems = [...listData] // 删除之前DOM节点 newItems.splice(oldIndex, 1) // 在拖拽结束目标位置增加新的DOM节点 newItems.splice(newIndex, 0, this.oldData) // 每次拖拽结束后,将拖拽处理完成的数据,赋值原数组,使DOM视图更新,页面显示拖拽动画 this.customApps = newItems // 每次拖拽结束后调用接口时时保存数据 Api(this.dragStartId, this.dragEndId).then((res) => {}) } },

拖拽完成动画样式:

复制代码
1
2
3
4
5
<style lang="scss" scoped> .sort-move { transition: transform 1s; } </style>

到此这篇关于vue实现页面div盒子拖拽排序功能的文章就介绍到这了,更多相关vue div盒子拖拽排序内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是落后咖啡最近收集整理的关于vue实现页面div盒子拖拽排序功能的全部内容,更多相关vue实现页面div盒子拖拽排序功能内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(140)

评论列表共有 0 条评论

立即
投稿
返回
顶部