forked from Eished/JavaScript_notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLimitDrag.js
More file actions
29 lines (28 loc) · 970 Bytes
/
LimitDrag.js
File metadata and controls
29 lines (28 loc) · 970 Bytes
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
// 继承属性
function LimitDrag(id) {
Drag.call(this, id);
}
// 继承原型
for (var i in Drag.prototype) {
LimitDrag.prototype[i] = Drag.prototype[i];
}
LimitDrag.prototype.mouseMove = function(ev) {
// 不断获取Event 对象,坐标才会不断更新
var ev = event||ev;
// console.log('可视区鼠标X:', ev.clientX, '鼠标Y:',ev.clientY);
// div位置 = 鼠标可视区新的位置 - 鼠标与div的距离
var l = ev.clientX - this.disX;
var t = ev.clientY - this.disY;
if (l < 0) {
l = 0;
} else if (l > document.documentElement.clientWidth - this.oDiv.offsetWidth) {
l = document.documentElement.clientWidth - this.oDiv.offsetWidth;
}
if ( t < 0) {
t = 0;
} else if (t > document.documentElement.clientHeight - this.oDiv.offsetHeight) {
t = document.documentElement.clientHeight - this.oDiv.offsetHeight;
}
this.oDiv.style.top = t + 'px';
this.oDiv.style.left = l + 'px';
}