最近有个项目需要实现和windows桌面类似的图标拖拽功能,找了很多组件都没找到一个合适的,但通过本博主的不懈努力最后发现了两款不错的插件:

  1. sortablejs
  2. vue-grid-layout
    本来一开始我使用的是第一个,但是可能是有点转不过来,最终放弃了第一个插件,但是第二个也确实香,虽然也有不少bug但是也不是不能解决的,下面就回到正题上来给大家粗略说一下:

先上代码吧,怕大家等不及往下拉:

<template><div><grid-layout :layout.sync="layout":col-num="8":row-height="100":is-draggable="true":is-resizable="false":vertical-compact="false":margin="[20, 24]":use-css-transforms="true"><grid-item v-for="(item,index) in layout":key="index":item="watchitem(item)":static="item.static":x="item.x":y="item.y":w="item.w":h="item.h":i="item.i"@move="moveEvent"@moved="movedEvent" ><span class="text">{{item.i}}</span></grid-item></grid-layout></div>
</template><script>
var historyLayout = [{"x":0,"y":0,"w":1,"h":1,"i":"0", static: false},{"x":1,"y":0,"w":1,"h":1,"i":"1", static: false},{"x":2,"y":0,"w":1,"h":1,"i":"2", static: false},{"x":3,"y":0,"w":1,"h":1,"i":"3", static: false},{"x":4,"y":0,"w":1,"h":1,"i":"4", static: false},{"x":5,"y":0,"w":1,"h":1,"i":"5", static: false},{"x":6,"y":0,"w":1,"h":1,"i":"6", static: false},{"x":7,"y":0,"w":1,"h":1,"i":"7", static: false},{"x":0,"y":1,"w":1,"h":1,"i":"8", static: false},{"x":1,"y":1,"w":1,"h":1,"i":"9", static: false},{"x":2,"y":1,"w":1,"h":1,"i":"10", static: false},{"x":3,"y":1,"w":1,"h":1,"i":"11", static: false},{"x":4,"y":1,"w":1,"h":1,"i":"12", static: false},{"x":5,"y":1,"w":1,"h":1,"i":"13", static: false},{"x":6,"y":1,"w":1,"h":1,"i":"14", static: false},{"x":7,"y":1,"w":1,"h":1,"i":"15", static: false},{"x":0,"y":2,"w":1,"h":1,"i":"16", static: false},{"x":1,"y":2,"w":1,"h":1,"i":"17", static: false},{"x":2,"y":2,"w":1,"h":1,"i":"18", static: false},{"x":3,"y":2,"w":1,"h":1,"i":"19", static: false}
];import { GridLayout, GridItem } from "vue-grid-layout"
export default {components: {GridLayout,GridItem},data() {return {layout: [{"x":0,"y":0,"w":1,"h":1,"i":"0", static: false},{"x":1,"y":0,"w":1,"h":1,"i":"1", static: false},{"x":2,"y":0,"w":1,"h":1,"i":"2", static: false},{"x":3,"y":0,"w":1,"h":1,"i":"3", static: false},{"x":4,"y":0,"w":1,"h":1,"i":"4", static: false},{"x":5,"y":0,"w":1,"h":1,"i":"5", static: false},{"x":6,"y":0,"w":1,"h":1,"i":"6", static: false},{"x":7,"y":0,"w":1,"h":1,"i":"7", static: false},{"x":0,"y":1,"w":1,"h":1,"i":"8", static: false},{"x":1,"y":1,"w":1,"h":1,"i":"9", static: false},{"x":2,"y":1,"w":1,"h":1,"i":"10", static: false},{"x":3,"y":1,"w":1,"h":1,"i":"11", static: false},{"x":4,"y":1,"w":1,"h":1,"i":"12", static: false},{"x":5,"y":1,"w":1,"h":1,"i":"13", static: false},{"x":6,"y":1,"w":1,"h":1,"i":"14", static: false},{"x":7,"y":1,"w":1,"h":1,"i":"15", static: false},{"x":0,"y":2,"w":1,"h":1,"i":"16", static: false},{"x":1,"y":2,"w":1,"h":1,"i":"17", static: false},{"x":2,"y":2,"w":1,"h":1,"i":"18", static: false},{"x":3,"y":2,"w":1,"h":1,"i":"19", static: false}],draggable: true,resizable: true,index: 0,newX:0,newY:0,curBox:'',}},methods: {watchitem (item) {if(this.curBox != item.i) {for (let j = 0; historyLayout[j] != undefined; j++) {if(historyLayout[j].i == item.i) {item.x = historyLayout[j].xitem.y = historyLayout[j].y}}}return item},moveEvent (i){this.curBox = i},movedEvent (i, newX, newY) {for (let j = 0; historyLayout[j] != undefined; j++) {if(historyLayout[j].i == i) {this.newX = historyLayout[j].xthis.newY = historyLayout[j].yhistoryLayout[j].x = newXhistoryLayout[j].y = newY}}for (let j = 0; historyLayout[j] != undefined; j++) {if(i!=historyLayout[j].i && historyLayout[j].x === newX && historyLayout[j].y === newY) {historyLayout[j].x = this.newXhistoryLayout[j].y = this.newY}}}}
}
</script><style scoped>
.vue-grid-layout {background: #eee;
}
.vue-grid-item:not(.vue-grid-placeholder) {background: #ccc;border: 1px solid black;
}
.vue-grid-item .resizing {opacity: 0.9;
}
.vue-grid-item .static {background: #cce;
}
.vue-grid-item .text {font-size: 24px;text-align: center;position: absolute;top: 0;bottom: 0;left: 0;right: 0;margin: auto;height: 100%;width: 100%;
}
.vue-grid-item .no-drag {height: 100%;width: 100%;
}
.vue-grid-item .minMax {font-size: 12px;
}
.vue-grid-item .add {cursor: pointer;
}
</style>

效果(本人是最看不惯别人的文章不带效果的,狗头保护…)


下面进入正文:

  1. 安装:
npm install vue-grid-layout --save
  1. 导入 && 引用
 import VueGridLayout from 'vue-grid-layout';export default {components: {GridLayout: VueGridLayout.GridLayout,GridItem: VueGridLayout.GridItem},// ... data, methods, mounted (), etc.}
  1. 属性 && 方法
    这里就不做过多的讲解,毕竟文档比我写要好
    属性

  2. 这里才最主要是新增代码
    vue-grid-layout中的标识栅格元素是否可拖拽该属性只能将元素向下排,并不能形成左右替换的形式,并且在该组件中也没有元素互相替换位置这种属性,所以我给它加了判定

...
methods: {watchitem (item) {// 判断当前移动的对象是否导致其他元素发生位移变换if(this.curBox != item.i) {for (let j = 0; historyLayout[j] != undefined; j++) {if(historyLayout[j].i == item.i) {item.x = historyLayout[j].xitem.y = historyLayout[j].y}}}return item},moveEvent (i){// 移动时记录当前移动的元素对象this.curBox = i},movedEvent (i, newX, newY) {// 将元素最新生成的位置记录在historyLayout中,用来形成判断for (let j = 0; historyLayout[j] != undefined; j++) {if(historyLayout[j].i == i) {this.newX = historyLayout[j].xthis.newY = historyLayout[j].yhistoryLayout[j].x = newXhistoryLayout[j].y = newY}}// 当元素移动到原本有元素的位置上时,原本位置上的元素迁移到移动前元素的位置上形成互换位置for (let j = 0; historyLayout[j] != undefined; j++) {if(i!=historyLayout[j].i && historyLayout[j].x === newX && historyLayout[j].y === newY) {historyLayout[j].x = this.newXhistoryLayout[j].y = this.newY}}}}

以上就是我目前功能的实现,但由于项目时间关系,还没有完善代码,没有写一个将元素插入其中的方法,希望各位有了更好的意见时可以一起分享分享

使用vue-grid-layout完成桌面拖拽布局功能(实现两个元素互换位置)相关推荐

  1. sortable vue 排序_vue 使用 sortable 实现 el-table 拖拽排序功能

    本文给大家介绍vue 使用 sortable 实现 el-table 拖拽排序功能,具体内容如下所示: npm 下载: npm install sortablejs --save 引入: import ...

  2. Vue Grid Layout -️ 适用Vue.js的栅格布局系统(保姆级使用教程)

    目录 一. Vue Grid Layout 简介 二.vue-grid-layout 的安装与使用 三. 属性 3.1 gridItem 的必须属性 3.2 框架元素的实际宽度高度计算方式 3.3 元 ...

  3. Vue自定义指令实现弹窗拖拽,四边拉伸及对角线拉伸

    Vue自定义指令实现弹窗拖拽,四边拉伸及对角线拉伸 引言 页面布局 drag.js文件 弹窗拖拽实现及边界限制 鼠标指针悬停样式 四边拉伸及对角线拉伸 拉伸干涉 引言 近期公司vue前端项目需求:实现 ...

  4. Vue 实现弹框自由拖拽(不出可视范围、解决快速拖动问题)

    Vue 实现弹框自由拖拽(不出可视范围.解决快速拖动问题) 由于页面中弹框很多,往往会挡住想要查看的内容.从而,有了自由移动拖拽弹框的需求:但在使用的过程中发现,一开始编写的drag.js文件会移出可 ...

  5. 如何实现从桌面拖拽到网页

    实现从桌面拖拽到网页 过去我们想实现网页中的拖拽效果,基本上都是使用DOM事件模型中的mousedown.mousemove.mouseup的事件监听来模拟拖拽效果,为了实现实时的拖拽移动效果,还要不 ...

  6. vue实现简单的鼠标拖拽横向滚动和 鼠标滚轮横向滚动

    原文地址:vue实现简单的鼠标拖拽横向滚动和 鼠标滚轮横向滚动_超人不会飞啊的博客-CSDN博客_vue鼠标拖拽 这边如果是动态渲染的div,需要等dom渲染结束后.id存在了.再去设置this.sc ...

  7. vue+flask实现视频合成(拖拽上传)

    vue+flask实现视频合成 效果如下 欢迎访问博客代码哈士奇 技术 聊天 交流群 974178910 前端交流群 535620886 vue+flask实现视频合成 拖拽上传我们之前一个文章有写过 ...

  8. 【vue弹框和div拖拽功能】vue-elementul弹框拖拽功能,移动拖拽功能,普通div拖拽功能【拿来即用,直接复制】

    前言 这里是通过vue的指令封装好的 直接创建一个js文件复制进去,引入一下就搞定了,非常简单 html版本引入vue语法的div和弹框拖拽功能 这里是考虑到有时候会在html页面开发的时候也会用到这 ...

  9. 通过layout实现可拖拽自动排序的UICollectionView

    Translate from http://blog.karmadust.com/drag-and-rearrange-uicollectionviews-through-layouts/ (Gith ...

最新文章

  1. mysql io模型_5种网络IO模型
  2. 阿里mysql同步工具otter的docker镜像
  3. 知识图谱(八)——知识存储和检索
  4. 企业网站前端制作实战教程 JQuery CSS JS HTML 项目需求分析与准备工作
  5. 自定义安装mysql linux_linux下 安装mysql 问题
  6. 红色风格电脑手机数码商城系统网站源码
  7. [转]设计模式六大原则[1]:单一职责原则
  8. bootstraptable列宽自适应内容_多列列表框行高和列宽的自适应调整
  9. arm 升腾310_简单说说华为海思的芯片产品线
  10. 在Debian 6 安装pptpd ×××。
  11. 从Bezier到NURBS曲线(1) - Bezier曲线
  12. 用LINUX就要安装文泉驿字体
  13. 微信小程序SEO排名优化
  14. object has no attribute 'cleaned_data'
  15. 邮箱地址注册申请能免费注册吗?
  16. EXCEL的COUNTIF和COUNTIFS函数的区别和联系
  17. 高等数学(总结9-多重积分)
  18. 4G模块中RSRP RSRQ RSSI SINR等信号值的含义和区别
  19. 高通平台开发系列讲解(外设篇)DSI分屏
  20. Qt之QLCDNumber

热门文章

  1. 服务端为什么需要心跳(保活)机制
  2. 【排序】【牛客模拟赛】牛半仙的妹子串
  3. JDK9新API:List.of();Map.of();Set.of();
  4. 女孩纹身全过程(组图)
  5. anycast隧道_讲述IPv6网络中的选播概述及Anycast困难解决方法
  6. 计算器Java代码实现(进行按钮事件处理和键盘事件处理)
  7. c++截取汉字和英文混合字符串
  8. 在Mac装Emmet插件
  9. Java—小鑫の日常系列故事(六)——奇遇记
  10. 抖音小店无货源不吃香了?这个项目的红利期结束了?