<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Enrique Delgado Monroy: Tag css</title>
    <link>http://www.enriquedelgado.com/articles/tag/css</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Work, Life, Stuff</description>
    <item>
      <title>Slider Controls with Prototype and Scriptaculous</title>
      <description>&lt;div style="float:right;"&gt;&lt;a href="/files/Slider.zip"&gt;&lt;img src="/files/slider.gif" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;Even though web-based applications have the majority of input elements found in traditional desktop-based ones, once in a while you are going to wish you had input elements that for some reason did not make it to the &lt;span class="caps"&gt;HTML&lt;/span&gt; standards.

	&lt;p&gt;One of the input elements I wish were supported by default are draggable slider controls. There is something cool about having a slider to quickly change a setting of a search or a calculation, instead of having to type values on a text box or clicking on drop-down boxes.&lt;/p&gt;


	&lt;p&gt;Out of the myriad of options I found during a quick Google search, I found these two options to be the most attractive:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://developer.yahoo.com/yui/examples/slider/slider-ticks.html"&gt;Yahoo UI Slider Control&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://wiki.script.aculo.us/scriptaculous/show/SliderDemo"&gt;Scriptaculous Slider Control&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Since I was already using Scriptaculous on the application in question, I decided to stick to that solution. Yahoo&amp;#8217;s version is definitely worth a look though. At first glance, Yahoo&amp;#8217;s example looks better than the Scriptaculous one, but as you will see we can get the best of both worlds.&lt;/p&gt;


	&lt;p&gt;One word of warning; even though sliders are a useful control, you should exercise discretion as to not over use them. The truth of the matter is that not everyone is familiar using sliders on the web, and it can quickly become a usability problem if not careful. Good uses for sliders are things like mortgage calculators, search result filter controls, and things of that sort. Do not replace menus or tabs with a slider ;)&lt;/p&gt;


	&lt;p&gt;Onto the interesting part. In order to use Scriptculous&amp;#8217; sliders, you will need to make sure you are including the library with the slider component. By default, everything is included when you include scriptaculous in the traditional way:&lt;/p&gt;


&lt;pre style="overflow: auto; background: #EEE;"&gt;
&amp;lt;script src="prototype.js" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src="scriptaculous.js" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;This will load about six components, but it is a good practice to load only those components that you are actually using. In my case, I&amp;#8217;ll be using effects and slider so I&amp;#8217;m calling Scriptaculous in this fashion:&lt;/p&gt;


&lt;pre style="overflow: auto; background: #EEE;"&gt;
 &amp;lt;script src="prototype.js" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;
 &amp;lt;script src="scriptaculous.js?load=effects,slider" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/pre&gt;

A Scriptaculous slider consist of two things:
	&lt;ul&gt;
	&lt;li&gt;A &amp;#8220;parent&amp;#8221; element (typically a &lt;tt&gt;&lt;span class="caps"&gt;DIV&lt;/span&gt;&lt;/tt&gt;) deemed to be the &amp;#8220;track&amp;#8221; on which a knob will slide.&lt;/li&gt;
		&lt;li&gt;A &amp;#8220;child&amp;#8221; nested element (typically a &lt;tt&gt;&lt;span class="caps"&gt;DIV&lt;/span&gt;&lt;/tt&gt;) that will act as the element to be slided across the length (or height) of the track.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;You can create a slider track and controls purely based on &lt;span class="caps"&gt;CSS&lt;/span&gt; code like in the Scriptaculous sample, but I chose to use images within &lt;tt&gt;&lt;span class="caps"&gt;DIV&lt;/span&gt;&lt;/tt&gt; elements to obtain the look of the Yahoo UI slider control. A possible &lt;span class="caps"&gt;HTML&lt;/span&gt; mark-up would be:&lt;/p&gt;


&lt;pre style="overflow: auto; background: #EEE;"&gt;
 &amp;lt;div id="track1" style="background-image:url(bg-fader.gif); background-repeat:no-repeat; width:209px; height:28px;"&amp;gt;
  &amp;lt;div id="handle1" style="background-image:url(thumb-n.gif); background-repeat:no-repeat; width:17px; height:21px; cursor:move;"&amp;gt; &amp;lt;/div&amp;gt;
 &amp;lt;/div&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;In the example above, we have a parent &lt;tt&gt;&lt;span class="caps"&gt;DIV&lt;/span&gt;&lt;/tt&gt; element (the track) with an ID of &lt;tt&gt;track1&lt;/tt&gt; and a child (the handle) element with an ID of &lt;tt&gt;handle1&lt;/tt&gt;. In this case the width and height of the &lt;tt&gt;&lt;span class="caps"&gt;DIV&lt;/span&gt;&lt;/tt&gt; elements match the track and handle image dimensions.&lt;/p&gt;


	&lt;p&gt;Here is where the magic comes. Lets make a &lt;tt&gt;Slider&lt;/tt&gt; object to give life to our &lt;span class="caps"&gt;HTML&lt;/span&gt; slider:&lt;/p&gt;


&lt;pre style="overflow: auto; background: #EEE;"&gt;
 &amp;lt;script type="text/javascript" language="javascript"&amp;gt;
  // &amp;lt;![CDATA[
  window.onload = function() {
    new Control.Slider('handle1',
                       'track1',                                  
                       {axis:'horizontal',
                        range: $R(1,5)
                       });
  }
  // ]]&amp;gt;
 &amp;lt;/script&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;In the example above, we are creating a horizontal slider with a handle element ID of &lt;tt&gt;handle1&lt;/tt&gt; and a track element ID of &lt;tt&gt;track1&lt;/tt&gt;. The slider possible values range from 1 to 5.&lt;/p&gt;


	&lt;p&gt;You may have noticed that the code is within the document&amp;#8217;s &lt;tt&gt;onLoad&lt;/tt&gt; event. This is because the slider object needs to be created after the track and slider elements have completely loaded. Alternatively, you could place the JavaScript code somewhere after the &lt;span class="caps"&gt;HTML&lt;/span&gt; elements in your document, but it is typically not a good idea to place JavaScript anywhere other than in its own &lt;tt&gt;.js&lt;/tt&gt; file and then calling that file on your document. See my other post about &lt;a href="http://enriquedelgado.com/articles/2007/05/03/attaching-functions-to-events-in-javascript-the-unobstrusive-way"&gt;unobstrusive JavaScript&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;There are several options you can play with to create many variations of Scriptaculous sliders. For a good reference, check out the &lt;a href="http://wiki.script.aculo.us/scriptaculous/show/Slider"&gt;slider documentation page&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Download a complete working demo &lt;a href="/files/Slider.zip"&gt;here&lt;/a&gt;. Just unzip the file and open the &lt;tt&gt;index.html&lt;/tt&gt; file with your browser. You should see some sliders with different attributes and further examples as to how to get useful data from the sliders :)&lt;/p&gt;</description>
      <pubDate>Tue, 11 Dec 2007 07:00:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:3ee3ab69-fd9b-4abc-8dfe-1698f9b3a7b1</guid>
      <author>Enrique Delgado</author>
      <link>http://www.enriquedelgado.com/articles/2007/12/11/html-slider-controls-with-prototype-and-scriptaculous</link>
      <category>Web Development</category>
      <category>html</category>
      <category>slider</category>
      <category>scriptaculous</category>
      <category>prototype</category>
      <category>DOM</category>
      <category>css</category>
      <enclosure type="application/zip" url="http://www.enriquedelgado.com/files/Slider.zip" length="284287"/>
    </item>
  </channel>
</rss>
