slider-from-markup.mustache revision a1e26dba97702175f8ab0531c41214d8f4704e0c
e2ef43621c3d11c1560031dfd9409bf779e6fda1Adam Moore{{>slider-from-markup-css}}
e2ef43621c3d11c1560031dfd9409bf779e6fda1Adam Moore<div class="intro">
e2ef43621c3d11c1560031dfd9409bf779e6fda1Adam Moore <p>This example illustrates a few points:</p>
e2ef43621c3d11c1560031dfd9409bf779e6fda1Adam Moore <li>How to create a Slider using existing markup</li>
e2ef43621c3d11c1560031dfd9409bf779e6fda1Adam Moore <li>How to disable a Slider</li>
e2ef43621c3d11c1560031dfd9409bf779e6fda1Adam Moore <li>How to use an image sprite to create a custom Slider skin</li>
e2ef43621c3d11c1560031dfd9409bf779e6fda1Adam Moore <p>The visualization of the Slider is based on the volume control in Mac OS X 10.5, with additional controls included for illustration. <strong>Click on the speaker icon to show the Slider</strong>.</p>
e2ef43621c3d11c1560031dfd9409bf779e6fda1Adam Moore<div class="example">
e2ef43621c3d11c1560031dfd9409bf779e6fda1Adam Moore {{>slider-from-markup-html}}
<p>The <a href="http://en.wikipedia.org/wiki/Progressive_enhancement">Progressive Enhancement</a> strategy recommends that your page not contain markup that will only be useful in cases where JavaScript is available. For this reason, Slider does not include an `HTML_PARSER` to reuse existing markup. However, it is possible to override a couple methods to accomplish the task.</p>
<label for="volume">volume</label><input type="text" size="3" maxlength="3" name="volume" id="volume" value="50">
<button type="button" id="volume_icon" class="level_2" title="Open volume slider"><p>Open</p></button>
<span class="yui3-slider-thumb"><img src="{{componentAssets}}/images/sprite.png" height="384" width="31"></span>
<p>To tell the Slider to use the existing rail and thumb elements, override the `renderRail` and `renderThumb` methods.</p>
var volume = new Y.Slider({
volume.renderRail = function () {
volume.renderThumb = function () {
volume.render( "#volume_slider" );
<p>By default, we want the Slider to be hidden until the user clicks on the speaker icon. However, we want to support muting or changing the value of the Slider while it is hidden.</p>
var control = Y.one('#volume_control'),
icon = Y.one('#volume_icon'),
control.toggleClass('volume-hide');
icon.on('click', showHideSlider);
Y.one( 'doc' ).on('click', handleDocumentClick );
<p>We want to disable the Slider and input and set the value to 0 if a user checks the mute checkbox. The value should be returned to the last assigned value when unmuted. To disable the Slider, set its `disabled` attribute to `true`.</p>
var volInput = Y.one('#volume'),
mute = Y.one('#mute'),
var disabled = !volume.get('disabled');
volume.set('disabled', disabled);
beforeMute = volume.getValue();
volume.setValue(0);
volInput.set('disabled','disabled');
volume.setValue(beforeMute);
volInput.set('disabled','');
mute.on('click', muteVolume);
<img id="demo_sprite" src="{{componentAssets}}/images/sprite.png" height="384" width="31" alt="Sprite of all custom image resources for this example">
<p>We'll be using the image sprite to the left to create a custom skin. In this design, to keep things simple, the Slider's container and end caps are all rendered together at the bottom of the sprite.</p>
<p>Slider's thumb range is constrained by the rail element, so it wouldn't be appropriate to use this image as the rail's background—the thumb would slide off the ends. Instead, the rail image is assigned as the background to the Slider's containing element `#volume_slider`. Then the default skin background image is removed on the rail.</p>
background: url("assets/images/sprite.png") no-repeat 0 -259px;
<p>You can see the full CSS and JavaScript for the other controls in the <a href="#full_code_listing">Full Code Listing</a> below.</p>