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調(diào)用微信JSDK 掃一掃,相冊等需要注意的事項
這篇文章主要介紹了vue調(diào)用微信JSDK 掃一掃,相冊等需要注意的事項,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2021-01-01vue2.5.2使用http請求獲取靜態(tài)json數(shù)據(jù)的實例代碼
這篇文章主要介紹了vue2.5.2使用http請求獲取靜態(tài)json數(shù)據(jù)的實例代碼,需要的朋友可以參考下2018-02-02vue中style設(shè)置scoped后部分樣式不生效的解決
這篇文章主要介紹了vue中style設(shè)置scoped后部分樣式不生效的解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-09-09Vue+axios實現(xiàn)統(tǒng)一接口管理的方法
這篇文章主要介紹了Vue+axios實現(xiàn)統(tǒng)一接口管理的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07