slider-basic.mustache revision 819e90d415ed17d59af3a247b2ad9d6feb0c21b5
68182450ad30d5d75b27bb6ce96aaafe61320d69Ryan Grove<style scoped>
68182450ad30d5d75b27bb6ce96aaafe61320d69Ryan Grove #demo input {
68182450ad30d5d75b27bb6ce96aaafe61320d69Ryan Grove .horiz_slider {
68182450ad30d5d75b27bb6ce96aaafe61320d69Ryan Grove margin-left: 1ex;
68182450ad30d5d75b27bb6ce96aaafe61320d69Ryan Grove .vert_slider {
68182450ad30d5d75b27bb6ce96aaafe61320d69Ryan Grove margin-bottom: 1em;
68182450ad30d5d75b27bb6ce96aaafe61320d69Ryan Grove<div class="intro">
68182450ad30d5d75b27bb6ce96aaafe61320d69Ryan Grove <p>This example walks you through the basics of creating a Slider from script.
68182450ad30d5d75b27bb6ce96aaafe61320d69Ryan Grove 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>
68182450ad30d5d75b27bb6ce96aaafe61320d69Ryan Grove <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 `</div>`.</p>
68182450ad30d5d75b27bb6ce96aaafe61320d69Ryan Grove <p>The first Slider is set up in a more traditional JavaScript coding style and
68182450ad30d5d75b27bb6ce96aaafe61320d69Ryan Grove the second using the shorter, method chaining style.</p>
68182450ad30d5d75b27bb6ce96aaafe61320d69Ryan Grove<div class="example yui3-skin-sam">
68182450ad30d5d75b27bb6ce96aaafe61320d69Ryan Grove {{>slider-basic-source}}
68182450ad30d5d75b27bb6ce96aaafe61320d69Ryan Grove<h3>Horizontal Slider with default configuration</h3>
<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>
var xSlider = new Y.Slider();
xSlider.render( ".horiz_slider" );
<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 `valueChange`.</p>
var xInput = Y.one( "#horiz_value" );
xSlider.on( "valueChange", updateInput, xInput );
<p>To feed input changes back to the Slider we'll listen to its `keyup` event. But we'll put the update on a delay to allow for a user to finish typing.</p>
<p>Additionally, we'll make the update function generic, since we have two Sliders in this example. We'll leverage the `setData` and `getData` 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>
var data = this.getData(),
slider = data.slider,
value = parseInt( this.get( "value" ), 10 );
if ( data.wait ) {
data.wait = null;
this.set( "value", value );
xInput.setData( { slider: xSlider } );
xInput.on( "keyup", updateSlider );
<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>
<p>Note that the `min` configuration is 100 in this case because the top is associated with the minimum value. Slider has no qualms about `min` being greater than `max`.</p>
<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>
var yInput = Y.one( "#vert_value" );
valueChange: Y.bind( updateInput, yInput )
var data = this.getData(),
slider = data.slider,
value = parseInt( this.get( "value" ), 10 );
if ( data.wait ) {
data.wait = null;
this.set( "value", value );
xSlider = new Y.Slider();
xInput = Y.one( "#horiz_value" );
xInput.setData( { slider: xSlider } );
xSlider.after( "valueChange", updateInput, xInput );
xInput.on( "keyup", updateSlider );
xSlider.render('.horiz_slider')
yInput = Y.one( "#vert_value" );
valueChange: Y.bind( updateInput, yInput )