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
290 lines (283 loc) · 8.34 KB
/
hover.js
File metadata and controls
290 lines (283 loc) · 8.34 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
steal('jquery', 'jquerypp/event/livehack', function ($) {
/**
* @class jQuery.Hover
* @plugin jquerypp/event/hover
* @download http://jmvcsite.heroku.com/pluginify?plugins[]=jquerypp/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;
var pending = $.data(delegate,"_hover"+selector);
// prevents another mouseenter until current has run its course
if(pending) {
// Under some circumstances, mouseleave may never fire
// (e.g., the element is removed while hovered)
// so if we've entered another element, wait the leave time,
// then force it to release.
if(!pending.forcing) {
pending.forcing = true;
clearTimeout(pending.leaveTimer);
var leaveTime = pending.leaving ?
Math.max(0,pending.hover.leave - (new Date() - pending.leaving)) :
pending.hover.leave;
var self = this;
setTimeout(function() {
pending.callHoverLeave();
onmouseenter.call(self,ev);
},leaveTime);
}
return;
}
var loc = {
pageX : ev.pageX,
pageY : ev.pageY
},
// The current distance
dist = 0,
// Timer that checks for the distance travelled
timer,
enteredEl = this,
// If we are hovered
hovered = false,
// The previous event
lastEv = ev,
// The $.Hover instance passed to events
hover = new $.Hover(),
// timer if hover.leave has been called
leaveTimer,
// Callback for triggering hoverleave
callHoverLeave = function () {
$.each(event.find(delegate, ["hoverleave"], selector), function () {
this.call(enteredEl, ev, hover)
})
cleanUp();
},
mousemove = function (ev) {
clearTimeout(leaveTimer);
// Update the distance and location
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);
if (hovered) {
// go right away
if (hover._leave === 0) {
callHoverLeave();
} else {
clearTimeout(leaveTimer);
// leave the hover after the time set in hover.leave(time)
pending.leaving = new Date();
leaveTimer = pending.leaveTimer = setTimeout(function(){
callHoverLeave();
}, hover._leave)
}
} else {
cleanUp();
}
},
cleanUp = function () {
// Unbind all events and data
$(enteredEl).unbind("mouseleave", mouseleave)
$(enteredEl).unbind("mousemove", mousemove);
$.removeData(delegate, "_hover" + selector)
},
hoverenter = function() {
$.each(event.find(delegate, ["hoverenter"], selector), function () {
this.call(enteredEl, lastEv, hover)
})
hovered = true;
};
pending = {
callHoverLeave: callHoverLeave,
hover: hover
};
$.data(delegate,"_hover"+selector, pending);
// Bind the mousemove event
$(enteredEl).bind("mousemove", mousemove).bind("mouseleave", mouseleave);
// call hoverinit for each element with the hover instance
$.each(event.find(delegate, ["hoverinit"], selector), function () {
this.call(enteredEl, ev, hover)
})
if (hover._delay === 0) {
hoverenter();
} else {
timer = setTimeout(function () {
// check that we aren't moving around
if (dist < hover._distance && $(enteredEl).queue().length == 0) {
hoverenter();
return;
} else {
// Reset distance and timer
dist = 0;
timer = setTimeout(arguments.callee, hover._delay)
}
}, hover._delay);
}
};
/**
* @add jQuery.event.special
*/
// Attach events
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)
return $;
})