VUE3实现拖拽功能自定义指令
1.首先创建一个js文件,命名为drag.js
注意看注释部分,对操作DOM块进行了不同需求的支持
可以只在移动头部时操作整个DOM,或者是否允许DOM元素移出屏幕都能实现
-
// 拖拽的指令
-
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 || {});
-
//拖拽事件赋值给头部标签,此处代码可实现仅在移动头部时操作整个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.onMouseDown(e)
-
this.el.setCapture && this.el.setCapture() //全局捕获
-
}
-
}
-
-
//样式设置
-
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) {
-
let X = e.clientX - this.x
-
let Y = e.clientY - this.y;
-
//此处使用注释处的代码,此处代码操作DOM块会移出屏幕
-
// if (X < 10 - this.el.offsetWidth) {
-
// X = 10 - this.el.offsetWidth
-
// } else if (X > document.documentElement.clientWidth - 10) {
-
// X = document.documentElement.clientWidth - 10
-
// }
-
// if (Y < 10 - this.el.clientHeight) {
-
// Y = 10 - this.el.clientHeight
-
// } else if (Y > document.documentElement.clientHeight - 10) {
-
// Y = document.documentElement.clientHeight - 10
-
// }
-
//下面代码操作DOM元素不会移出屏幕,-25应更换为-10,按自己需求设置
-
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) {
-
document.onmousemove = null
-
document.onmouseup = null
-
this.setEleStyle({
-
'cursor': 'pointer'
-
})
-
this.el.setCapture && this.el.setCapture() //释放全局捕获
-
}
-
// gtouchstart() {
-
// this.timeOutEvent = setTimeout(() => {
-
// this.timeOutEvent = 0;
-
// //真正长按后应该执行的内容
-
// console.log("长按事件触发发");
-
// }, 500);
-
// return false;
-
// }
-
-
// gtouchmove() {
-
// clearTimeout(this.timeOutEvent); //清除定时器
-
// this.timeOutEvent = 0;
-
// console.log("取消了");
-
// }
-
-
// gtouchend() {
-
// clearTimeout(this.timeOutEvent); //清除定时器
-
// if (this.timeOutEvent !== 0) {
-
// console.log("你这是点击,不是长按");
-
// }
-
// return false;
-
// }
-
}
-
export const drag = {
-
mounted(el, binding) {
-
new Drap(el, binding.value || {})
-
}
-
}
2.在main.js中引入drag.js
-
import {createApp} from 'vue';
-
import App from './App.vue';
-
import {drag} from "../public/js/drag.js"
-
-
let app = createApp(App);
-
app.directive('drag', drag)
3.在你想使用的标签中添加v-drag即可实现拖动了
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfikgfg
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13