-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.hoverDirection.js
58 lines (53 loc) · 1.32 KB
/
jquery.hoverDirection.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
/*
* jQuery hoverDirection plugin
* @version: 1.0
* @author: Jerome Musialak <[email protected]> - http://www.musialak.fr
*/
(function($) {
$.fn.hoverDirection = function (handlerIn, handlerOut) {
var oldPosition = {pageX : 0, pageY : 0};
$(document).mousemove(function (e) {
oldPosition.pageX = e.pageX;
oldPosition.pageY = e.pageY;
});
return this.each(
function () {
var $this = $(this);
var direction = "top";
var offset = $this.offset();
var grid = {
top: offset.top,
left: offset.left,
bottom: offset.top + $this.height(),
right: offset.left + $this.width()
};
$(this).hover(function(e) {
if (oldPosition.pageX > grid.left && oldPosition.pageX < grid.right)
{
direction = "top";
if (e.pageY <= oldPosition.pageY)
direction = "bottom";
}
else
{
direction = "right";
if (e.pageX >= oldPosition.pageX)
direction = "left";
}
if (handlerIn)
handlerIn.apply(this, [direction]);
}, function(e) {
if (e.pageX => grid.right)
direction = "right";
else if (e.pageX <= grid.left)
direction = "left";
else if (e.pageY >= grid.bottom)
direction = "bottom";
else
direction = "top";
if (handlerOut)
handlerOut.apply(this, [direction]);
});
});
};
})(jQuery);