|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Basic Sliders</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 |
|
|
|
24 |
<h1>Example: Basic Sliders</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><style scoped> |
|
|
29 |
#demo input { |
|
|
30 |
width: 2em; |
|
|
31 |
} |
|
|
32 |
.horiz_slider { |
|
|
33 |
margin-left: 1ex; |
|
|
34 |
} |
|
|
35 |
.vert_slider { |
|
|
36 |
margin-bottom: 1em; |
|
|
37 |
} |
|
|
38 |
</style> |
|
|
39 |
|
|
|
40 |
<div class="intro"> |
|
|
41 |
<p>This example walks you through the basics of creating a Slider from script. |
|
|
42 |
Both Sliders in this example link to text input fields that expect numeric input. The first Slider uses the default configuration, spanning values between 0 and 100, and is rendered inline.</p> |
|
|
43 |
|
|
|
44 |
<p>The second Slider is configured to orient vertically (along the y axis) and the configuration includes minimum, maximium, and initial values. It is rendered into a <code></div></code>.</p> |
|
|
45 |
|
|
|
46 |
<p>The first Slider is set up in a more traditional JavaScript coding style and |
|
|
47 |
the second using the shorter, method chaining style.</p> |
|
|
48 |
</div> |
|
|
49 |
|
|
|
50 |
<div id="demo" class="example yui3-skin-sam"> <!-- You need this skin class --> |
|
|
51 |
|
|
|
52 |
<h4>Horizontal Slider</h4> |
|
|
53 |
<p> |
|
|
54 |
<label for="horiz_value">Value: </label> |
|
|
55 |
<input id="horiz_value" value="0"> |
|
|
56 |
<span class="horiz_slider"></span> |
|
|
57 |
</p> |
|
|
58 |
|
|
|
59 |
<h4>Vertical Slider</h4> |
|
|
60 |
<p><label for="vert_value">Value: </label><input id="vert_value" value="30"></p> |
|
|
61 |
<div class="vert_slider"></div> |
|
|
62 |
|
|
|
63 |
</div> |
|
|
64 |
|
|
|
65 |
<script> |
|
|
66 |
// Create a YUI instance and request the slider module and its dependencies |
|
|
67 |
YUI().use("slider", function (Y) { |
|
|
68 |
|
|
|
69 |
var xInput, // input tied to xSlider |
|
|
70 |
yInput, // input tied to ySlider |
|
|
71 |
xSlider; // horizontal Slider |
|
|
72 |
|
|
|
73 |
// Function to pass input value back to the Slider |
|
|
74 |
function updateSlider( e ) { |
|
|
75 |
var data = this.getData(), |
|
|
76 |
slider = data.slider, |
|
|
77 |
value = parseInt( this.get( "value" ), 10 ); |
|
|
78 |
|
|
|
79 |
if ( data.wait ) { |
|
|
80 |
data.wait.cancel(); |
|
|
81 |
} |
|
|
82 |
|
|
|
83 |
// Update the Slider on a delay to allow time for typing |
|
|
84 |
data.wait = Y.later( 200, slider, function () { |
|
|
85 |
data.wait = null; |
|
|
86 |
this.set( "value", value ); |
|
|
87 |
} ); |
|
|
88 |
} |
|
|
89 |
|
|
|
90 |
// Function to update the input value from the Slider value |
|
|
91 |
function updateInput( e ) { |
|
|
92 |
this.set( "value", e.newVal ); |
|
|
93 |
} |
|
|
94 |
|
|
|
95 |
|
|
|
96 |
|
|
|
97 |
// Create a horizontal Slider using all defaults |
|
|
98 |
xSlider = new Y.Slider(); |
|
|
99 |
|
|
|
100 |
// Link the input value to the Slider |
|
|
101 |
xInput = Y.one( "#horiz_value" ); |
|
|
102 |
xInput.setData( { slider: xSlider } ); |
|
|
103 |
|
|
|
104 |
// Pass the input as the 'this' object inside updateInput |
|
|
105 |
xSlider.after( "valueChange", updateInput, xInput ); |
|
|
106 |
xInput.on( "keyup", updateSlider ); |
|
|
107 |
|
|
|
108 |
// Render the Slider next to the input |
|
|
109 |
xSlider.render('.horiz_slider') |
|
|
110 |
|
|
|
111 |
|
|
|
112 |
// Create the vertical Slider. |
|
|
113 |
yInput = Y.one( "#vert_value" ); |
|
|
114 |
yInput.setData( "slider", new Y.Slider({ |
|
|
115 |
axis: 'y', |
|
|
116 |
min : 100, // min is the value at the top |
|
|
117 |
max : -100, // max is the value at the bottom |
|
|
118 |
value : 30, // initial value |
|
|
119 |
length: '201px', // rail extended to afford all values |
|
|
120 |
|
|
|
121 |
// construction-time event subscription |
|
|
122 |
after : { |
|
|
123 |
valueChange: Y.bind( updateInput, yInput ) |
|
|
124 |
} |
|
|
125 |
}).render( ".vert_slider" ) // render returns the Slider |
|
|
126 |
) // set( "data", ... ) returns the Node |
|
|
127 |
.on( "keyup", updateSlider ); // chain the keyup subscription |
|
|
128 |
|
|
|
129 |
}); |
|
|
130 |
|
|
|
131 |
</script> |
|
|
132 |
|
|
|
133 |
<h3>Horizontal Slider with default configuration</h3> |
|
|
134 |
<p>Sliders are horizontal by default, with available values ranging from 0 to 100, and an initial value of 0. The rail is 150 pixels long plus a few pixels for the rail's end caps.</p> |
|
|
135 |
|
|
|
136 |
<pre class="code prettyprint">var xSlider = new Y.Slider(); |
|
|
137 |
|
|
|
138 |
// render into the <span> next to the input |
|
|
139 |
xSlider.render( ".horiz_slider" );</pre> |
|
|
140 |
|
|
|
141 |
|
|
|
142 |
<h3>Linking a Slider with a text input</h3> |
|
|
143 |
<p>To keep the Slider's value and input value in sync, you need to subscribe to events on both the input and the Slider. For Slider-to-input, the interesting event is <code>valueChange</code>.</p> |
|
|
144 |
|
|
|
145 |
<pre class="code prettyprint">// Function to update the input value from the Slider value |
|
|
146 |
function updateInput( e ) { |
|
|
147 |
this.set( "value", e.newVal ); |
|
|
148 |
} |
|
|
149 |
|
|
|
150 |
var xInput = Y.one( "#horiz_value" ); |
|
|
151 |
// Subscribe to the Slider's valueChange event, passing the input as the 'this' |
|
|
152 |
xSlider.on( "valueChange", updateInput, xInput );</pre> |
|
|
153 |
|
|
|
154 |
|
|
|
155 |
<h3>Linking the input with the Slider</h3> |
|
|
156 |
<p>To feed input changes back to the Slider we'll listen to its <code>keyup</code> event. But we'll put the update on a delay to allow for a user to finish typing.</p> |
|
|
157 |
|
|
|
158 |
<p>Additionally, we'll make the update function generic, since we have two Sliders in this example. We'll leverage the <code>setData</code> and <code>getData</code> methods of Node instances and store a reference to the Slider. Then we can use this object from inside the function to get back to the slider the input is related to.</p> |
|
|
159 |
|
|
|
160 |
<pre class="code prettyprint">// Function to pass input value back to the Slider |
|
|
161 |
function updateSlider( e ) { |
|
|
162 |
var data = this.getData(), |
|
|
163 |
slider = data.slider, |
|
|
164 |
value = parseInt( this.get( "value" ), 10 ); |
|
|
165 |
|
|
|
166 |
if ( data.wait ) { |
|
|
167 |
data.wait.cancel(); |
|
|
168 |
} |
|
|
169 |
|
|
|
170 |
// Update the Slider on a delay to allow time for typing |
|
|
171 |
data.wait = Y.later( 200, slider, function () { |
|
|
172 |
data.wait = null; |
|
|
173 |
this.set( "value", value ); |
|
|
174 |
} ); |
|
|
175 |
} |
|
|
176 |
|
|
|
177 |
xInput.setData( { slider: xSlider } ); |
|
|
178 |
xInput.on( "keyup", updateSlider );</pre> |
|
|
179 |
|
|
|
180 |
|
|
|
181 |
<h3>Creating the vertical Slider</h3> |
|
|
182 |
<p>To create a vertical Slider you just need to set the <code>axis</code> attribute to "y".</p> |
|
|
183 |
|
|
|
184 |
<p>For this Slider, we'll use more extensive method chaining and include value and rail configurations. First we'll change the value range from 0 - 100 to -100 - +100 and set an initial value of +30.</p> |
|
|
185 |
|
|
|
186 |
<p>Note that the <code>min</code> configuration is 100 in this case because the top is associated with the minimum value. Slider has no qualms about <code>min</code> being greater than <code>max</code>.</p> |
|
|
187 |
|
|
|
188 |
<p>The rail length is then configured to be 201 pixels, so each value has a distinct pixel position on the rail (don't forget 0).</p> |
|
|
189 |
|
|
|
190 |
<p>Finally, the <code>valueChange</code> subscription is included in the configuration as well.</p> |
|
|
191 |
|
|
|
192 |
<pre class="code prettyprint">var yInput = Y.one( "#vert_value" ); |
|
|
193 |
yInput.setData( "slider", new Y.Slider({ |
|
|
194 |
axis: 'y', |
|
|
195 |
min : 100, // min is the value at the top |
|
|
196 |
max : -100, // max is the value at the bottom |
|
|
197 |
value : 30, // initial value |
|
|
198 |
length: '201px', // rail extended to afford all values |
|
|
199 |
|
|
|
200 |
// construction-time event subscription |
|
|
201 |
after : { |
|
|
202 |
valueChange: Y.bind( updateInput, yInput ) |
|
|
203 |
} |
|
|
204 |
}).render( ".vert_slider" ) // render returns the Slider |
|
|
205 |
) // set( "data", ... ) returns the Node |
|
|
206 |
.on( "keyup", updateSlider ); // chain the keyup subscription</pre> |
|
|
207 |
|
|
|
208 |
|
|
|
209 |
<h3>Full Code Listing</h3> |
|
|
210 |
|
|
|
211 |
<h4>HTML</h4> |
|
|
212 |
|
|
|
213 |
<p> |
|
|
214 |
<strong>Note:</strong> be sure to add the <code>yui3-skin-sam</code> classname to the |
|
|
215 |
page's <code><body></code> element or to a parent element of the widget in order to apply |
|
|
216 |
the default CSS skin. See <a href="http://yuilibrary.com/yui/docs/tutorials/skins/">Understanding Skinning</a>. |
|
|
217 |
</p> |
|
|
218 |
|
|
|
219 |
|
|
|
220 |
<pre class="code prettyprint"><div id="demo" class="example yui3-skin-sam"> <!-- You need this skin class --> |
|
|
221 |
|
|
|
222 |
<h4>Horizontal Slider</h4> |
|
|
223 |
<p> |
|
|
224 |
<label for="horiz_value">Value: </label> |
|
|
225 |
<input id="horiz_value" value="0"> |
|
|
226 |
<span class="horiz_slider"></span> |
|
|
227 |
</p> |
|
|
228 |
|
|
|
229 |
<h4>Vertical Slider</h4> |
|
|
230 |
<p><label for="vert_value">Value: </label><input id="vert_value" value="30"></p> |
|
|
231 |
<div class="vert_slider"></div> |
|
|
232 |
|
|
|
233 |
</div></pre> |
|
|
234 |
|
|
|
235 |
|
|
|
236 |
<h4>JavaScript</h4> |
|
|
237 |
<pre class="code prettyprint">// Create a YUI instance and request the slider module and its dependencies |
|
|
238 |
YUI().use("slider", function (Y) { |
|
|
239 |
|
|
|
240 |
var xInput, // input tied to xSlider |
|
|
241 |
yInput, // input tied to ySlider |
|
|
242 |
xSlider; // horizontal Slider |
|
|
243 |
|
|
|
244 |
// Function to pass input value back to the Slider |
|
|
245 |
function updateSlider( e ) { |
|
|
246 |
var data = this.getData(), |
|
|
247 |
slider = data.slider, |
|
|
248 |
value = parseInt( this.get( "value" ), 10 ); |
|
|
249 |
|
|
|
250 |
if ( data.wait ) { |
|
|
251 |
data.wait.cancel(); |
|
|
252 |
} |
|
|
253 |
|
|
|
254 |
// Update the Slider on a delay to allow time for typing |
|
|
255 |
data.wait = Y.later( 200, slider, function () { |
|
|
256 |
data.wait = null; |
|
|
257 |
this.set( "value", value ); |
|
|
258 |
} ); |
|
|
259 |
} |
|
|
260 |
|
|
|
261 |
// Function to update the input value from the Slider value |
|
|
262 |
function updateInput( e ) { |
|
|
263 |
this.set( "value", e.newVal ); |
|
|
264 |
} |
|
|
265 |
|
|
|
266 |
|
|
|
267 |
|
|
|
268 |
// Create a horizontal Slider using all defaults |
|
|
269 |
xSlider = new Y.Slider(); |
|
|
270 |
|
|
|
271 |
// Link the input value to the Slider |
|
|
272 |
xInput = Y.one( "#horiz_value" ); |
|
|
273 |
xInput.setData( { slider: xSlider } ); |
|
|
274 |
|
|
|
275 |
// Pass the input as the 'this' object inside updateInput |
|
|
276 |
xSlider.after( "valueChange", updateInput, xInput ); |
|
|
277 |
xInput.on( "keyup", updateSlider ); |
|
|
278 |
|
|
|
279 |
// Render the Slider next to the input |
|
|
280 |
xSlider.render('.horiz_slider') |
|
|
281 |
|
|
|
282 |
|
|
|
283 |
// Create the vertical Slider. |
|
|
284 |
yInput = Y.one( "#vert_value" ); |
|
|
285 |
yInput.setData( "slider", new Y.Slider({ |
|
|
286 |
axis: 'y', |
|
|
287 |
min : 100, // min is the value at the top |
|
|
288 |
max : -100, // max is the value at the bottom |
|
|
289 |
value : 30, // initial value |
|
|
290 |
length: '201px', // rail extended to afford all values |
|
|
291 |
|
|
|
292 |
// construction-time event subscription |
|
|
293 |
after : { |
|
|
294 |
valueChange: Y.bind( updateInput, yInput ) |
|
|
295 |
} |
|
|
296 |
}).render( ".vert_slider" ) // render returns the Slider |
|
|
297 |
) // set( "data", ... ) returns the Node |
|
|
298 |
.on( "keyup", updateSlider ); // chain the keyup subscription |
|
|
299 |
|
|
|
300 |
});</pre> |
|
|
301 |
|
|
|
302 |
</div> |
|
|
303 |
</div> |
|
|
304 |
</div> |
|
|
305 |
|
|
|
306 |
<div class="yui3-u-1-4"> |
|
|
307 |
<div class="sidebar"> |
|
|
308 |
|
|
|
309 |
|
|
|
310 |
|
|
|
311 |
<div class="sidebox"> |
|
|
312 |
<div class="hd"> |
|
|
313 |
<h2 class="no-toc">Examples</h2> |
|
|
314 |
</div> |
|
|
315 |
|
|
|
316 |
<div class="bd"> |
|
|
317 |
<ul class="examples"> |
|
|
318 |
|
|
|
319 |
|
|
|
320 |
<li data-description="The basics of setting up a horizontal and vertical Slider"> |
|
|
321 |
<a href="slider-basic.html">Basic Sliders</a> |
|
|
322 |
</li> |
|
|
323 |
|
|
|
324 |
|
|
|
325 |
|
|
|
326 |
<li data-description="Creating a vertical Slider from existing markup"> |
|
|
327 |
<a href="slider-from-markup.html">Creating a Slider from Existing Markup</a> |
|
|
328 |
</li> |
|
|
329 |
|
|
|
330 |
|
|
|
331 |
|
|
|
332 |
<li data-description="Specifying an alternate skin for a Slider instance"> |
|
|
333 |
<a href="slider-skin.html">Alternate Skins</a> |
|
|
334 |
</li> |
|
|
335 |
|
|
|
336 |
|
|
|
337 |
|
|
|
338 |
|
|
|
339 |
|
|
|
340 |
|
|
|
341 |
|
|
|
342 |
|
|
|
343 |
|
|
|
344 |
|
|
|
345 |
|
|
|
346 |
|
|
|
347 |
|
|
|
348 |
|
|
|
349 |
</ul> |
|
|
350 |
</div> |
|
|
351 |
</div> |
|
|
352 |
|
|
|
353 |
|
|
|
354 |
|
|
|
355 |
<div class="sidebox"> |
|
|
356 |
<div class="hd"> |
|
|
357 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
358 |
</div> |
|
|
359 |
|
|
|
360 |
<div class="bd"> |
|
|
361 |
<ul class="examples"> |
|
|
362 |
|
|
|
363 |
|
|
|
364 |
|
|
|
365 |
|
|
|
366 |
|
|
|
367 |
|
|
|
368 |
|
|
|
369 |
|
|
|
370 |
<li data-description="Use three sliders to control RGB values and update Hex and HSL strings."> |
|
|
371 |
<a href="../color/rgb-slider.html">RGB Slider</a> |
|
|
372 |
</li> |
|
|
373 |
|
|
|
374 |
|
|
|
375 |
|
|
|
376 |
<li data-description="Use the HSL color picker to select a new color. Then chose the color type you like best."> |
|
|
377 |
<a href="../color/hsl-picker.html">HSL Color Picker</a> |
|
|
378 |
</li> |
|
|
379 |
|
|
|
380 |
|
|
|
381 |
|
|
|
382 |
<li data-description="Use the HSL color picker to create harmony colors."> |
|
|
383 |
<a href="../color/hsl-harmony.html">HSL Harmony</a> |
|
|
384 |
</li> |
|
|
385 |
|
|
|
386 |
|
|
|
387 |
|
|
|
388 |
<li data-description="Shows how to use Overlay's constrainment support, to limit the XY value which can be set for an Overlay."> |
|
|
389 |
<a href="../overlay/overlay-constrain.html">Constrain Support</a> |
|
|
390 |
</li> |
|
|
391 |
|
|
|
392 |
|
|
|
393 |
|
|
|
394 |
<li data-description="Use StyleSheet to adjust the CSS rules applying a page theme from user input"> |
|
|
395 |
<a href="../stylesheet/stylesheet-theme.html">Adjusting a Page Theme on the Fly</a> |
|
|
396 |
</li> |
|
|
397 |
|
|
|
398 |
|
|
|
399 |
|
|
|
400 |
<li data-description="Example Photo Browser application."> |
|
|
401 |
<a href="../dd/photo-browser.html">Photo Browser</a> |
|
|
402 |
</li> |
|
|
403 |
|
|
|
404 |
|
|
|
405 |
</ul> |
|
|
406 |
</div> |
|
|
407 |
</div> |
|
|
408 |
|
|
|
409 |
</div> |
|
|
410 |
</div> |
|
|
411 |
</div> |
|
|
412 |
</div> |
|
|
413 |
|
|
|
414 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
415 |
<script>prettyPrint();</script> |
|
|
416 |
|
|
|
417 |
<script> |
|
|
418 |
YUI.Env.Tests = { |
|
|
419 |
examples: [], |
|
|
420 |
project: '../assets', |
|
|
421 |
assets: '../assets/slider', |
|
|
422 |
name: 'slider-basic', |
|
|
423 |
title: 'Basic Sliders', |
|
|
424 |
newWindow: '', |
|
|
425 |
auto: false |
|
|
426 |
}; |
|
|
427 |
YUI.Env.Tests.examples.push('slider-basic'); |
|
|
428 |
YUI.Env.Tests.examples.push('slider-from-markup'); |
|
|
429 |
YUI.Env.Tests.examples.push('slider-skin'); |
|
|
430 |
YUI.Env.Tests.examples.push('rgb-slider'); |
|
|
431 |
YUI.Env.Tests.examples.push('hsl-picker'); |
|
|
432 |
YUI.Env.Tests.examples.push('hsl-harmony'); |
|
|
433 |
YUI.Env.Tests.examples.push('overlay-constrain'); |
|
|
434 |
YUI.Env.Tests.examples.push('stylesheet-theme'); |
|
|
435 |
YUI.Env.Tests.examples.push('photo-browser'); |
|
|
436 |
|
|
|
437 |
</script> |
|
|
438 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
439 |
|
|
|
440 |
|
|
|
441 |
|
|
|
442 |
</body> |
|
|
443 |
</html> |