|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>The mouseenter, mouseleave, and hover Events</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
<a href="#toc" class="jump">Jump to Table of Contents</a> |
|
|
24 |
|
|
|
25 |
|
|
|
26 |
<h1>The mouseenter, mouseleave, and hover Events</h1> |
|
|
27 |
<div class="yui3-g"> |
|
|
28 |
<div class="yui3-u-3-4"> |
|
|
29 |
<div id="main"> |
|
|
30 |
<div class="content"><div class="intro"> |
|
|
31 |
<p>The <code>event-mouseenter</code> module adds support for "mouseenter" and |
|
|
32 |
"mouseleave" events that are often preferable alternatives to "mouseover" |
|
|
33 |
and "mouseout".</p> |
|
|
34 |
|
|
|
35 |
<p>The <code>event-hover</code> module adds support for a "hover" event that combines |
|
|
36 |
subscriptions to each of these two events, taking two callback |
|
|
37 |
functions, one for when the mouse enters the subscribed node, and another |
|
|
38 |
for when it leaves the node.</p> |
|
|
39 |
</div> |
|
|
40 |
|
|
|
41 |
<h2 id="mouseover-and-mouseout-are-noisy"><code>mouseover</code> and <code>mouseout</code> are noisy</h2> |
|
|
42 |
|
|
|
43 |
<p>The <code>mouseover</code> and <code>mouseout</code> events bubble. That means with the following DOM structure...</p> |
|
|
44 |
|
|
|
45 |
<pre class="code prettyprint"><div id="hover-me"> |
|
|
46 |
<div class="header"> |
|
|
47 |
<button class="close">Close</button> |
|
|
48 |
</div> |
|
|
49 |
<div class="body"> |
|
|
50 |
... more markup |
|
|
51 |
</div> |
|
|
52 |
</div></pre> |
|
|
53 |
|
|
|
54 |
|
|
|
55 |
<p>...a <code>mouseover</code> subscription on <code>#hover-me</code> would be called three times when a user moved the mouse over the close button because</p> |
|
|
56 |
|
|
|
57 |
<ol> |
|
|
58 |
<li>The user moused over #hover-me</li> |
|
|
59 |
<li>The user moused over the <div> in #hover-me, and that <code>mouseover</code> event bubbled to #hover-me</li> |
|
|
60 |
<li>The user moused over the close button, and that <code>mouseover</code> event bubbled to #hover-me</li> |
|
|
61 |
</ol> |
|
|
62 |
|
|
|
63 |
<p>Chances are, you don't care about any of these except the first. That's where <code>mouseenter</code> and <code>mouseleave</code> come in. They only fire when the <code>e.target</code> of the event is the node subscribed to. That means no noise.</p> |
|
|
64 |
|
|
|
65 |
<pre class="code prettyprint">YUI().use('event-mouseenter', 'transition', function (Y) { |
|
|
66 |
var hoverMe = Y.one('#hover-me'); |
|
|
67 |
|
|
|
68 |
hoverMe.on('mouseenter', function () { |
|
|
69 |
this.one('.header').transition('fadeIn'); |
|
|
70 |
}); |
|
|
71 |
|
|
|
72 |
hoverMe.on('mouseleave', function () { |
|
|
73 |
this.one('.header').transition('fadeOut'); |
|
|
74 |
}); |
|
|
75 |
});</pre> |
|
|
76 |
|
|
|
77 |
|
|
|
78 |
<p>Though these events work by essentially <em>not</em> bubbling, you can still delegate <code>mouseenter</code> and <code>mouseleave</code> using <code>node.delegate(...)</code>.</p> |
|
|
79 |
|
|
|
80 |
<pre class="code prettyprint">// The overHandler callback will be executed when any .hoverable |
|
|
81 |
// element is moused over. |
|
|
82 |
container.delegate('mouseenter', overHandler, '.hoverable');</pre> |
|
|
83 |
|
|
|
84 |
|
|
|
85 |
<h2 id="hover"><code>mouseenter</code> + <code>mouseleave</code> == <code>hover</code></h2> |
|
|
86 |
|
|
|
87 |
<p>It's common to pair <code>mouseenter</code> and <code>mouseleave</code> subscriptions (or |
|
|
88 |
<code>mouseover</code> and <code>mouseout</code> for the uninitiated) to create an effect that only |
|
|
89 |
lasts as long as the mouse is over an element. To make that easier, the |
|
|
90 |
<code>event-hover</code> module adds a <code>hover</code> event. It accepts a second callback and |
|
|
91 |
attaches the first to <code>mouseenter</code> and the second to <code>mouseleave</code>.</p> |
|
|
92 |
|
|
|
93 |
<pre class="code prettyprint">YUI().use('event-hover', 'transition', function (Y) { |
|
|
94 |
function over() { |
|
|
95 |
this.one('.header').transition('fadeIn'); |
|
|
96 |
} |
|
|
97 |
function out() { |
|
|
98 |
this.one('.header').transition('fadeOut'); |
|
|
99 |
} |
|
|
100 |
|
|
|
101 |
// Two callbacks, `mouseenter` and `mouseleave` |
|
|
102 |
Y.one('#hover-me').on('hover', over, out); |
|
|
103 |
});</pre> |
|
|
104 |
|
|
|
105 |
|
|
|
106 |
<p>If you need to override the default <code>this</code> object assignment or bind arguments to a <code>hover</code> subscription, just add those arguments after the second callback.</p> |
|
|
107 |
|
|
|
108 |
<p>Similarly, if you want to delegate the <code>hover</code> event, pass a CSS filter after the second callback.</p> |
|
|
109 |
|
|
|
110 |
<pre class="code prettyprint">// Default `this` to racerX |
|
|
111 |
mach5.on('hover', racerX.oilSlick, racerX.tireSpikes, racerX); |
|
|
112 |
|
|
|
113 |
// The delegate filter comes before the `this` override |
|
|
114 |
ruralTown.delegate('hover', ufo.scan, ufo.memoryWipe, '.person', ufo);</pre> |
|
|
115 |
|
|
|
116 |
</div> |
|
|
117 |
</div> |
|
|
118 |
</div> |
|
|
119 |
|
|
|
120 |
<div class="yui3-u-1-4"> |
|
|
121 |
<div class="sidebar"> |
|
|
122 |
|
|
|
123 |
<div id="toc" class="sidebox"> |
|
|
124 |
<div class="hd"> |
|
|
125 |
<h2 class="no-toc">Table of Contents</h2> |
|
|
126 |
</div> |
|
|
127 |
|
|
|
128 |
<div class="bd"> |
|
|
129 |
<ul class="toc"> |
|
|
130 |
<li> |
|
|
131 |
<a href="#mouseover-and-mouseout-are-noisy"><code>mouseover</code> and <code>mouseout</code> are noisy</a> |
|
|
132 |
</li> |
|
|
133 |
<li> |
|
|
134 |
<a href="#hover"><code>mouseenter</code> + <code>mouseleave</code> == <code>hover</code></a> |
|
|
135 |
</li> |
|
|
136 |
</ul> |
|
|
137 |
</div> |
|
|
138 |
</div> |
|
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
<div class="sidebox"> |
|
|
143 |
<div class="hd"> |
|
|
144 |
<h2 class="no-toc">Examples</h2> |
|
|
145 |
</div> |
|
|
146 |
|
|
|
147 |
<div class="bd"> |
|
|
148 |
<ul class="examples"> |
|
|
149 |
|
|
|
150 |
|
|
|
151 |
<li data-description="Use the Event Utility to attach simple DOM event handlers."> |
|
|
152 |
<a href="basic-example.html">Simple DOM Events</a> |
|
|
153 |
</li> |
|
|
154 |
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
<li data-description="Using the synthetic event API to create a DOM event that fires in response to arrow keys being pressed."> |
|
|
158 |
<a href="synth-example.html">Creating an Arrow Event for DOM Subscription</a> |
|
|
159 |
</li> |
|
|
160 |
|
|
|
161 |
|
|
|
162 |
|
|
|
163 |
<li data-description="Supporting cross-device swipe gestures, using the event-move gesture events"> |
|
|
164 |
<a href="swipe-example.html">Supporting A Swipe Left Gesture</a> |
|
|
165 |
</li> |
|
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
|
169 |
|
|
|
170 |
|
|
|
171 |
|
|
|
172 |
|
|
|
173 |
|
|
|
174 |
|
|
|
175 |
|
|
|
176 |
|
|
|
177 |
|
|
|
178 |
</ul> |
|
|
179 |
</div> |
|
|
180 |
</div> |
|
|
181 |
|
|
|
182 |
|
|
|
183 |
|
|
|
184 |
<div class="sidebox"> |
|
|
185 |
<div class="hd"> |
|
|
186 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
187 |
</div> |
|
|
188 |
|
|
|
189 |
<div class="bd"> |
|
|
190 |
<ul class="examples"> |
|
|
191 |
|
|
|
192 |
|
|
|
193 |
|
|
|
194 |
|
|
|
195 |
|
|
|
196 |
|
|
|
197 |
|
|
|
198 |
|
|
|
199 |
<li data-description="Creating an accessible menu button using the Focus Manager Node Plugin, Event's delegation support and mouseenter event, along with the Overlay widget and Node's support for the WAI-ARIA Roles and States."> |
|
|
200 |
<a href="../node-focusmanager/node-focusmanager-button.html">Accessible Menu Button</a> |
|
|
201 |
</li> |
|
|
202 |
|
|
|
203 |
|
|
|
204 |
|
|
|
205 |
<li data-description="Shows how to extend the base widget class, to create your own Widgets."> |
|
|
206 |
<a href="../widget/widget-extend.html">Extending the Base Widget Class</a> |
|
|
207 |
</li> |
|
|
208 |
|
|
|
209 |
|
|
|
210 |
|
|
|
211 |
<li data-description="Example Photo Browser application."> |
|
|
212 |
<a href="../dd/photo-browser.html">Photo Browser</a> |
|
|
213 |
</li> |
|
|
214 |
|
|
|
215 |
|
|
|
216 |
|
|
|
217 |
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation."> |
|
|
218 |
<a href="../dd/portal-drag.html">Portal Style Example</a> |
|
|
219 |
</li> |
|
|
220 |
|
|
|
221 |
|
|
|
222 |
|
|
|
223 |
<li data-description="Use IO to request data over HTTP."> |
|
|
224 |
<a href="../io/get.html">HTTP GET to request data</a> |
|
|
225 |
</li> |
|
|
226 |
|
|
|
227 |
|
|
|
228 |
</ul> |
|
|
229 |
</div> |
|
|
230 |
</div> |
|
|
231 |
|
|
|
232 |
</div> |
|
|
233 |
</div> |
|
|
234 |
</div> |
|
|
235 |
</div> |
|
|
236 |
|
|
|
237 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
238 |
<script>prettyPrint();</script> |
|
|
239 |
|
|
|
240 |
<script> |
|
|
241 |
YUI.Env.Tests = { |
|
|
242 |
examples: [], |
|
|
243 |
project: '../assets', |
|
|
244 |
assets: '../assets/event', |
|
|
245 |
name: 'event', |
|
|
246 |
title: 'The mouseenter, mouseleave, and hover Events', |
|
|
247 |
newWindow: '', |
|
|
248 |
auto: false |
|
|
249 |
}; |
|
|
250 |
YUI.Env.Tests.examples.push('basic-example'); |
|
|
251 |
YUI.Env.Tests.examples.push('synth-example'); |
|
|
252 |
YUI.Env.Tests.examples.push('swipe-example'); |
|
|
253 |
YUI.Env.Tests.examples.push('node-focusmanager-button'); |
|
|
254 |
YUI.Env.Tests.examples.push('widget-extend'); |
|
|
255 |
YUI.Env.Tests.examples.push('photo-browser'); |
|
|
256 |
YUI.Env.Tests.examples.push('portal-drag'); |
|
|
257 |
YUI.Env.Tests.examples.push('get'); |
|
|
258 |
|
|
|
259 |
</script> |
|
|
260 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
261 |
|
|
|
262 |
|
|
|
263 |
|
|
|
264 |
</body> |
|
|
265 |
</html> |