vue實(shí)現(xiàn)彈窗拖拽效果
本文實(shí)例為大家分享了vue實(shí)現(xiàn)彈窗拖拽效果的具體代碼,供大家參考,具體內(nèi)容如下
前言
實(shí)現(xiàn)拖拽其實(shí)簡(jiǎn)單來(lái)說(shuō)就分為三步
一、創(chuàng)建一個(gè)js文件
因?yàn)楸旧韉ialog窗口不具備移動(dòng)拖拽能力,所以需要以下方法去實(shí)現(xiàn),在src/components同級(jí)文件下創(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) => { ? ? ? // 鼠標(biāo)按下,計(jì)算當(dāng)前元素距離可視區(qū)的距離 ? ? ? const disX = e.clientX - dialogHeaderEl.offsetLeft ? ? ? const disY = e.clientY - dialogHeaderEl.offsetTop ? ? ? ? // 獲取到的值帶px 正則匹配替換 ? ? ? let styL, styT ? ? ? ? // 注意在ie中 第一次獲取到的值為組件自帶50% 移動(dòng)之后賦值為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) { ? ? ? ? // 通過(guò)事件委托,計(jì)算移動(dòng)的距離 ? ? ? ? const l = e.clientX - disX ? ? ? ? const t = e.clientY - disY ? ? ? ? ? // 移動(dòng)當(dāng)前元素 ? ? ? ? dragDom.style.left = `${l + styL}px` ? ? ? ? dragDom.style.top = `${t + styT}px` ? ? ? ? ? // 將此時(shí)的位置傳出去 ? ? ? ? // 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) => { ? ? ? // 鼠標(biāo)按下,計(jì)算當(dāng)前元素距離可視區(qū)的距離 ? ? ? const disX = e.clientX - el.offsetLeft ? ? ? ? document.onmousemove = function(e) { ? ? ? ? e.preventDefault() // 移動(dòng)時(shí)禁用默認(rèn)事件 ? ? ? ? ? // 通過(guò)事件委托,計(jì)算移動(dòng)的距離 ? ? ? ? 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' //為彈窗移動(dòng)
三、在需要進(jìn)行彈窗的里面設(shè)置以下代碼,標(biāo)簽引入 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"一定要添加這句話,要不然沒(méi)有效果。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue自定義指令實(shí)現(xiàn)彈窗拖拽四邊拉伸及對(duì)角線拉伸效果
- VUE實(shí)現(xiàn)可隨意拖動(dòng)的彈窗組件
- 使用Vue組件實(shí)現(xiàn)一個(gè)簡(jiǎn)單彈窗效果
- 關(guān)于vue.js彈窗組件的知識(shí)點(diǎn)總結(jié)
- vue實(shí)現(xiàn)點(diǎn)擊按鈕“查看詳情”彈窗展示詳情列表操作
- 很棒的vue彈窗組件
- vue彈窗消息組件的使用方法
- vue中實(shí)現(xiàn)點(diǎn)擊空白區(qū)域關(guān)閉彈窗的兩種方法
- Vue中關(guān)閉彈窗組件時(shí)銷(xiāo)毀并隱藏操作
相關(guān)文章
vue調(diào)用微信JSDK 掃一掃,相冊(cè)等需要注意的事項(xiàng)
這篇文章主要介紹了vue調(diào)用微信JSDK 掃一掃,相冊(cè)等需要注意的事項(xiàng),幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2021-01-01vue2.5.2使用http請(qǐng)求獲取靜態(tài)json數(shù)據(jù)的實(shí)例代碼
這篇文章主要介紹了vue2.5.2使用http請(qǐng)求獲取靜態(tài)json數(shù)據(jù)的實(shí)例代碼,需要的朋友可以參考下2018-02-02vue中style設(shè)置scoped后部分樣式不生效的解決
這篇文章主要介紹了vue中style設(shè)置scoped后部分樣式不生效的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-09-09Vue+axios實(shí)現(xiàn)統(tǒng)一接口管理的方法
這篇文章主要介紹了Vue+axios實(shí)現(xiàn)統(tǒng)一接口管理的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07