Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<link href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  
<p>
  <input type="text" id="amount" readonly>
</p>
 
<div id="slider"></div>
</body>
</html>
 
body {
  font-family: sans-serif;
  font-size: 12px;
}
#amount {
  font-size: 22px;
  font-weight: bold;
  border:none;
  color: #ff5555;
  background: none;
}
 
$(function() {
    var values = [0, 50, 90, 330, 500];
    var slider = $("#slider").slider({
        orientation: 'horizontal',
        min: values[0],
        max: values[values.length - 1],
        value: values[0],
        slide: function(event, ui) {
            var includeLeft = event.keyCode != $.ui.keyCode.RIGHT;
            var includeRight = event.keyCode != $.ui.keyCode.LEFT;
            var value = findNearest(includeLeft, includeRight, ui.value);
            slider.slider('value', value);
          
            $("#amount").val('$' + slider.slider('value'));                      
            return false;
        }
    });
    
    function findNearest(includeLeft, includeRight, value) {
        var nearest = null;
        var diff = null;
        for (var i = 0; i < values.length; i++) {
            if ((includeLeft && values[i] <= value) || (includeRight && values[i] >= value)) {
                var newDiff = Math.abs(value - values[i]);
                if (diff == null || newDiff < diff) {
                    nearest = values[i];
                    diff = newDiff;
                }
            }
        }
        return nearest;
    }
  
$("#amount").val('$' + slider.slider('value'));
});
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers