vue實現(xiàn)彈窗拖拽效果
本文實例為大家分享了vue實現(xiàn)彈窗拖拽效果的具體代碼,供大家參考,具體內(nèi)容如下
前言
實現(xiàn)拖拽其實簡單來說就分為三步
一、創(chuàng)建一個js文件
因為本身dialog窗口不具備移動拖拽能力,所以需要以下方法去實現(xiàn),在src/components同級文件下創(chuàng)建utils文件夾,然后創(chuàng)建名為directives.js的文件。
directives.js的代碼如下:
import Vue from 'vue'
?
// v-dialogDrag: 彈窗拖拽
Vue.directive('dialogDrag', {
? bind(el, binding, vnode, oldVnode) {
? ? const dialogHeaderEl = el.querySelector('.el-dialog__header')
? ? const dragDom = el.querySelector('.el-dialog')
? ? dialogHeaderEl.style.cursor = 'move'
?
? ? // 獲取原有屬性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
? ? const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null)
?
? ? dialogHeaderEl.onmousedown = (e) => {
? ? ? // 鼠標按下,計算當前元素距離可視區(qū)的距離
? ? ? const disX = e.clientX - dialogHeaderEl.offsetLeft
? ? ? const disY = e.clientY - dialogHeaderEl.offsetTop
?
? ? ? // 獲取到的值帶px 正則匹配替換
? ? ? let styL, styT
?
? ? ? // 注意在ie中 第一次獲取到的值為組件自帶50% 移動之后賦值為px
? ? ? if (sty.left.includes('%')) {
? ? ? ? styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100)
? ? ? ? styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100)
? ? ? } else {
? ? ? ? styL = +sty.left.replace(/\px/g, '')
? ? ? ? styT = +sty.top.replace(/\px/g, '')
? ? ? }
?
? ? ? document.onmousemove = function(e) {
? ? ? ? // 通過事件委托,計算移動的距離
? ? ? ? const l = e.clientX - disX
? ? ? ? const t = e.clientY - disY
?
? ? ? ? // 移動當前元素
? ? ? ? dragDom.style.left = `${l + styL}px`
? ? ? ? dragDom.style.top = `${t + styT}px`
?
? ? ? ? // 將此時的位置傳出去
? ? ? ? // binding.value({x:e.pageX,y:e.pageY})
? ? ? }
?
? ? ? document.onmouseup = function(e) {
? ? ? ? document.onmousemove = null
? ? ? ? document.onmouseup = null
? ? ? }
? ? }
? }
})
?
// v-dialogDragWidth: 彈窗寬度拖大 拖小
Vue.directive('dialogDragWidth', {
? bind(el, binding, vnode, oldVnode) {
? ? const dragDom = binding.value.$el.querySelector('.el-dialog')
?
? ? el.onmousedown = (e) => {
? ? ? // 鼠標按下,計算當前元素距離可視區(qū)的距離
? ? ? const disX = e.clientX - el.offsetLeft
?
? ? ? document.onmousemove = function(e) {
? ? ? ? e.preventDefault() // 移動時禁用默認事件
?
? ? ? ? // 通過事件委托,計算移動的距離
? ? ? ? const l = e.clientX - disX
? ? ? ? dragDom.style.width = `${l}px`
? ? ? }
?
? ? ? document.onmouseup = function(e) {
? ? ? ? document.onmousemove = null
? ? ? ? document.onmouseup = null
? ? ? }
? ? }
? }
})
二、在main.js里面引入此模塊
代碼如下:
import './utils/directives.js' //為彈窗移動
三、在需要進行彈窗的里面設(shè)置以下代碼,標簽引入 v-dialogDrag
<el-dialog ? ? ? ? ? ?? ? ? ? ? ? ? width="80%" ? ? ? ? ? ? title="傳感器數(shù)據(jù)" ? ? ? ? ? ? :visible.sync="dialogVisible" ? ? ? ? ? ? :before-close="handleClose"? ? ? ? ? ? ? :modal-append-to-body='false' ? ? ? ? ? ? append-to-body ? ? ? ? ? ? class="upload-dialog" ? ? ? ? ? ? v-dialogDrag? ? ? ? ? ? ? :close-on-click-modal="false" ? ? ? ? > ? ? ? ? </el-dialog
注意::close-on-click-modal="false"一定要添加這句話,要不然沒有效果。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue el-table實現(xiàn)多選框回填的示例代碼
摘要:Vue多選框回填是實現(xiàn)表單數(shù)據(jù)高效處理的常見需求,本文主要介紹了vue el-table實現(xiàn)多選框回填的示例代碼,具有一定的參考價值,感興趣的可以了解一下2024-01-01
vue/cli?配置動態(tài)代理無需重啟服務的操作方法
vue-cli是vue.js的腳手架,用于自動生成vue.js+webpack的項目模板,分為vue?init?webpack-simple?項目名和vue?init?webpack?項目名兩種,這篇文章主要介紹了vue/cli?配置動態(tài)代理,無需重啟服務,需要的朋友可以參考下2022-05-05
vue3使用vis繪制甘特圖制作timeline可拖動時間軸及時間軸中文化(推薦)
這篇文章主要介紹了vue3使用vis繪制甘特圖制作timeline可拖動時間軸,時間軸中文化,本文結(jié)合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-02-02

