Vue實(shí)現(xiàn)dom元素拖拽并限制移動(dòng)范圍的操作代碼
需求
dom元素拖拽并限制在父組件范圍內(nèi)
拖拽功能封裝
export const initVDrag = (vue) => { vue.directive('drag', (el) => { const oDiv = el // 當(dāng)前元素 oDiv.onmousedown = (e) => { let target = oDiv while ( window.getComputedStyle(target).position !== 'absolute' && target !== document.body ) { target = target.parentElement } let parent = target.parentNode document.onselectstart = () => { return false } if (!target.getAttribute('init_x')) { target.setAttribute('init_x', target.offsetLeft) target.setAttribute('init_y', target.offsetTop) } // e.clientX, e.clientY是鼠標(biāo)點(diǎn)擊的位置 // target.offsetLeft, target.offsetTop是當(dāng)前元素左上角的位置 // 計(jì)算鼠標(biāo)按下的位置距離當(dāng)前元素左上角的距離 const disX = e.clientX - target.offsetLeft const disY = e.clientY - target.offsetTop // target.clientWidth, target.clientHeight是當(dāng)前元素的尺寸 // parent.clientWidth, parent.clientHeight是父元素的尺寸 // parent.offsetLeft, parent.offsetTop是父元素左上角的位置 // 可移動(dòng)范圍的位置 const minX = parent.offsetLeft const maxX = parent.offsetLeft + parent.clientWidth - target.clientWidth const minY = parent.offsetTop const maxY = parent.offsetTop + parent.clientHeight - target.clientHeight document.onmousemove = (e) => { // 通過(guò)事件委托,計(jì)算移動(dòng)的距離,e是最新的鼠標(biāo)位置,disX、disY是鼠標(biāo)剛點(diǎn)擊時(shí)的位置 let l = e.clientX - disX let t = e.clientY - disY // 約束移動(dòng)范圍在父元素區(qū)域內(nèi) if (l < minX) { l = minX } else if (l > maxX) { l = maxX } if (t < minY) { t = minY } else if (t > maxY) { t = maxY } // 給當(dāng)前元素樣式中的left和top賦值 target.style.left = l + 'px' target.style.top = t + 'px' } document.onmouseup = (e) => { document.onmousemove = null document.onmouseup = null document.onselectstart = null } // 不return false的話,可能導(dǎo)致鼠標(biāo)黏連,鼠標(biāo)粘在dom上拿不下來(lái),相當(dāng)于onmouseup失效 return false } }) }
使用拖拽功能
以vite為例:
vite-env.d.ts
... declare module '@utils/directive/vDrag.js' ...
main.ts
... import { createApp } from 'vue' import { initVDrag } from '@/utils/directive/vDrag.js' ... let instance: any = null instance = createApp(App) initVDrag(instance) ...
test.vue
<template> <div v-drag /> </template>
到此這篇關(guān)于Vue實(shí)現(xiàn)dom元素拖拽并限制移動(dòng)范圍的文章就介紹到這了,更多相關(guān)vue dom元素拖拽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue使用axios跨域請(qǐng)求數(shù)據(jù)問(wèn)題詳解
這篇文章主要為大家詳細(xì)介紹了vue使用axios跨域請(qǐng)求數(shù)據(jù)的問(wèn)題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Vue2安裝使用MonacoEditor方式(自定義語(yǔ)法,自定義高亮,自定義提示)
這篇文章主要介紹了Vue2安裝使用MonacoEditor方式(自定義語(yǔ)法,自定義高亮,自定義提示),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04Vue3中的?computed,watch,watchEffect的使用方法
這篇文章主要介紹了Vue3中的?computed,watch,watchEffect的使用方法,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)價(jià)值,需要得小伙伴可以參考一下2022-06-06Vue常見(jiàn)報(bào)錯(cuò)以及解決方案實(shí)例總結(jié)
最近做了一個(gè)比較老的vue項(xiàng)目,啟動(dòng)居然各種報(bào)錯(cuò),下面這篇文章主要給大家介紹了關(guān)于Vue常見(jiàn)報(bào)錯(cuò)以及解決方案的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07Vue3中的ref為何要用.value進(jìn)行值的調(diào)用呢
這篇文章主要介紹了Vue3中的ref為何要用.value進(jìn)行值的調(diào)用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09vue實(shí)現(xiàn)短信驗(yàn)證碼登錄功能(流程詳解)
無(wú)論是移動(dòng)端還是pc端登錄或者注冊(cè)界面都會(huì)見(jiàn)到手機(jī)驗(yàn)證碼登錄這個(gè)功能,輸入手機(jī)號(hào),得到驗(yàn)證碼,這篇文章主要介紹了基于vue實(shí)現(xiàn)短信驗(yàn)證碼登錄功能,需要的朋友可以參考下2019-12-12公共Hooks封裝useTableData表格數(shù)據(jù)實(shí)例
這篇文章主要為大家介紹了公共Hooks封裝useTableData表格數(shù)據(jù)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12vue3使用mitt.js實(shí)現(xiàn)各種組件間的通信
在vue工程中,除開(kāi)vue自帶的什么父子間,祖孫間通信,還有一個(gè)非常方便的通信方式,類似Vue2.x?使用?EventBus?進(jìn)行組件通信,而?Vue3.x?推薦使用?mitt.js,可以實(shí)現(xiàn)各個(gè)組件間的通信,所以本文給大家介紹了vue3使用mitt.js實(shí)現(xiàn)組件通信,需要的朋友可以參考下2024-05-05