-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathvue-drag.js
91 lines (90 loc) · 2.94 KB
/
vue-drag.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
*Vue-drag.js v1.1.0
*By BosenY
*/
function detectmob() {
if( navigator.userAgent.match(/Android/i)
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/iPhone/i)
|| navigator.userAgent.match(/iPad/i)
|| navigator.userAgent.match(/iPod/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/Windows Phone/i)
){
return true;
}
else {
return false;
}
}
(function () {
var vueDrag = {}
vueDrag.install = function (Vue) {
Vue.directive("drag", {
bind: function (el, binding) {
// console.log(binding)
el.style.position = "absolute"
var isChildDom
if (binding.value !== undefined) {
isChildDom = true
} else {
isChildDom = false
}
var offsetX = 0
var offsetY = 0
function down(e) {
offsetX = (e.pageX - el.offsetLeft)
offsetY = (e.pageY - el.offsetTop)
if (isChildDom) {
var childdom = el.querySelector(binding.value)
childdom.style.position = "relative"
if(binding.modifiers.cursor) childdom.style.cursor = "move"
var barStyle = childdom.currentStyle
? childdom.currentStyle
: window.getComputedStyle(childdom, null)
var boxStyle = el.currentStyle
? el.currentStyle
: window.getComputedStyle(el, null)
var left = Number(barStyle.getPropertyValue("left").replace("px", "")) + Number(boxStyle.getPropertyValue("left").replace("px", "")) + Number(boxStyle.getPropertyValue("border-left-width").replace("px", ""))
var right = left + Number(barStyle.getPropertyValue("width").replace("px", ""))
var top = Number(barStyle.getPropertyValue("top").replace("px", "")) + Number(boxStyle.getPropertyValue("top").replace("px", "")) + Number(boxStyle.getPropertyValue("border-top-width").replace("px", ""))
var bottom = top + Number(barStyle.getPropertyValue("height").replace("px", ""))
// console.log(`left:${left}`)
// console.log(`right:${right}`)
// console.log(`top:${top}`)
// console.log(`bottom:${bottom}`)
// console.log(`clientX: ${e.clientX}`)
// console.log(`clientY: ${e.clientY}`)
if (e.clientX <= right && e.clientX >= left && e.clientY >= top && e.clientY <= bottom) {
addEventListener("mousemove", move)
addEventListener("mouseup", up)
}
} else {
if(binding.modifiers.cursor) el.style.cursor = "move"
addEventListener("mousemove", move)
addEventListener("mouseup", up)
}
}
function move(e) {
el.style.left = (e.pageX - offsetX) + "px"
el.style.top = (e.pageY - offsetY) + "px"
}
function up() {
removeEventListener("mousemove", move)
removeEventListener("mouseup", up)
}
el.addEventListener("mousedown", down)
}
})
}
if (typeof exports == "object") {
module.exports = vueDrag
} else if (typeof define == "function" && define.amd) {
define([], function () {
return vueDrag
})
} else if (window.Vue) {
window.vueDrag = vueDrag
Vue.use(vueDrag)
}
})()