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
219 lines (209 loc) · 5.98 KB
/
hover.js
File metadata and controls
219 lines (209 loc) · 5.98 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
steal.plugins('jquery/event','jquery/event/livehack').then(function($){
/**
* @constructor jQuery.Hover
* @plugin jquery/event/hover
* @download jquery/dist/jquery.event.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").live("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
* @init Creates a new hover. This is never
* called directly.
*/
jQuery.Hover = function(){
this._delay = jQuery.Hover.delay;
this._distance = jQuery.Hover.distance;
};
/**
* @Static
*/
$.extend(jQuery.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
})
/**
* @Prototype
*/
$.extend(jQuery.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;
},
/**
* 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;
}
})
var $ = jQuery,
event = jQuery.event,
handle = event.handle,
onmouseenter = function(ev){
//now start checking mousemoves to update location
var delegate = ev.liveFired || ev.currentTarget;
var selector = ev.handleObj.selector;
var loc = {
pageX : ev.pageX,
pageY : ev.pageY
},
dist = 0,
timer,
entered = this,
called = false,
lastEv = ev,
hover = new jQuery.Hover();
$(entered).bind("mousemove.specialMouseEnter", {}, function(ev){
dist += Math.pow( ev.pageX-loc.pageX, 2 ) + Math.pow( ev.pageY-loc.pageY, 2 );
loc = {
pageX : ev.pageX,
pageY : ev.pageY
}
lastEv = ev
}).bind("mouseleave.specialMouseLeave",{}, function(ev){
clearTimeout(timer);
if(called){
$.each(event.find(delegate, ["hoverleave"], selector), function(){
this.call(entered, ev)
})
}
$(entered).unbind("mouseleave.specialMouseLeave")
})
$.each(event.find(delegate, ["hoverinit"], selector), function(){
this.call(entered, ev, hover)
})
timer = setTimeout(function(){
//check that we aren't moveing around
if(dist < hover._distance && $(entered).queue().length == 0){
$.each(event.find(delegate, ["hoverenter"], selector), function(){
this.call(entered, lastEv, hover)
})
called = true;
$(entered).unbind("mousemove.specialMouseEnter")
}else{
dist = 0;
timer = setTimeout(arguments.callee, hover._delay)
}
}, hover._delay)
};
/**
* @add jQuery.event.special static
*/
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").live("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").live("hoverenter", function(ev, hover){
* $(this).addClass("hovering");
* })
* @codeend
*/
"hoverenter",
/**
* @attribute hoverleave
* Called when the mouse leaves an element that has been
* hovered.
* @codestart
* $(".option").live("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").live("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 )
})