export const dragPlugin = {
data() {
return {
isDown: false,
moveX: 0,
moveY: 0,
click_x: 0,
click_y: 0,
div_x: 0,
div_y: 0,
elementObj: null,
clickCicleTime: ''
}
},
methods: {
onMousedown(e, className) {
if (!e || !className) {
return;
}
this.clickCicleTime = + new Date()
this.click_x = e.clientX
this.click_y = e.clientY
this.isDown = true
this.elementObj = document.getElementsByClassName(className)[0];
this.div_x = this.elementObj.offsetLeft
this.div_y = this.elementObj.offsetTop
let self = this;
document.addEventListener('mousemove', function (e) {
self.dragMouseMove(e);
})
this.elementObj.addEventListener('mouseup', function (e) {
e.stopPropagation();
e.preventDefault();
self.onMouseup(e);
})
},
onMouseup(e) {
var event = (e) ? e : window.event;
event.stopPropagation();
event.preventDefault();
let _clickCicleTime = + new Date()
let differTime = _clickCicleTime - this.clickCicleTime
if (differTime < 200 && this.isDown) {
if (this.elementObj && this.elementObj.className == 'lvs-broadcast__zoom-out') {
this.showPanel = !this.showPanel;
}
}
this.isDown = false
this.mouseMoveX = 0
this.mouseMoveY = 0
document.removeEventListener('mousemove', this.dragMouseMove)
this.elementObj.removeEventListener('mouseup', this.onMouseup)
},
dragMouseMove(e) {
this.moveX = e.clientX
this.moveY = e.clientY
this.mouseMoveX = this.moveX - this.click_x
this.mouseMoveY = this.moveY - this.click_y
if (this.isDown) {
var new_div_x = this.div_x + this.mouseMoveX
var new_div_y = this.div_y + this.mouseMoveY
this.elementObj.style.left = new_div_x + 'px'
this.elementObj.style.top = new_div_y + 'px'
}
}
}
}
调用:
import { dragPlugin } from 'xxx'
mixins: [dragPlugin],