jQuery UI 滑块对数刻度
我正在使用以下 jQuery UI 代码作为对数滑块:
var minVal = 10;
var maxVal = 100;
$("#slider").slider({
range: true,
min: minVal,
max: maxVal / 2,
values: [minVal, maxVal],
slide: function(event, ui) {
$("#amount_min").val(Number(expon(ui.values[0], minVal, maxVal)).toFixed(0));
$("#amount_max").val(Number(expon(ui.values[1], minVal, maxVal)).toFixed(0));
}
});
expon
函数是:
function expon(val, min,max) {
var minv = Math.log(min);
var maxv = Math.log(max);
max = max / 2;
// calculate adjustment factor
var scale = (maxv - minv) / (max - min);
return Math.exp(minv + scale * (val - min));
}
#amount_min
和 #amount_max
是 HTML 输入元素。上面的代码可以很好地从滑块获取值并将它们放入输入元素。
但现在我需要与 expon() 相反的函数 - 当我更改输入值时更改滑块。有人能帮我解决这个问题吗?
I'm using this jQuery UI code use for a logarithmic slider:
var minVal = 10;
var maxVal = 100;
$("#slider").slider({
range: true,
min: minVal,
max: maxVal / 2,
values: [minVal, maxVal],
slide: function(event, ui) {
$("#amount_min").val(Number(expon(ui.values[0], minVal, maxVal)).toFixed(0));
$("#amount_max").val(Number(expon(ui.values[1], minVal, maxVal)).toFixed(0));
}
});
The expon
function is:
function expon(val, min,max) {
var minv = Math.log(min);
var maxv = Math.log(max);
max = max / 2;
// calculate adjustment factor
var scale = (maxv - minv) / (max - min);
return Math.exp(minv + scale * (val - min));
}
#amount_min
and #amount_max
are HTML input elements. The code above works just fine to get values from the slider and to put them to the input elements.
But now I need the function opposite to expon()
— to change the slider when I change the values of the inputs. Can anybody help me with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在这里找到了解决方案: 对数滑块
我在一个项目中使用了您非常有用的代码...这是我所拥有的:
然后我使用此代码更改滑块位置:
我希望这对您有用:)
I found the solution here: Logarithmic slider
I've used your very helpful code for a project... this is what I have:
I then change my slider positions using this code:
I hope that works for you :)
这就是我所做的:
This is what i did: