首页 > 其他分享 >拖拽宫格vue-grid-layout详细应用及案例

拖拽宫格vue-grid-layout详细应用及案例

时间:2023-08-07 11:35:13浏览次数:61  
标签:vue layout 元素 value Boolean grid const 宫格

目录

1、前言

vue-grid-layout是一个适用于vue的拖拽栅格布局库,功能齐全,适用于拖拽+高度/宽度自由调节的布局需求。本文将讲述一些常用参数和事件,以及做一个同步拖拽的Demo。效果动态图如下:

vue-grid-layout

2、安装

  • vue2版本:
npm install vue-grid-layout --save
  • vue3版本:
npm install [email protected] --save

3、属性

  • GridLayout 容器:
属性名 类型 必填 默认值 描述
layout Array - 数据源,每一项必须有i, x, y, w 和 h属性
colNum Int 12 列数
rowHeight Int 150 每行的高度像素
maxRows Int Infinity 最大行数
margin Array [10, 10] 元素边距
isDraggable Boolean true 是否可拖拽
isResizable Boolean true 是否可调整大小
isMirrored Boolean false 是否可镜像反转
autoSize Boolean true 是否自动调整大小
verticalCompact Boolean true 布局是否垂直压缩
preventCollision Boolean false 防止碰撞,为true则元素只能拖动至空白处
useCssTransforms Boolean true 是否使用CSS属性 transition-property: transform
responsive Boolean false 布局是否为响应式
breakpoints Boolean 为响应式布局设置断点
cols Boolean 设置每个断点对应的列数
  • GridItem 子项:
属性名 类型 必填 默认值 描述
i string - 子项ID
x number - 元素位于第几列
y number - 元素位于第几行
w number - 初始宽度,值必须为colNum的倍数
h number - 初始高度,值必须为rowHeight的倍数
minW number 1 元素最小宽度,值必须为colNum的倍数,如果w小于minW,则minW的值会被w覆盖
minH number 1 元素最小高度,值必须为rowHeight的倍数,如果h小于minH,则minH的值会被h覆盖
maxW number Infinity 元素最大宽度,值必须为colNum的倍数,如果w大于maxW,则maxW的值会被w覆盖
maxH number Infinity 元素最大高度,值必须为rowHeight的倍数,如果h大于maxH,则maxH的值会被h覆盖
isDraggable Boolean null 是否可拖拽。如果值为null则取决于父容器
isResizable Boolean null 是否可调整大小。如果值为null则取决于父容器
static Boolean false 是否为静态的,无法拖拽、调整大小或被其他元素移动
dragIgnoreFrom string 'a, button' 标识哪些子元素无法触发拖拽事件,值为css-like选择器
dragAllowFrom string null 标识哪些子元素可以触发拖拽事件,值为css-like选择器,如果值为null则表示所有子元素
resizeIgnoreFrom string 'a, button' 标识哪些子元素无法触发调整大小的事件,值为css-like选择器

4、事件

  • GridLayout 容器:
事件名 描述
layoutCreatedEvent 对应Vue生命周期的created
layoutBeforeMountEvent 对应Vue生命周期的beforeMount
layoutMountedEvent 对应Vue生命周期的mounted
layoutReadyEvent 当完成mount中的所有操作时生成的事件
layoutUpdatedEvent 布局更新或栅格元素的位置重新计算事件
breakpointChangedEvent 断点更改事件,每次断点值由于窗口调整大小而改变
  • GridItem 子项:
事件名 描述
moveEvent 移动时的事件
resizeEvent 调整大小时的事件
movedEvent 移动后的事件
resizedEvent 调整大小后的事件
containerResizedEvent 栅格元素/栅格容器更改大小的事件(浏览器窗口或其他)

5、占位符样式修改

直接覆盖默认的class样式

.vue-grid-item.vue-grid-placeholder {
    background: red;
    opacity: 0.2;
    transition-duration: 100ms;
    z-index: 2;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    -o-user-select: none;
    user-select: none;
}

.vue-grid-item.vue-grid-placeholder {
    background: green !important;
}

6、案例

注:本案例是按照vue3的写法

  • HTML:
<div class="grid_box">
	<div class="left">
		<grid-layout
			v-model:layout="layoutLeft"
			:col-num="4"
			:row-height="50"
			:is-draggable="true"
			:is-resizable="true"
			:is-mirrored="false"
			:vertical-compact="true"
			:margin="[10, 10]"
			:use-css-transforms="true"
			ref="gridLeftRef"
		>
			<grid-item
				v-for="item in layoutLeft"
				:x="item.x"
				:y="item.y"
				:w="item.w"
				:h="item.h"
				:i="item.i"
				:key="item.i"
				@resized="handleGridSync"
				@moved="handleGridSync"
			>
				<div class="left_layout_item">
					<div class="del_btn" @click="deleteGrid(item.i)">删</div>
					<span>{{ item.i }}</span>
				</div>
			</grid-item>
		</grid-layout>
	</div>
	<div class="right">
		<grid-layout
			v-model:layout="layoutRight"
			:col-num="4"
			:row-height="10"
			:is-draggable="true"
			:is-resizable="true"
			:is-mirrored="false"
			:vertical-compact="true"
			:margin="[10, 10]"
			:use-css-transforms="true"
			ref="gridRightRef"
		>
			<grid-item v-for="item in layoutRight" :x="item.x" :y="item.y" :w="item.w" :h="item.h" :i="item.i" :key="item.i">
				<div class="right_layout_item">{{ item.i }}</div>
			</grid-item>
		</grid-layout>
	</div>
</div>
  • 引入组件
import VueGridLayout from 'vue-grid-layout'
  • 数据源:
const gridLeftRef = ref<any>()
const gridRightRef = ref<any>()

const layoutLeft = ref([
	{ i: '1', x: 0, y: 0, w: 2, h: 2 },
	{ i: '2', x: 2, y: 0, w: 2, h: 2 },
	{ i: '3', x: 0, y: 0, w: 2, h: 2 },
	{ i: '4', x: 2, y: 0, w: 2, h: 2 }
])

const layoutRight = ref([
	{ i: '1', x: 0, y: 0, w: 2, h: 2 },
	{ i: '2', x: 2, y: 0, w: 2, h: 2 },
	{ i: '3', x: 0, y: 0, w: 2, h: 2 },
	{ i: '4', x: 2, y: 0, w: 2, h: 2 }
])
  • 处理方法:
// 处理Grid同步
const handleGridSync = () => {
	layoutLeft.value.forEach((item1) => {
		layoutRight.value.forEach((item2) => {
			if (item1.i === item2.i) {
				item2.x = item1.x
				item2.y = item1.y
				item2.w = item1.w
				item2.h = 2
			}
		})
	})
	gridLeftRef.value.layoutUpdate()
	gridLeftRef.value.updateHeight()
	gridRightRef.value.layoutUpdate()
	gridRightRef.value.updateHeight()
}

// 创造Grid
const createGrid = () => {
	let maxH = 0
	layoutLeft.value.forEach((item) => {
		if (item.y > maxH) maxH = item.y
	})
	const uid = createUuid()
	layoutLeft.value.push({ i: uid, x: 0, y: maxH, w: 2, h: 2 })
	layoutRight.value.push({ i: uid, x: 0, y: maxH, w: 2, h: 2 })
	handleGridSync()
}

// 删除Grid
const deleteGrid = (id: string) => {
	const idx1 = layoutLeft.value.findIndex((item1) => item1.i === id)
	layoutLeft.value.splice(idx1, 1)
	const idx2 = layoutRight.value.findIndex((item2) => item2.i === id)
	layoutRight.value.splice(idx2, 1)
	handleGridSync()
}

本次分享就到这儿啦,我是鹏多多,如果您看了觉得有帮助,欢迎评论,关注,点赞,转发,我们下次见~


PS:在本页按F12,在console中输入document.querySelectorAll('.diggit')[0].click(),有惊喜哦


