vue項(xiàng)目中實(shí)現(xiàn)el-dialog組件可拖拽效果
0. 首先上圖,看效果
1. 實(shí)現(xiàn)方法
第一步:創(chuàng)建 drag.js文件 實(shí)現(xiàn)拖拽源碼
/** ?* 拖拽移動(dòng) ?* @param ?{elementObjct} bar 鼠標(biāo)點(diǎn)擊控制拖拽的元素 ?* @param {elementObjct} ?target 移動(dòng)的元素 ?* @param {function} ?callback 移動(dòng)后的回調(diào) ?*/ export function startDrag(bar, target, callback) { ? ? var params = { ? ? ? top: 0, ? ? ? left: 0, ? ? ? currentX: 0, ? ? ? currentY: 0, ? ? ? flag: false, ? ? ? cWidth: 0, ? ? ? cHeight: 0, ? ? ? tWidth: 0, ? ? ? tHeight: 0 ? ? }; ?? ? ? // 給拖動(dòng)塊添加樣式 ? ? bar.style.cursor = 'move'; ?? ? ? // 獲取相關(guān)CSS屬性 ? ? // o是移動(dòng)對(duì)象 ? ? // var getCss = function (o, key) { ? ? // ? return o.currentStyle ? o.currentStyle[key] : document.defaultView.getComputedStyle(o, false)[key]; ? ? // }; ?? ? ? bar.onmousedown = function (event) { ? ? ? // 按下時(shí)初始化params ? ? ? var e = event ? event : window.event; ? ? ? params = { ? ? ? ? top: target.offsetTop, ? ? ? ? left: target.offsetLeft, ? ? ? ? currentX: e.clientX, ? ? ? ? currentY: e.clientY, ? ? ? ? flag: true, ? ? ? ? cWidth: document.body.clientWidth, ? ? ? ? cHeight: document.body.clientHeight, ? ? ? ? tWidth: target.offsetWidth, ? ? ? ? tHeight: target.offsetHeight ? ? ? }; ?? ? ? ? // 給被拖動(dòng)塊初始化樣式 ? ? ? target.style.margin = 0; ? ? ? target.style.top = params.top + 'px'; ? ? ? target.style.left = params.left + 'px'; ?? ? ? ? if (!event) { ? ? ? ? // 防止IE文字選中 ? ? ? ? bar.onselectstart = function () { ? ? ? ? ? return false; ? ? ? ? } ? ? ? } ?? ? ? ? document.onmousemove = function (event) { ? ? ? ? // 防止文字選中 ? ? ? ? window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty(); ?? ? ? ? ? var e = event ? event : window.event; ? ? ? ? if (params.flag) { ? ? ? ? ? var nowX = e.clientX; ? ? ? ? ? var nowY = e.clientY; ? ? ? ? ? // 差異距離 ? ? ? ? ? var disX = nowX - params.currentX; ? ? ? ? ? var disY = nowY - params.currentY; ? ? ? ? ? // 最終移動(dòng)位置 ? ? ? ? ? var zLeft = 0; ? ? ? ? ? var zTop = 0; ?? ? ? ? ? ? zLeft = parseInt(params.left) + disX; ? ? ? ? ? // 限制X軸范圍 ? ? ? ? ? if (zLeft <= -parseInt(params.tWidth / 2)) { ? ? ? ? ? ? zLeft = -parseInt(params.tWidth / 2); ? ? ? ? ? } ? ? ? ? ? if (zLeft >= params.cWidth - parseInt(params.tWidth * 0.5)) { ? ? ? ? ? ? zLeft = params.cWidth - parseInt(params.tWidth * 0.5); ? ? ? ? ? } ?? ? ? ? ? ? zTop = parseInt(params.top) + disY; ? ? ? ? ? // 限制Y軸范圍 ? ? ? ? ? if (zTop <= 0) { ? ? ? ? ? ? zTop = 0; ? ? ? ? ? } ? ? ? ? ? if (zTop >= params.cHeight - parseInt(params.tHeight * 0.5)) { ? ? ? ? ? ? zTop = params.cHeight - parseInt(params.tHeight * 0.5); ? ? ? ? ? } ?? ? ? ? ? ? // 執(zhí)行移動(dòng) ? ? ? ? ? target.style.left = zLeft + 'px'; ? ? ? ? ? target.style.top = zTop + 'px'; ? ? ? ? } ?? ? ? ? ? if (typeof callback == "function") { ? ? ? ? ? callback(zLeft, zTop); ? ? ? ? } ? ? ? } ?? ? ? ? document.onmouseup = function () { ? ? ? ? params.flag = false; ? ? ? ? document.onmousemove = null; ? ? ? ? document.onmouseup = null; ? ? ? }; ? ? }; ? }
第二步:新建 directive.js 文件,創(chuàng)建vue指令配置文件
// 引入拖拽js import { startDrag } from './drag.js' /** ?* 為el-dialog彈框增加拖拽功能 ?* @param {*} el 指定dom ?* @param {*} binding 綁定對(duì)象 ?* desc ? 只要用到了el-dialog的組件,都可以通過(guò)增加v-draggable屬性變?yōu)榭赏献У膹椏? ?*/ const draggable = (el, binding) => { ? ? // 綁定拖拽事件 [綁定拖拽觸發(fā)元素為彈框頭部、拖拽移動(dòng)元素為整個(gè)彈框] ? ? startDrag(el.querySelector('.el-dialog__header'), el.querySelector('.el-dialog'), binding.value); }; const directives = { ? ? draggable, }; // 這種寫法可以批量注冊(cè)指令 export default { ? install(Vue) { ? ? ? Object.keys(directives).forEach((key) => { ? ? ? Vue.directive(key, directives[key]); ? ? }); ? }, };
第三步:main.js文件中全局引入vue指令
/* 注冊(cè)全局指令 */ import directive from './utils/directive'; Vue.use(directive) ? 第四步:使用v-draagble <el-dialog ? ?v-draggable ? ?title="新增" ? ?:visible.sync="isShow" ? ?@close="handleCancelConfigInfo"> ? ?<-- xxxx 彈框內(nèi)容... --> ? ?</el-form> ? ?<div slot="footer"> ? ? ? <el-button size="small">取消</el-button> ? ? ? <el-button size="small">保存</el-button> ? ?</div> </el-dialog>
注意事項(xiàng)
文件引入路徑需前后保持一致
參考資料
Vue 自定義拖拽指令 v-drag vue+element 實(shí)現(xiàn)拖拽 Drag 彈框
到此這篇關(guān)于vue項(xiàng)目中實(shí)現(xiàn)el-dialog組件可拖拽效果的文章就介紹到這了,更多相關(guān)vue el-dialog可拖拽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VUE異步更新DOM - 用$nextTick解決DOM視圖的問(wèn)題
這篇文章主要介紹了VUE異步更新DOM - 用$nextTick解決DOM視圖的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11Vue項(xiàng)目如何根據(jù)圖片url獲取file對(duì)象并用axios上傳
這篇文章主要介紹了Vue項(xiàng)目如何根據(jù)圖片url獲取file對(duì)象并用axios上傳問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09vue實(shí)現(xiàn)將數(shù)據(jù)存入vuex中以及從vuex中取出數(shù)據(jù)
今天小編就為大家分享一篇vue實(shí)現(xiàn)將數(shù)據(jù)存入vuex中以及從vuex中取出數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11Vue實(shí)現(xiàn)簡(jiǎn)單搜索功能的示例代碼
在vue項(xiàng)目中,搜索功能是我們經(jīng)常需要使用的一個(gè)場(chǎng)景,最常用的是在列表數(shù)據(jù)中搜索一個(gè)想要的,今天的例子就是我們實(shí)現(xiàn)vue從列表數(shù)據(jù)中搜索,希望對(duì)大家有所幫助2023-03-03vue如何通過(guò)點(diǎn)擊事件彈出彈窗頁(yè)面詳解
彈窗是我們開(kāi)發(fā)中經(jīng)常遇到的一個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于vue如何通過(guò)點(diǎn)擊事件彈出彈窗頁(yè)面的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06詳解最新vue-cli 2.9.1的webpack存在問(wèn)題
這篇文章主要介紹了最新vue-cli 2.9.1的webpack存在問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12vue監(jiān)聽(tīng)瀏覽器原生返回按鈕,進(jìn)行路由轉(zhuǎn)跳操作
這篇文章主要介紹了vue監(jiān)聽(tīng)瀏覽器原生返回按鈕,進(jìn)行路由轉(zhuǎn)跳操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09公共Hooks封裝useTableData表格數(shù)據(jù)實(shí)例
這篇文章主要為大家介紹了公共Hooks封裝useTableData表格數(shù)據(jù)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12