Vue3下使用vue-grid-layout从外部拖入demo10

2024-02-29 20:28

本文主要是介绍Vue3下使用vue-grid-layout从外部拖入demo10,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

由于Vue3文档缺失,示例10也是也跑不起来,这边参考示例改动了一下。

改动的点主要是this指向、$children相关的问题
在Vue3中移除了$children可以使用$ref替代

参考文章:计算坐标方法重写、文档示例

关键点:计算中的子组件取值使用ref绑定即可

对于循环的Dom树可以参考模板引用

完整代码:

	<template><div class="user-canvas" @dragenter.prevent @dragover.prevent><div id="content" class="w-full pb-16"><grid-layoutclass="min-h-screen"ref="gridlayout":layout.sync="gridConfig.layout":col-num="60":row-height="30":is-draggable="draggable && gridConfig.configType !== 2":is-resizable="resizable && gridConfig.configType !== 2":vertical-compact="false":use-css-transforms="true"><!--      <grid-item class="h-full relative z-50" :x="0" :y="2" :w="12" :h="2" i="s1">--><!--      </grid-item>--><grid-itemref="gridItemRefs"v-for="item in gridConfig.layout"class="h-full select-none relative overflow-hidden":static="item.static":x="item.x":y="item.y":w="item.w":h="item.h":i="item.i":minW="8":minH="2"><div>{{item.key}}</div></grid-item></grid-layout></div></div></template>
	import { ref, onUnmounted } from 'vue'// refconst gridlayout = ref(null)const gridItemRefs = ref([])// 计算坐标用let mouseXY = { x: null, y: null }let DragPos = { x: null, y: null, w: 4, h: 4, i: null }const gridConfig ={layout:[{x: 0,y: 3,w: 4,h: 12,i: '1',static: true,moved: false}]}const handleDrop = (event) => {event.preventDefault()// TODO}// 开始拖动const drag = (e) => {let parentRect = document.getElementById('content').getBoundingClientRect()let mouseInGrid = falseif (mouseXY.x > parentRect.left &&mouseXY.x < parentRect.right &&mouseXY.y > parentRect.top &&mouseXY.y < parentRect.bottom) {mouseInGrid = true}if (mouseInGrid === true && gridConfig.layout.findIndex((item) => item.i === 'drop') === -1) {gridConfig.layout.push({x: (gridConfig.layout.length * 2) % 12,y: gridConfig.layout.length + 12, // puts it at the bottomw: gridConfig.dragType?.w || 4,h: gridConfig.dragType?.h || 4,i: 'drop'})}let index = gridConfig.layout.findIndex((item) => item.i === 'drop')if (index !== -1) {try {gridItemRefs.value[gridConfig.layout.length - 1].$refs.item.style.display = 'none'} catch {}let el = gridItemRefs.value[index]// el.dragging = { top: mouseXY.y - parentRect.top, left: mouseXY.x - parentRect.left }let new_pos = calcXY(el, mouseXY.y - parentRect.top, mouseXY.x - parentRect.left, 1, 1)if (mouseInGrid === true) {// 红色游标部分gridlayout.value.dragEvent('dragstart','drop',new_pos.x,new_pos.y,gridConfig.dragType?.h || 4,gridConfig.dragType?.w || 4)DragPos.i = String(index)DragPos.x = gridConfig.layout[index].xDragPos.y = gridConfig.layout[index].y}if (mouseInGrid === false) {gridlayout.value.dragEvent('dragend', 'drop', new_pos.x, new_pos.y, 1, 1)gridConfig.layout = gridConfig.layout.filter((obj) => obj.i !== 'drop')}}}// 释放拖动const dragend = (e) => {let parentRect = document.getElementById('content').getBoundingClientRect()let mouseInGrid = falseif (mouseXY.x > parentRect.left &&mouseXY.x < parentRect.right &&mouseXY.y > parentRect.top &&mouseXY.y < parentRect.bottom) {mouseInGrid = true}if (mouseInGrid === true) {gridlayout.value.dragEvent('dragend', 'drop', DragPos.x, DragPos.y, 1, 1)gridConfig.addGridItem(DragPos)gridConfig.layout = gridConfig.layout.filter((obj) => obj.i !== 'drop')}gridItemRefs.value[gridConfig.layout.length - 1].$refs.item.style.display = 'none'}// 计算坐标const calcXY = (el, top, left, width, height) => {const colWidth = el.calcColWidth()let x = Math.round((left - 10) / (colWidth + 10))let y = Math.round((top - 10) / (el.rowHeight + 10))const _width = width || el.innerWconst _height = height || el.innerHx = Math.max(Math.min(x, el.cols - _width), 0)y = Math.max(Math.min(y, el.maxRows - _height), 0)return { x, y }}// 加载完成后监听鼠标事件onMounted(() => {document.addEventListener('dragover',function (e) {mouseXY.x = e.clientXmouseXY.y = e.clientY},false)})// 销毁时关闭监听onUnmounted(() => {document.removeEventListener('dragover',function (e) {mouseXY.x = nullmouseXY.y = null},false)})

这篇关于Vue3下使用vue-grid-layout从外部拖入demo10的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/759966

相关文章

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

CSS Anchor Positioning重新定义锚点定位的时代来临(最新推荐)

《CSSAnchorPositioning重新定义锚点定位的时代来临(最新推荐)》CSSAnchorPositioning是一项仍在草案中的新特性,由Chrome125开始提供原生支持需... 目录 css Anchor Positioning:重新定义「锚定定位」的时代来了! 什么是 Anchor Pos

CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比

《CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比》CSS中的position属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布... css 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4