ElementUI的Dialog彈窗實(shí)現(xiàn)拖拽移動(dòng)功能示例代碼
在項(xiàng)目中使用el-dialog中發(fā)現(xiàn)不能夠拖拽移動(dòng),因此網(wǎng)上找了相關(guān)資料,使用自定義指令實(shí)現(xiàn)拖拽功能。
1、創(chuàng)建自定義指令:
新建文件directive/el-drag-dialog/index.js
import drag from "./drag"; const install = function (Vue) { Vue.directive("el-drag-dialog", drag); }; if (window.Vue) { window["el-drag-dialog"] = drag; Vue.use(install); } drag.install = install; export default drag;
新建文件directive/el-drag-dialog/drag.js
export default { bind(el, binding, vnode) { const dialogHeaderEl = el.querySelector(".el-dialog__header"); const dragDom = el.querySelector(".el-dialog"); dialogHeaderEl.style.cssText += ";cursor:move;"; dragDom.style.cssText += ";top:0px;"; // 獲取原有屬性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null); const getStyle = (function () { if (window.document.currentStyle) { return (dom, attr) => dom.currentStyle[attr]; } else { return (dom, attr) => getComputedStyle(dom, false)[attr]; } })(); dialogHeaderEl.onmousedown = (e) => { // 鼠標(biāo)按下,計(jì)算當(dāng)前元素距離可視區(qū)的距離 const disX = e.clientX - dialogHeaderEl.offsetLeft; const disY = e.clientY - dialogHeaderEl.offsetTop; const dragDomWidth = dragDom.offsetWidth; const dragDomheight = dragDom.offsetHeight; const screenWidth = document.body.clientWidth; const screenHeight = document.body.clientHeight; const minDragDomLeft = dragDom.offsetLeft; const maxDragDomLeft = screenWidth - dragDom.offsetLeft - dragDomWidth; const minDragDomTop = dragDom.offsetTop; const maxDragDomTop = screenHeight - dragDom.offsetTop - dragDomheight; // 獲取到的值帶px 正則匹配替換 let styL = getStyle(dragDom, "left"); let styT = getStyle(dragDom, "top"); if (styL.includes("%")) { styL = +document.body.clientWidth * (+styL.replace(/%/g, "") / 100); styT = +document.body.clientHeight * (+styT.replace(/%/g, "") / 100); } else { styL = +styL.replace(/\px/g, ""); styT = +styT.replace(/\px/g, ""); } document.onmousemove = function (e) { // 通過事件委托,計(jì)算移動(dòng)的距離 let left = e.clientX - disX; let top = e.clientY - disY; // 邊界處理 if (-left > minDragDomLeft) { left = -minDragDomLeft; } else if (left > maxDragDomLeft) { left = maxDragDomLeft; } if (-top > minDragDomTop) { top = -minDragDomTop; } else if (top > maxDragDomTop) { top = maxDragDomTop; } // 移動(dòng)當(dāng)前元素 dragDom.style.cssText += `;left:${left + styL}px;top:${top + styT}px;`; // emit onDrag event vnode.child.$emit("dragDialog"); }; document.onmouseup = function () { document.onmousemove = null; document.onmouseup = null; }; }; }, };
2、引入自定義指令
import elDragDialog from "@/directive/el-drag-dialog";
directives: { elDragDialog },
3、使用自定義指令(v-el-darg-dialog)
<el-dialog v-el-drag-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose" v-if="dialogVisible" > <span>這是一段信息</span> <span slot="footer" class="dialog-footer"> <el-button @click="dialogVisible = false">取 消</el-button> <el-button type="primary" @click="dialogVisible = false" >確 定</el-button > </span> </el-dialog>
其中v-el-drag-dialog為自定義指令,v-if主要為了處理每次打開彈框都在中間位置。
4、完整代碼:
<template> <div> <el-button @click="dialogVisible = true">打開彈框</el-button> <el-dialog v-el-drag-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose" v-if="dialogVisible" > <span>這是一段信息</span> <span slot="footer" class="dialog-footer"> <el-button @click="dialogVisible = false">取 消</el-button> <el-button type="primary" @click="dialogVisible = false" >確 定</el-button > </span> </el-dialog> </div> </template> <script> import elDragDialog from "@/directive/el-drag-dialog"; export default { name: "DialogView", directives: { elDragDialog, }, data() { return { dialogVisible: false, }; }, methods: { handleClose() { this.dialogVisible = false; }, }, }; </script>
5、效果
到此這篇關(guān)于ElementUI的Dialog彈窗實(shí)現(xiàn)拖拽移動(dòng)功能的文章就介紹到這了,更多相關(guān)ElementUI Dialog彈窗拖拽移動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js 設(shè)置選中行的樣式的實(shí)現(xiàn)代碼
設(shè)置選中的行樣式的js代碼,需要的朋友可以參考下,這里只給出了函數(shù),具體的自己發(fā)揮。2010-05-05如何利用Three.js實(shí)現(xiàn)web端顯示點(diǎn)云數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于如何利用Three.js實(shí)現(xiàn)web端顯示點(diǎn)云數(shù)據(jù)的相關(guān)資料,最近在項(xiàng)目中遇到需求,需要在web端顯示點(diǎn)云數(shù)據(jù),將我的實(shí)現(xiàn)步驟介紹在這里供大家參考,需要的朋友可以參考下2023-11-11js給對(duì)象動(dòng)態(tài)添加、設(shè)置、刪除屬性名與屬性值實(shí)例代碼
由于項(xiàng)目需要常常會(huì)遇到為某一個(gè)對(duì)象動(dòng)態(tài)添加屬性的情況,下面這篇文章主要給大家介紹了關(guān)于js給對(duì)象動(dòng)態(tài)添加、設(shè)置、刪除屬性名與屬性值的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11javascript動(dòng)態(tài)創(chuàng)建及刪除元素的方法
這篇文章主要介紹了javascript動(dòng)態(tài)創(chuàng)建及刪除元素的方法,涉及針對(duì)DOM元素操作的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12js實(shí)現(xiàn)溫度計(jì)時(shí)間樣式代碼分享
本文主要介紹了javascript實(shí)現(xiàn)溫度計(jì)時(shí)間樣式,推薦給大家,有需要的小伙伴可以參考下。2015-08-08非常好用的JsonToString 方法 簡(jiǎn)單實(shí)例
這篇文章介紹了非常好用的JsonToString簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-07-07JS實(shí)現(xiàn)可自定義大小,可雙擊關(guān)閉的彈出層效果
這篇文章主要介紹了JS實(shí)現(xiàn)可自定義大小,可雙擊關(guān)閉的彈出層效果,涉及JavaScript定時(shí)函數(shù)及頁(yè)面元素動(dòng)態(tài)操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10js監(jiān)聽輸入框值的即時(shí)變化onpropertychange、oninput
很多情況下我們都會(huì)即時(shí)監(jiān)聽輸入框值的變化,以便作出即時(shí)動(dòng)作去引導(dǎo)瀏覽者增強(qiáng)網(wǎng)站的用戶體驗(yàn)感。2011-07-07