面向百度编程

公众号

weixinQRcode.png

往期文章

个人主页

标签:vue,layout,元素,value,Boolean,grid,const,宫格
From: https://www.cnblogs.com/-pdd/p/17610975.html

相关文章

  • 前端 Vue 应该知道的一些东西,个人笔记 2021-11-26
    前端代码编写规范及es6常用语法命名规范文件夹名称,文件名称,组件名称,统一使用大驼峰或者小横线方式命名;组件文件名:list-item.vue.或者ListItem.vue;基础的无状态的通用组件加VBaseApp前缀BaseButtonAppButton在html中<base-button>或者<BaseButton>url路径名:小......
  • 【Vue笔记链接总结】
    【Vue笔记链接总结】【一】前端发展史【1.0】前端的发展史-Chimengmeng-博客园(cnblogs.com)【二】Vue之介绍及引入【2.0】Vue之引入-Chimengmeng-博客园(cnblogs.com)【三】Vue之基础语法【3.0】Vue之语法-Chimengmeng-博客园(cnblogs.com)【四】Vue......
  • vue-router addRoute将子路由添加到指定路由下
    router.addRoute()可以向vue路由中动态的添加路由信息,但,路由存在多层级关系,例如,最开始的路由是这样的:{path:'/',name:'layout',component:resolve=>require(['../layout'],resolve),meta:{title:'',handleStatus:......
  • 【8.0】Vue之ref属性
    【ref属性】ref属性,vue提供的,写在标签上可以写在普通标签:在vue中使用this.$refs.名字拿到dom对象,可以原生操作可以写在组件上:在vue中使用this.$refs.名字拿到[组件]对象,组件属性,方法直接使用即可【详解】ref属性是Vue.js中提供的一种特殊属性,它可以用于在标签上......
  • 【9.0】Vue之项目规范
    【一】vue-cli创建项目【1】引入单页面应用(SPA)单页面应用(SinglePageApplication,简称SPA)是一种Web应用程序的架构方式。传统的多页面应用中,每次导航到新页面都会进行整个页面的重新加载。而SPA只有一个主页面(通常是index.html),页面的内容通过动态渲染来更新,不会重新加载整......
  • 【七】Vue之Vue-cli
    【一】Vue-CLI项目搭建【二】Vue-CLI项目搭建参考步骤Vue-CLI(VueCommandLineInterface)是Vue.js官方提供的一个基于命令行的快速搭建Vue项目的工具。它为我们创建一个Vue项目提供了简单易用的脚手架。【1】安装Node.js:首先需要安装Node.js,因为Vue-CLI是基于Node.js运......
  • 【十】Vue之高级
    【一】ref属性【1】详解被用来给元素或子组件注册引用信息(id的替代者)应用在html标签上获取的是真实DOM元素,应用在组件标签上是组件实例对象(vc)在Vue中,$refs是一个特殊的属性,用于给元素或子组件注册引用信息。它允许我们在模板或组件中通过引用名称来访问对应的DOM元素或......
  • 【十一】Vue之Vue3
    【一】Vue3的变化【1】性能的提升打包大小减少41%初次渲染快55%,更新渲染快133%内存减少54%【2】源码的升级使用Proxy代替defineProperty实现响应式重写虚拟DOM的实现和Tree-Shaking【3】拥抱TypeScriptVue3可以更好的支持TypeScript【4】新的特性Composition......
  • 【3.0】Vue之语法
    【一】插值语法<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title><!--Vue文件--><scriptsrc="https://cdn.bootcdn.net/ajax/libs/vue/2.7.9/vu......
  • 【2.0】Vue之引入
    【一】Vue介绍Vue(读音/vjuː/,类似于view)是一个渐进式JavaScript框架,用于构建用户界面。它与其他大型框架的不同之处在于,Vue的设计理念是可以逐层应用的。Vue的核心库只关注视图层,这使得它不仅易于上手,还方便与第三方库或已有项目进行整合。Vue采用了M-V-VM(Model......