-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.delegateHoverIntent.js
99 lines (82 loc) · 2.67 KB
/
jquery.delegateHoverIntent.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
/** @file
*
* A simple, hover intent delegate.
*
* Requires:
* jquery.js
*/
(function($) {
/** @brief Create a hoverIntent delegate for the source element.
* @param sel The selection to use for the delegate;
* @param cb If provided, the callback to invoke on hover in/out;
*
* Triggers an event of type 'hover-in' or 'hover-out' on the source element.
*
* If the callback is provided, will also provide a proxy for the 'hover-in'
* and 'hover-out' events and pass them directly to the callback.
*/
$.fn.delegateHoverIntent = function(sel, cb) {
var $self = this;
// Set up the mouseenter/leave delegate
$self.delegate(sel, 'mouseenter mouseleave', function(e) {
var $el = $(this);
var timer = $el.data('hoverIntentTimer');
var hoverE = $.Event( e ); // Make the originalEvent available
//console.log('delegateHoverIntent mouse: '+ e.type);
if (e.type === 'mouseleave')
{
// event: mouseleave
if (timer)
{
// Just cancel the pending change
/*
console.log('delegateHoverIntent mouse: '+ e.type
+' -- cancel timer');
// */
clearTimeout(timer);
return;
}
/*
console.log('delegateHoverIntent mouse: '+ e.type
+' -- trigger "hover-out"');
// */
hoverE.type = 'hover-out';
$el.trigger( hoverE );
return;
}
/*
console.log('delegateHoverIntent mouse: '+ e.type
+ ' -- set timer...');
// */
// event: mouseenter
timer = setTimeout(function() {
$el.removeData('hoverIntentTimer');
/*
console.log('delegateHoverIntent mouse: '+ e.type
+' -- trigger "hover-in"');
// */
hoverE.type = 'hover-in';
$el.trigger( hoverE );
}, 250);
$el.data('hoverIntentTimer', timer);
});
if (cb)
{
/* Set up a 'hover-in'/'hover-out' delegate to invoke the provide
* callback.
*/
$self.delegate(sel, 'hover-in hover-out', function(e) {
cb.call(this, e);
});
}
};
/** @brief Remove a hoverIntent delegate for the source element.
* @param sel The selection to use for the delegate;
*/
$.fn.undelegateHoverIntent = function(sel) {
var $self = this;
// Set up the mouseenter/leave delegate
$self.undelegate(sel, 'mouseenter mouseleave');
$self.undelegate(sel, 'hover-in hover-out');
};
}(jQuery));