最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue3自定義drag指令詳解

 更新時(shí)間:2023年12月20日 16:33:03   作者:藏在角落的星  
這篇文章主要為大家詳細(xì)介紹了Vue3自定義drag指令的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

新增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 || {});
		//拖拽事件賦值給頭部標(biāo)簽,此處代碼可實(shí)現(xiàn)僅在移動(dòng)頭部時(shí)操作整個(gè)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)
	}
	//移動(dòng)move
	onMouseMove(e) {
		this.gtouchmove()
		let X = e.clientX - this.x
		let Y = e.clientY - this.y
    //下面代碼操作DOM元素不會(huì)移出屏幕,-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;
			//真正長(zhǎng)按后應(yīng)該執(zhí)行的內(nèi)容
			console.log("長(zhǎng)按事件觸發(fā)發(fā)");
			this.onMouseDown(e)
			this.el.setCapture && this.el.setCapture() //全局捕獲
		}, 200); 
		return false;
	}
	gtouchmove() {
		clearTimeout(this.timeOutEvent); //清除定時(shí)器
		this.timeOutEvent = 0;
		console.log("取消了");
	}
	gtouchend() {
		clearTimeout(this.timeOutEvent); //清除定時(shí)器
		if (this.timeOutEvent !== 0) {
			console.log("你這是點(diǎn)擊,不是長(zhǎng)按");
		}
		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)

頁(yè)面代碼使用v-drag

注:mini-dialog為自定義彈框組件

<div class="m-dia" v-if="dialogShow" v-drag>
    <mini-dialog @closed="closedMini"></mini-dialog>
</div>

css樣式

注意,可移動(dòng)塊必須是top和left屬性

.m-dia {
    position: absolute;
    top: 75%;
    left: 70%;
}

結(jié)語(yǔ):

上述代碼能完成基礎(chǔ)自定義拖拽功能,未測(cè)試僅抓取頭部才能拖動(dòng)情況,且拖動(dòng)代碼塊時(shí)光標(biāo)顯示為指針,不是可移動(dòng)光標(biāo)。

到此這篇關(guān)于Vue3自定義drag指令詳解的文章就介紹到這了,更多相關(guān)Vue3自定義drag指令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

武川县| 松滋市| 沙洋县| 永胜县| 余江县| 冕宁县| 湛江市| 公安县| 常熟市| 新巴尔虎右旗| 东山县| 六安市| 龙井市| 汝南县| 武夷山市| 宿州市| 邯郸县| 富川| 扎鲁特旗| 岳普湖县| 沧州市| 弥勒县| 个旧市| 余姚市| 武功县| 林周县| 鲁甸县| 瓮安县| 大洼县| 朔州市| 东平县| 通河县| 郸城县| 错那县| 恩施市| 边坝县| 平遥县| 湖口县| 新邵县| 黄龙县| 临朐县|