看这里:
http://docs.jquery.com/UI/API/1.8/Slider
以下代码运行良好,是您原始 jsfiddle 的扩展。我终于明白了你想要什么,现在它似乎工作正常:
实时工作代码示例
JavaScript:
$(document).ready(function () {
'use strict';
var stepSlider = $('#stepSlider');
var stepText = $('#stepText');
var rangeSlider = $('#rangeSlider');
var rangeText = $('#rangeText');
var oldStep;
stepSlider.slider({
min:0,
max:120,
slide:function (event, ui) {
stepText.text(ui.value);
var rsValues = rangeSlider.slider("option", "values");
var stepDiff = ui.value - oldStep;
var rsMax = rangeSlider.slider("option", "max");
var rsMin = rangeSlider.slider("option", "min");
if (stepDiff > 0) {
rsValues[1] += stepDiff;
if (rsValues[1] > rsMax) {
rsValues[0] = rsMax - ui.value;
rsValues[1] = rsMax;
}
} else if (stepDiff < 0) {
rsValues[1] += stepDiff;
if (rsValues[1] < rsMin) {
rsValues[0] = rsMin;
rsValues[1] = ui.value;
}
}
rangeSlider.slider("option", "values", rsValues);
rangeText.text(rsValues[0] + ' - ' + rsValues[1]);
oldStep = ui.value;
}
});
rangeSlider.slider({
min:0,
max:600,
values:[0, 0],
slide:function (event, ui) {
var step = stepSlider.slider("option", "value");
var oldValues = rangeSlider.slider("option", "values");
var rsMax = rangeSlider.slider("option", "max");
var rsMin = rangeSlider.slider("option", "min");
var diff;
if (oldValues[0] != ui.values[0]) {
diff = ui.values[0] - oldValues[0];
if (diff > 0 && diff <= step && ui.values[1] === rsMax) {
return false;
}
ui.values[1] += diff;
if (ui.values[1] > rsMax) {
ui.values[1] = rsMax;
ui.values[0] = ui.values[1] - step;
}
rangeSlider.slider("option", "values", ui.values);
rangeText.text(ui.values[0] + ' - ' + ui.values[1]);
return false;
}
if (oldValues[1] != ui.values[1]) {
diff = ui.values[1] - oldValues[1];
if (diff < 0 && diff >= -step && ui.values[0] === rsMin) {
return false;
}
ui.values[0] += diff;
if (ui.values[0] < rsMin) {
ui.values[0] = rsMin;
ui.values[1] = ui.values[0] + step;
}
rangeSlider.slider("option", "values", ui.values);
rangeText.text(ui.values[0] + ' - ' + ui.values[1]);
return false;
}
rangeText.text(ui.values[0] + ' - ' + ui.values[1]);
}
});
oldStep = stepSlider.slider("option", "value");
});
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Sliders</title>
<link href="rangeSliders.css" rel="stylesheet" type="text/css"/>
<link href="http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
</head>
<body>
<div id="stepSlider">
<div id="stepText">0</div>
</div>
<div id="rangeSlider">
<div id="rangeText">0-600</div>
</div>
<script type="text/javascript" src="rangeSliders.js"></script>
</body>
</html>
CSS:
body {
margin: 50px;
}
#stepSlider, #rangeSlider {
width: 200px;
margin-bottom: 25px;
}
#stepText, #rangeText {
margin: -4px 0 0 225px;
width: 100%;
}