forked from jupiterjs/jquerymx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhover.js
More file actions
251 lines (243 loc) · 6.73 KB
/
hover.js
File metadata and controls
251 lines (243 loc) · 6.73 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
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
* Provides delegate-able hover events.
* <p>
* A hover happens when the mouse stops moving
* over an element for a period of time. You can listen
* and configure hover with the following events:
* </p>
* <ul>
* <li><code>[jQuery.event.special.hoverinit hoverinit]</code> - called on mouseenter, use this event to customize
* [jQuery.Hover.prototype.delay] and [jQuery.Hover.prototype.distance]</li>
* <li><code>[jQuery.event.special.hoverenter hoverenter]</code> - an element is being hovered</li>
* <li><code>[jQuery.event.special.hovermove hovermove]</code> - the mouse moves on an element that has been hovered</li>
* <li><code>[jQuery.event.special.hoverleave hoverleave]</code> - the mouse leaves the element that has been hovered</li>
* </ul>
* <h3>Quick Example</h3>
* The following listens for hoverenter and adds a class to style
* the element, and removes the class on hoverleave.
* @codestart
* $('#menu').delegate(".option","hoverenter",function(){
* $(this).addClass("hovering");
* }).delegate(".option","hoverleave",function(){
* $(this).removeClass("hovering");
* })
* @codeend
* <h2>Configuring Distance and Delay</h2>
* <p>An element is hovered when the mouse
* moves less than a certain distance in
* specific time over the element.
* </p>
* <p>
* You can configure that distance and time by
* adjusting the <code>distance</code> and
* <code>delay</code> values.
* </p>
* <p>You can set delay and distance globally
* by adjusting the static properties:</p>
* </p>
* @codestart
* $.Hover.delay = 10
* $.Hover.distance = 1
* @codeend
* <p>Or you can adjust delay and distance for
* an individual element in hoverenter:</p>
* @codestart
* $(".option").delegate("hoverinit", function(ev, hover){
* //set the distance to 10px
* hover.distance(10)
* //set the delay to 200ms
* hover.delay(10)
* })
* @codeend
* <h2>Demo</h2>
* @demo jquery/event/hover/hover.html
* @parent specialevents
* @constructor Creates a new hover. This is never
* called directly.
*/
$.Hover = function(){
this._delay = $.Hover.delay;
this._distance = $.Hover.distance;
this._leave = $.Hover.leave
};
/**
* @Static
*/
$.extend($.Hover,{
/**
* @attribute delay
* A hover is activated if it moves less than distance in this time.
* Set this value as a global default.
*/
delay: 100,
/**
* @attribute distance
* A hover is activated if it moves less than this distance in delay time.
* Set this value as a global default.
*/
distance: 10,
leave : 0
})
/**
* @Prototype
*/
$.extend($.Hover.prototype,{
/**
* Sets the delay for this hover. This method should
* only be used in hoverinit.
* @param {Number} delay the number of milliseconds used to determine a hover
*
*/
delay: function( delay ) {
this._delay = delay;
return this;
},
/**
* Sets the distance for this hover. This method should
* only be used in hoverinit.
* @param {Number} distance the max distance in pixels a mouse can move to be considered a hover
*/
distance: function( distance ) {
this._distance = distance;
return this;
},
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
* Listen for hoverinit events to configure
* [jQuery.Hover.prototype.delay] and [jQuery.Hover.prototype.distance]
* for the current element. Hoverinit is called on mouseenter.
* @codestart
* $(".option").delegate("hoverinit", function(ev, hover){
* //set the distance to 10px
* hover.distance(10)
* //set the delay to 200ms
* hover.delay(10)
* })
* @codeend
*/
"hoverinit",
/**
* @attribute hoverenter
* Hoverenter events are called when the mouses less
* than [jQuery.Hover.prototype.distance] pixels in
* [jQuery.Hover.prototype.delay] milliseconds.
* @codestart
* $(".option").delegate("hoverenter", function(ev, hover){
* $(this).addClass("hovering");
* })
* @codeend
*/
"hoverenter",
/**
* @attribute hoverleave
* Called when the mouse leaves an element that has been
* hovered.
* @codestart
* $(".option").delegate("hoverleave", function(ev, hover){
* $(this).removeClass("hovering");
* })
* @codeend
*/
"hoverleave",
/**
* @attribute hovermove
* Called when the mouse moves on an element that
* has been hovered.
* @codestart
* $(".option").delegate("hovermove", function(ev, hover){
* //not sure why you would want to listen for this
* //but we provide it just in case
* })
* @codeend
*/
"hovermove"], "mouseenter", onmouseenter )
})