-
Notifications
You must be signed in to change notification settings - Fork 3
/
Dragify.js
143 lines (116 loc) · 4.51 KB
/
Dragify.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
;(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define([], factory)
} else if (typeof exports === 'object') {
// Node, CommonJS
module.exports = factory()
} else {
// Window
root.Dragify = factory()
}
}(this, function () {
var Watcher = function () {
this.events = {}
}
Watcher.prototype = {
construct: Watcher,
on: function (type, fn) {
this._getEventInfo(type).push(fn)
return this
},
trigger: function (type) {
var event = this._getEventInfo(type)
var params = Array.prototype.slice.call(arguments, 1)
event.forEach(function (fn) {
fn.apply(fn, params)
})
return this
},
_getEventInfo: function (type) {
if (!this.events[type]) this.events[type] = []
return this.events[type]
},
remove: function (type, fn) {
var event = this._getEventInfo(type)
if (!fn) {
this.events[type] = []
} else {
event.splice(event.indexOf(fn), 1)
}
return this
}
}
/**
* 让元素可拖动
* @param elem 拖动元素
*/
function Class(elem) {
this.$elem = elem
this.watcher = new Watcher()
this.init()
}
Class.prototype = {
constructor: Class,
init: function () {
var that = this
that.$elem.addEventListener(EVENTS[0], function (e) {
var eInfo = that._getEventInfo(e)
var $parent = that.$elem.offsetParent
var diffX = eInfo.clientX - that.$elem.offsetLeft
var diffY = eInfo.clientY - that.$elem.offsetTop
var pDiffX = $parent.offsetLeft || 0
var pDiffY = $parent.offsetTop || 0
var windowWidth = document.documentElement.clientWidth
var windowHeight = document.documentElement.clientHeight
var elemWidth = that.$elem.offsetWidth
var elemHeight = that.$elem.offsetHeight
var zIndex = getComputedStyle(that.$elem).zIndex
that.$elem.classList.add('drag-start')
that.watcher.trigger('start', that.$elem)
document.addEventListener(EVENTS[1], move)
function move(e) {
var eInfo = that._getEventInfo(e)
var left = eInfo.clientX - diffX
var top = eInfo.clientY - diffY
if (left + pDiffX < 0) left = left - (left + pDiffX)
if (top + pDiffY < 0) top = top - (top + pDiffY)
if (left + pDiffX + elemWidth > windowWidth) left = windowWidth - (pDiffX + elemWidth)
if (top + pDiffY + elemHeight > windowHeight) top = windowHeight - (pDiffY + elemHeight)
that.$elem.style.position = 'absolute'
that.$elem.style.left = left + 'px'
that.$elem.style.top = top + 'px'
that.$elem.style.zIndex = 19911125
that.$elem.classList.add('drag-move')
that.watcher.trigger('move', that.$elem)
}
document.addEventListener(EVENTS[2], end)
function end(e) {
document.removeEventListener(EVENTS[1], move)
document.removeEventListener(EVENTS[2], end)
that.$elem.style.zIndex = zIndex
that.$elem.classList.remove('drag-start')
that.$elem.classList.remove('drag-move')
that.watcher.trigger('end', that.$elem)
}
})
},
on: function () {
this.watcher.on.apply(this.watcher, arguments)
return this.watcher
},
_getEventInfo: function (e) {
return Class.isTouch() ? e.targetTouches[0] : e
}
}
Class.isTouch = function (e) {
return 'ontouchstart' in window ||
window.DocumentTouch && document instanceof window.DocumentTouch ||
navigator.maxTouchPoints > 0 ||
window.navigator.msMaxTouchPoints > 0
}
var EVENTS = Class.isTouch()
? ["touchstart", "touchmove", "touchend"]
: ["mousedown", "mousemove", "mouseup"]
return Class
}))