vue实现平面图标记点位可拖拽可删除(可自定义标记图标)

2023-10-06 22:40

本文主要是介绍vue实现平面图标记点位可拖拽可删除(可自定义标记图标),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

需求:

1.不限制背景图的大小做overflower,标记点位获取相对位置xy,可拖动修改标点位置,可删除标点
2.关联设备后做自定义标记图标

图例:

在这里插入图片描述

代码:

1.html

<el-dialog title="设置设备" :visible.sync="imgMark" append-to-body id="plan"><div class="plan-img"><img class="bg" :src="bgSrc" @click.stop="setPoint" /><div v-for="(item, index) in markerArr" :key="index" class="block" :style="{ left: item.x, top: item.y, transform: 'translate(-50%, -100%)' }"@drag="dragenter($event, item)"draggable="true"><img :src="markSrc" class="mark-icon"  @click.stop="editPoit(index,item)" /><div class="button-group" v-if="item.showModal"><span @click="associative(index)">关联设备</span><span @click="deletePoit(index)">删除点位</span></div></div></div><div slot="footer" class="dialog-footer"><el-button type="primary" @click="submitMark" class="button-minwidth">确定</el-button><el-button @click="imgMark = false" class="button-minwidth">取消</el-button></div>
</el-dialog>

2.js

