forked from bitovi/jquerypp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhover.js
More file actions
247 lines (239 loc) · 6.74 KB
/
hover.js
File metadata and controls
247 lines (239 loc) · 6.74 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
steal('jquery/event','jquery/event/livehack').then(function($){
/**
* @class jQuery.Hover
* @plugin jquery/event/hover
* @download http://jmvcsite.heroku.com/pluginify?plugins[]=jquery/event/hover/hover.js
* @parent jQuery.event.hover
*
* Creates a new hover. The constructor should not be called directly.
*
* An instance of `$.Hover` is passed as the second argument to each
* [jQuery.event.hover] event handler:
*
* $('#menu').on("hoverinit", function(ev, hover) {
* // Set the hover distance to 20px
* hover.distance(20);
* });
*/
$.Hover = function(){
this._delay = $.Hover.delay;
this._distance = $.Hover.distance;
this._leave = $.Hover.leave
};
/**
* @Static
*/
$.extend($.Hover,{
/**
* @attribute delay
*
* `$.Hover.delay` is the delay (in milliseconds) after which the hover is
* activated by default.
*
* Set this value as a global default. The default is 100ms.
*
* // Set the global hover delay to 1 second
* $.Hover.delay = 1000;
*/
delay: 100,
/**
* @attribute distance
*
* `$.Hover.distance` is the maximum distance (in pixels) that the mouse is allowed to
* travel within the time of [jQuery.Hover.delay] in order to activate a hover.
*
* Set this value as a global default. The default is 10px.
*
* // Set the global hover distance to 1 pixel
* $.Hover.distance = 1;
*/
distance: 10,
leave : 0
})
/**
* @Prototype
*/
$.extend($.Hover.prototype,{
/**
* `hover.delay(time)` sets the delay (in ms) for this hover.
* This method should only be used in [jQuery.event.hover.hoverinit hoverinit]:
*
* $('.hoverable').on('hoverinit', function(ev, hover) {
* // Set the delay to 500ms
* hover.delay(500);
* });
*
* @param {Number} delay the number of milliseconds used to determine a hover
* @return {$.Hover} The hover object
*/
delay: function( delay ) {
this._delay = delay;
return this;
},
/**
* `hover.distance(px) sets the maximum distance (in pixels) the mouse is allowed to travel in order to activate
* the hover. This method should only be used in [jQuery.event.hover.hoverinit hoverinit]:
*
* $('.hoverable').on('hoverinit', function(ev, hover) {
* // Set the distance to 1px
* hover.distance(1);
* });
*
* @param {Number} distance the max distance in pixels a mouse can move to be considered a hover
* @return {$.Hover} The hover object
*/
distance: function( distance ) {
this._distance = distance;
return this;
},
/**
* `hover.leave(delay)` sets a delay for how long the hover should stay active after the mouse left.
* This method should only be used in [jQuery.event.hover.hoverinit hoverinit]:
*
* $('.hoverable').on('hoverinit', function(ev, hover) {
* // Stay active for another second after the mouse left
* hover.leave(1000);
* });
*
* @param {Number} delay the number of milliseconds the hover should stay active after the mouse leaves
* @return {$.Hover} The hover object
*/
leave : function(leave){
this._leave = leave;
return this;
}
})
var event = $.event,
handle = event.handle,
onmouseenter = function(ev){
//now start checking mousemoves to update location
var delegate = ev.delegateTarget || ev.currentTarget;
var selector = ev.handleObj.selector;
//prevents another mouseenter until current has run its course
if($.data(delegate,"_hover"+selector)){
return;
}
$.data(delegate,"_hover"+selector, true)
var loc = {
pageX : ev.pageX,
pageY : ev.pageY
},
dist = 0,
timer,
enteredEl = this,
hovered = false,
lastEv = ev,
hover = new $.Hover(),
leaveTimer,
callHoverLeave = function(){
$.each(event.find(delegate, ["hoverleave"], selector), function(){
this.call(enteredEl, ev, hover)
})
cleanUp();
},
mouseenter = function(ev){
clearTimeout(leaveTimer);
dist += Math.pow( ev.pageX-loc.pageX, 2 ) + Math.pow( ev.pageY-loc.pageY, 2 );
loc = {
pageX : ev.pageX,
pageY : ev.pageY
}
lastEv = ev
},
mouseleave = function(ev){
clearTimeout(timer);
// go right away
if(hovered){
if(hover._leave === 0){
callHoverLeave();
}else{
clearTimeout(leaveTimer);
leaveTimer = setTimeout(function(){
callHoverLeave();
}, hover._leave)
}
}else{
cleanUp();
}
},
cleanUp = function(){
$(enteredEl).unbind("mouseleave",mouseleave)
$(enteredEl).unbind("mousemove",mouseenter);
$.removeData(delegate,"_hover"+selector)
};
$(enteredEl).bind("mousemove",mouseenter).bind("mouseleave", mouseleave);
$.each(event.find(delegate, ["hoverinit"], selector), function(){
this.call(enteredEl, ev, hover)
})
timer = setTimeout(function(){
//check that we aren't moveing around
if(dist < hover._distance && $(enteredEl).queue().length == 0){
$.each(event.find(delegate, ["hoverenter"], selector), function(){
this.call(enteredEl, lastEv, hover)
})
hovered = true;
return;
}else{
dist = 0;
timer = setTimeout(arguments.callee, hover._delay)
}
}, hover._delay)
};
/**
* @add jQuery.event.special
*/
event.setupHelper( [
/**
* @attribute hoverinit
* @parent jQuery.event.hover
*
* `hoverinit` is called when a hover is about to start (on `mouseenter`). Listen for `hoverinit` events to configure
* [jQuery.Hover::delay delay] and [jQuery.Hover::distance distance]
* for this specific event:
*
* $(".option").on("hoverinit", function(ev, hover){
* //set the distance to 10px
* hover.distance(10);
* //set the delay to 200ms
* hover.delay(10);
* // End the hover one second after the mouse leaves
* hover.leave(1000);
* })
*/
"hoverinit",
/**
* @attribute hoverenter
* @parent jQuery.event.hover
*
* `hoverenter` events are called when the mouses less than [jQuery.Hover.prototype.distance] pixels in
* [jQuery.Hover.prototype.delay delay] milliseconds.
*
* $(".option").on("hoverenter", function(ev, hover){
* $(this).addClass("hovering");
* })
*/
"hoverenter",
/**
* @attribute hoverleave
* @parent jQuery.event.hover
*
* `hoverleave` is called when the mouse leaves an element that has been hovered.
*
* $(".option").on("hoverleave", function(ev, hover){
* $(this).removeClass("hovering");
* })
*/
"hoverleave",
/**
* @attribute hovermove
* @parent jQuery.event.hover
*
* `hovermove` is called when a `mousemove` occurs on an element that has been hovered.
*
* $(".option").on("hovermove", function(ev, hover){
* // not sure why you would want to listen for this
* // but we provide it just in case
* })
*/
"hovermove"], "mouseenter", onmouseenter )
})