Vue3自定義drag指令詳解
新增drag.js文件
// 拖拽的指令
class Drap {
static zIndex = 1;
constructor(el, option = {}) {
this.el = el;
this.x = 0;
this.y = 0;
this.option = option;
this.init();
this.timeOutEvent = 0;
this.ele = null
}
init() {
this.setEleStyle(this.option || {});
//拖拽事件賦值給頭部標簽,此處代碼可實現(xiàn)僅在移動頭部時操作整個DOM塊
// this.ele = this.el.getElementsByClassName('header')[0]
// if(this.ele){
// this.ele.onmousedown = (e) => {
// this.onMouseDown(e)
// this.el.setCapture && this.el.setCapture() //全局捕獲
// }
// }else{
// this.el.onmousedown = (e) => {
// this.onMouseDown(e)
// this.el.setCapture && this.el.setCapture() //全局捕獲
// }
// }
this.el.onmousedown = (e) => {
this.gtouchstart(e)
}
}
//樣式設(shè)置
setEleStyle(option) {
for (const key in option) {
this.el.style[key] = option[key]
}
}
//按下ele
onMouseDown(e) {
let zIndex = getComputedStyle(this.el).getPropertyValue('z-index');
zIndex = isNaN(zIndex) ? 1 : zIndex
Drap.zIndex = Drap.zIndex > zIndex ? Number(Drap.zIndex) + 1 : Number(zIndex) + 1
this.setEleStyle({
"zIndex": Drap.zIndex,
position: 'fixed',
'cursor': 'move'
})
this.x = e.clientX - this.el.offsetLeft;
this.y = e.clientY - this.el.offsetTop;
document.onmousemove = (e) => this.onMouseMove(e);
document.onmouseup = (e) => this.onMouseUp(e)
}
//移動move
onMouseMove(e) {
this.gtouchmove()
let X = e.clientX - this.x
let Y = e.clientY - this.y
//下面代碼操作DOM元素不會移出屏幕,-25應(yīng)更換為-10,按自己需求設(shè)置
if (X < 0) {
X = 0
} else if (X > document.documentElement.clientWidth - this.el.offsetWidth) {
X = document.documentElement.clientWidth - this.el.offsetWidth - 25
}
if (Y < 0) {
Y = 0
} else if (Y > document.documentElement.clientHeight - this.el.offsetHeight) {
Y = document.documentElement.clientHeight - this.el.offsetHeight - 25
}
this.el.style.left = X + 'px'
this.el.style.top = Y + 'px'
}
//釋放
onMouseUp(e) {
this.gtouchend()
document.onmousemove = null
document.onmouseup = null
this.setEleStyle({
'cursor': 'pointer'
})
this.el.setCapture && this.el.setCapture() //釋放全局捕獲
}
gtouchstart(e) {
this.timeOutEvent = setTimeout(() => {
this.timeOutEvent = 0;
//真正長按后應(yīng)該執(zhí)行的內(nèi)容
console.log("長按事件觸發(fā)發(fā)");
this.onMouseDown(e)
this.el.setCapture && this.el.setCapture() //全局捕獲
}, 200);
return false;
}
gtouchmove() {
clearTimeout(this.timeOutEvent); //清除定時器
this.timeOutEvent = 0;
console.log("取消了");
}
gtouchend() {
clearTimeout(this.timeOutEvent); //清除定時器
if (this.timeOutEvent !== 0) {
console.log("你這是點擊,不是長按");
}
return false;
}
}
const drag = {
mounted(el, binding) {
new Drap(el, binding.value || {})
}
}
export default drag
main.js添加全局導(dǎo)入
import drag from './directives/drag.js'
app.directive('drag', drag)
頁面代碼使用v-drag
注:mini-dialog為自定義彈框組件
<div class="m-dia" v-if="dialogShow" v-drag>
<mini-dialog @closed="closedMini"></mini-dialog>
</div>
css樣式
注意,可移動塊必須是top和left屬性
.m-dia {
position: absolute;
top: 75%;
left: 70%;
}
結(jié)語:
上述代碼能完成基礎(chǔ)自定義拖拽功能,未測試僅抓取頭部才能拖動情況,且拖動代碼塊時光標顯示為指針,不是可移動光標。
到此這篇關(guān)于Vue3自定義drag指令詳解的文章就介紹到這了,更多相關(guān)Vue3自定義drag指令內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VUE父組件異步獲取數(shù)據(jù),子組件接收的值為空的問題
這篇文章主要介紹了VUE父組件異步獲取數(shù)據(jù),子組件接收的值為空的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
vue3中<script?setup>?和?setup函數(shù)的區(qū)別對比
這篇文章主要介紹了vue3中<script?setup>?和?setup函數(shù)的區(qū)別,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
Vue自定義復(fù)制指令 v-copy功能的實現(xiàn)
這篇文章主要介紹了Vue自定義復(fù)制指令 v-copy,使用自定義指令創(chuàng)建一個點擊復(fù)制文本功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01
Vue Cli與BootStrap結(jié)合實現(xiàn)表格分頁功能
這篇文章主要介紹了Vue Cli與BootStrap結(jié)合實現(xiàn)表格分頁功能,需要的朋友可以參考下2017-08-08

