-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
113 lines (89 loc) · 3.22 KB
/
index.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
/**
* Module dependencies
*/
var document = window.document,
body = document.body,
docEl = document.documentElement,
on = window.addEventListener || window.attachEvent,
moveEvent = (on === window.attachEvent) ? 'onmousemove' : 'mousemove',
moved = false,
requestAnimFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, 1000 / 60);
};
}()),
eve,
mousePosition = {};
function update() {
if (eve === undefined) { return; }
var coordX = 0,
coordY = 0;
if (eve.pageX || eve.pageY) {
coordX = eve.pageX;
coordY = eve.pageY;
} else {
coordX = eve.clientX + body.scrollLeft + docEl.scrollLeft;
coordY = eve.clientY + body.scrollTop + docEl.scrollTop;
}
mousePosition.x = coordX;
mousePosition.y = coordY;
eve = undefined;
}
on(moveEvent, function captureEvent(e) { eve = e || window.event; });
(function updateloop() {
requestAnimFrame(updateloop);
update();
}());
/**
* Calculates if the mouse position is near to a given element.
* @function
* @param {DOMElement} element - A given DOMElement.
* @param {Number} [distance] - Minimum distance (in pixels) between the DOMElement and mouse position.
* @returns {Boolean}
*/
function isNear(element, distance) {
distance = distance || 100;
var rect = element.getBoundingClientRect(),
area = {
'top': rect.top - distance,
'right': rect.right + distance,
'bottom': rect.bottom + distance,
'left': rect.left - distance
},
near = false,
percentageX = 0,
percentageY = 0;
if ((mousePosition.x >= area.left && mousePosition.x <= area.right) && (mousePosition.y >= area.top && mousePosition.y <= area.bottom)) {
if ((mousePosition.x >= rect.left && mousePosition.x <= rect.right) && (mousePosition.y >= rect.top && mousePosition.y <= rect.bottom)) {
near = 'inside';
} else {
// from the left
if (mousePoint.x >= area.left && mousePoint.x <= offset.left) {
percentageX = (mousePoint.x - area.left) / (offset.left - area.left) * 100;
// from the right
} else if (mousePoint.x >= offset.right && mousePoint.x <= area.right) {
percentageX = (area.right - mousePoint.x) / (area.right - offset.right) * 100;
}
// from the top
if (mousePoint.y >= area.top && mousePoint.y <= offset.top) {
percentageY = (mousePoint.y - area.top) / (offset.top - area.top) * 100;
// from the bottom
} else if (mousePoint.y >= offset.bottom && mousePoint.y <= area.bottom) {
percentageY = (area.bottom - mousePoint.y) / (area.bottom - offset.bottom) * 100;
}
percentageX = Math.floor(percentageX);
percentageY = Math.floor(percentageY);
near = [percentageX, percentageY];
}
} else {
near = false;
}
return near;
}
/**
* Expose isNear
*/
exports = module.exports = isNear;