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的組件,都可以通過增加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學(xué)習(xí)筆記之vue1.0和vue2.0的區(qū)別介紹
今天我們來說一說vue1.0和vue2.0的主要變化有哪些?對(duì)vue相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2017-05-05
Vue實(shí)現(xiàn)組件間通信的幾種方式(多種場(chǎng)景)
本文主要介紹了Vue實(shí)現(xiàn)組件間通信的幾種方式,不同的場(chǎng)景使用不同的方式,基本滿足所有開發(fā)場(chǎng)景中的通信需求,感興趣的可以了解一下2021-10-10
vue-video-player 解決微信自動(dòng)全屏播放問題(橫豎屏導(dǎo)致樣式錯(cuò)亂問題)
這篇文章主要介紹了vue-video-player 解決微信自動(dòng)全屏播放問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
一文詳解Vue3組件通信輕松玩轉(zhuǎn)復(fù)雜數(shù)據(jù)流
在大型Vue項(xiàng)目中,組件通信如同神經(jīng)網(wǎng)絡(luò)般貫穿整個(gè)應(yīng)用,這篇文章將為大家詳細(xì)介紹一下Vue3中的組件通信方式,有需要的小伙伴可以了解下2025-02-02
vue init webpack 建vue項(xiàng)目報(bào)錯(cuò)的解決方法
今天小編就為大家分享一篇vue init webpack 建vue項(xiàng)目報(bào)錯(cuò)的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
element表單驗(yàn)證如何清除校驗(yàn)提示語
本文主要介紹了element表單驗(yàn)證如何清除校驗(yàn)提示語,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
antdv vue upload自定義上傳結(jié)合表單提交方式
這篇文章主要介紹了antdv vue upload自定義上傳結(jié)合表單提交方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue中動(dòng)態(tài)出來返回的時(shí)間秒數(shù)(在多少秒顯示分、小時(shí)等等)
這篇文章主要給大家介紹了關(guān)于vue中動(dòng)態(tài)出來返回的時(shí)間秒數(shù)(在多少秒顯示分、小時(shí)等等)的相關(guān)資料,文中通過代碼示例介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01

