欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • vue使用axios訪問本地json文件404問題及解決

    vue使用axios訪問本地json文件404問題及解決

    這篇文章主要介紹了vue使用axios訪問本地json文件404問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 如何利用vue實(shí)現(xiàn)css過渡和動(dòng)畫

    如何利用vue實(shí)現(xiàn)css過渡和動(dòng)畫

    過渡Vue在插入、更新或者移除 DOM 時(shí),提供多種不同方式的應(yīng)用過渡效果這篇文章主要給大家介紹了關(guān)于如何利用vue實(shí)現(xiàn)css過渡和動(dòng)畫的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • vue 項(xiàng)目中實(shí)現(xiàn)按鈕防抖方法

    vue 項(xiàng)目中實(shí)現(xiàn)按鈕防抖方法

    這篇文章主要介紹了vue 項(xiàng)目中實(shí)現(xiàn)按鈕防抖方法,首先需要新建 .js文件存放防抖方法,引入防抖文件,methods中添加方法,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • vue實(shí)現(xiàn)拖拽與排序功能的示例代碼

    vue實(shí)現(xiàn)拖拽與排序功能的示例代碼

    在Web應(yīng)用程序中,實(shí)現(xiàn)拖拽和排序功能是非常常見的需求,本文為大家介紹了如何使用Vue來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單但功能強(qiáng)大的拖拽與排序功能,感興趣的小伙伴可以參考下
    2023-10-10
  • VUE父組件異步獲取數(shù)據(jù),子組件接收的值為空的問題

    VUE父組件異步獲取數(shù)據(jù),子組件接收的值為空的問題

    這篇文章主要介紹了VUE父組件異步獲取數(shù)據(jù),子組件接收的值為空的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue基于better-scroll仿京東分類列表

    vue基于better-scroll仿京東分類列表

    這篇文章主要為大家詳細(xì)介紹了vue基于better-scroll仿京東分類列表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • vue3中<script?setup>?和?setup函數(shù)的區(qū)別對(duì)比

    vue3中<script?setup>?和?setup函數(shù)的區(qū)別對(duì)比

    這篇文章主要介紹了vue3中<script?setup>?和?setup函數(shù)的區(qū)別,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04
  • 使用vue實(shí)現(xiàn)HTML頁(yè)面生成圖片的方法

    使用vue實(shí)現(xiàn)HTML頁(yè)面生成圖片的方法

    這篇文章主要介紹了使用vue實(shí)現(xiàn)HTML頁(yè)面生成圖片的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Vue自定義復(fù)制指令 v-copy功能的實(shí)現(xiàn)

    Vue自定義復(fù)制指令 v-copy功能的實(shí)現(xiàn)

    這篇文章主要介紹了Vue自定義復(fù)制指令 v-copy,使用自定義指令創(chuàng)建一個(gè)點(diǎn)擊復(fù)制文本功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-01-01
  • Vue Cli與BootStrap結(jié)合實(shí)現(xiàn)表格分頁(yè)功能

    Vue Cli與BootStrap結(jié)合實(shí)現(xiàn)表格分頁(yè)功能

    這篇文章主要介紹了Vue Cli與BootStrap結(jié)合實(shí)現(xiàn)表格分頁(yè)功能,需要的朋友可以參考下
    2017-08-08

最新評(píng)論