<script>
import bgSrc from '@/assets/images/bg.png'
import markSrc from '@/assets/images/img-mark.png'export default {data() {return {//平面图标记点位imgMark:false,bgSrc,markSrc,markerArr:[],};},created() {window.addEventListener("resize", this.getImgRect)},beforeDestroy() {window.removeEventListener("resize", this.getImgRect)},methods: {//设置设备handleSetDev(row){this.imgMark = truethis.getImgRect()},//绘制标记setPoint(e){e = e || window.event;let x = e.offsetX || e.layerX;let y = e.offsetY || e.layerY;let obj = {x: x + 'px',y: y + 'px',showModal:false}this.markerArr.push(obj);},//获取相对位置getImgRect() {setTimeout(() => {// 元素点是相对 背景图定位的    拖拽点获得的做标是相对屏幕的距离 this.imgRect = document.querySelector(".plan-img .bg").getBoundingClientRect()}, 30);},//移动位置dragenter(e, item) {const {x, y} = this.imgRectif(e.clientX !==0 && e.clientY !==0) {item.x = `${e.clientX - x}px`item.y = `${e.clientY - y}px`}   },//编辑标记editPoit(index,item){if(item.showModal==false){item.showModal=true}else {item.showModal=false}},//关联设备associative(index){},//提交关联设备submitDev(){this.spaceDev=false},//删除标记deletePoit(index){this.markerArr.splice(index, 1);},//提交标记submitMark(){this.imgMark=false},}
};
</script>

3.css

#plan{::v-deep .el-dialog{height: 65%;}::v-deep .el-dialog__body{overflow: hidden !important;}.plan-img{width: 100%;height: 100%;overflow: auto;margin-bottom: 20px;position: relative;.bg{position: relative;  top:0px;  left:0px;}.block{cursor: pointer;z-index: 29;position:absolute;display: flex;flex-direction: row;width: 24px;height: 30px;.mark-icon{position: absolute;top: 0;left: 0;width: 24px;height: 30px;z-index: 30;}.button-group{position: absolute;left: 34px;top: 0px;display: flex;flex-direction: column;background: #ffffff;z-index: 40;span{padding: 10px 44px 10px 20px;white-space: nowrap;}span:first-child{border-bottom: 1px solid #DCDFE6;}}}}
}

上图用到的图片放在下面了

标点图:在这里插入图片描述
背景图:

在这里插入图片描述

切换自定义图标:

1.图例
在这里插入图片描述

这里我就不写具体功能实现js代码了,实现的思路是:
1.打开弹窗时必须要把markerArr置空并调用后台获取标点详情接口获取markerArr数据,下面组合数据可以参考
2.在标点选择关联的数据提交后,改变当前对象isDev属性为true切换图标样式并展示当前对象icon,下面HTML和CSS代码可以对比上文参考
3.如果当前对象的isDev已经为true,也可以切换另一条数据绑定Id进行重新绑定存入markerArr,这一步需要后台提供筛选数据的接口为基础,最后按后台保存接口参数要求筛选组合markerArr属性数据提交保存

2.代码

(1)组合数据

let arr=res.dataif(arr.length>0){arr.forEach(item=>{let obj={deptId:item.deptId,xcoordinate:item.xcoordinate?item.xcoordinate+'px':'',ycoordinate:item.ycoordinate?item.ycoordinate+'px':'',showModal:false,isDev:true,Id:item.Id,devName:item.devName,icon:item.icon}this.markerArr.push(obj)})
}

(2)html

<el-dialog title="设置设备" :visible.sync="imgMark" append-to-body id="plan"><div class="plan-img"><img class="bg" :src="bgSrc" @click.stop="setPoint" /><div v-for="(item, index) in markerArr" :key="index" :class="item.isDev==true?'blocks':'block'" :style="{ left: item.xcoordinate, top: item.ycoordinate, transform: 'translate(-50%, -100%)' }"@drag="dragenter($event, item)"draggable="true"><img v-if="item.isDev==false" :src="markSrc" class="mark-icon"  @click.stop="editPoit(index,item)" /> --><div v-if="item.isDev==true" class="marker-outer" @click.stop="editPoit(index,item)"><div class="marker-inner"><img :src="item.icon"/></div></div><div class="button-group" v-if="item.showModal"><span @click="associative(index)">关联设备</span><span @click="deletePoit(index)">删除点位</span></div></div></div><div slot="footer" class="dialog-footer"><el-button type="primary" @click="submitMark" class="button-minwidth">{{$t('Base.confirms')}}</el-button><el-button @click="imgMark = false" class="button-minwidth">{{$t('Base.cancels')}}</el-button></div></el-dialog>

(3)css

.blocks{cursor: pointer;z-index: 29;position:absolute;display: flex;flex-direction: row;width: 60px;height: 80px;.marker-outer{position: absolute;top: 0;left: 0;z-index: 30;display: inline-block;width: 66px;height: 66px;border-radius: 50%;background: #ffffff;text-align: center;box-shadow: 0px 0px 2px 3px #0095FF;.marker-inner{display: inline-block;width: 43px;height: 43px;border-radius: 50%;background: #fff;color: #111;position: absolute;top: 10px;left: 10px;z-index: 31;img{width: 100%;height: 100%;}}}

最近产品提了个做边界限制的需求,所以需要根据图标显示对按钮编辑框的显示位置进行动态变更

<div class="button-group" v-if="item.showModal":class="[toLeftBottom?'button-lb':'',toLeftTop?'button-lt':'',toRight?'buttom-r':'',toRightBottom?'buttom-rb':'',toRightTop?'buttom-rt':'']" ><span @click="associative(index)">关联设备</span><span @click="deletePoit(index)">删除点位</span>
</div>
data(){return{toLeftTop:false,  //左上toLeftBottom:false,  //左下toRight:false,toRightTop:false,  //右上toRightBottom:false,  //右下}
},
methods:{//编辑标记editPoit(index,item){this.markerArr.forEach(res=>{if(res!==item){res.showModal=false}else{if(item.showModal==false){item.showModal=true}else {item.showModal=false}}})let x=parseInt(item.xcoordinate)let y=parseInt(item.ycoordinate)let width=parseInt(this.imgWidth)let height=parseInt(this.imgHeight)this.toRight=falsethis.toLeftBottom=falsethis.toRightTop=falsethis.toLeftTop=falsethis.toRightBottom=falseif(x>=width-100){this.toRight=trueif(y<=50){this.toRightBottom=true}else if(y>=height-50){this.toRightTop=true}}else if(x<=20){if(y<=20){this.toLeftBottom=true}else if(y>=height-50){this.toLeftTop=true}}},
}
.button-lb{top: 80px;}
.button-lt{top:5px;}
.buttom-r{left: -130px;}
.buttom-rb{top: 80px;}
.buttom-rt{top: -40px;}.button-lb{top: 30px;}
.button-lt{top: -40px;}
.buttom-r{left: -130px;}
.buttom-rb{top: 30px;}
.buttom-rt{top: -40px;}

这篇关于vue实现平面图标记点位可拖拽可删除(可自定义标记图标)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Vue3 的 shallowRef 和 shallowReactive:优化性能

大家对 Vue3 的 ref 和 reactive 都很熟悉,那么对 shallowRef 和 shallowReactive 是否了解呢? 在编程和数据结构中,“shallow”(浅层)通常指对数据结构的最外层进行操作,而不递归地处理其内部或嵌套的数据。这种处理方式关注的是数据结构的第一层属性或元素,而忽略更深层次的嵌套内容。 1. 浅层与深层的对比 1.1 浅层(Shallow) 定义

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

电脑桌面文件删除了怎么找回来?别急,快速恢复攻略在此

在日常使用电脑的过程中,我们经常会遇到这样的情况:一不小心,桌面上的某个重要文件被删除了。这时,大多数人可能会感到惊慌失措,不知所措。 其实,不必过于担心,因为有很多方法可以帮助我们找回被删除的桌面文件。下面,就让我们一起来了解一下这些恢复桌面文件的方法吧。 一、使用撤销操作 如果我们刚刚删除了桌面上的文件,并且还没有进行其他操作,那么可以尝试使用撤销操作来恢复文件。在键盘上同时按下“C

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount