Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could do it using jquery's slider by hooking into the slide event (effectively overriding it)... Something like this:</p> <pre><code>$(function() { var values = [0, 10, 50, 60, 70, 80, 90, 91, 92, 93, 94, 95, 100]; var slider = $("#slider").slider({ slide: function(event, ui) { var includeLeft = event.keyCode != $.ui.keyCode.RIGHT; var includeRight = event.keyCode != $.ui.keyCode.LEFT; slider.slider('option', 'value', findNearest(includeLeft, includeRight, ui.value)); return false; } }); function findNearest(includeLeft, includeRight, value) { var nearest = null; var diff = null; for (var i = 0; i &lt; values.length; i++) { if ((includeLeft &amp;&amp; values[i] &lt;= value) || (includeRight &amp;&amp; values[i] &gt;= value)) { var newDiff = Math.abs(value - values[i]); if (diff == null || newDiff &lt; diff) { nearest = values[i]; diff = newDiff; } } } return nearest; } }); </code></pre> <p>Should work for what you describe... I've tested it for dragging with the mouse &amp; using left/right/home/end keys (obviously you'd need to change the left/right to up/down if you want a vertical slider).</p> <p>Some notes:</p> <ul> <li>Obviously the values array is whatever steps you want for your purposes.</li> <li>Also obviously, the above code assumes a div with an id of "slider" to work.</li> <li>It will probably work strangely if your min/max values are not the same as your min/max values for the slider (I would suggest just using "min: values[0], max: values[values.length - 1]" as your min/max options on the slider &amp; then you should always be safe).</li> <li>Obviously this option won't currently degrade to a drop down list, but it would be very easy to do... simply render your choices as a normal drop down list (the default state incase of no javascript) &amp; then use jquery to hide the drop down list and also create your values array based on the options. Then you could just update the (hidden) drop down list at the same time as you update the slider in the code above, so that when your form posts, the correct value will be selected in the drop down list.</li> </ul> <p>Hope that helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload