jQuery UI 滑块对数刻度

发布于 2024-12-13 19:12:50 字数 882 浏览 3 评论 0原文

我正在使用以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿哥们儿 2024-12-20 19:12:50

我在这里找到了解决方案: 对数滑块

我在一个项目中使用了您非常有用的代码...这是我所拥有的:

var gMinPrice = 100;
var gMaxPrice = 1000; //in my project these are dynamic

function expon(val){
    var minv = Math.log(gMinPrice);
    var maxv = Math.log(gMaxPrice);
    var scale = (maxv-minv) / (gMaxPrice-gMinPrice);
    return Math.exp(minv + scale*(val-gMinPrice));

}
function logposition(val){
    var minv = Math.log(gMinPrice);
    var maxv = Math.log(gMaxPrice);
    var scale = (maxv-minv) / (gMaxPrice-gMinPrice);
    return (Math.log(val)-minv) / scale + gMinPrice;
}

然后我使用此代码更改滑块位置:

var gBudgetBottom, gBudgetTop;
$('#priceSlider').slider({ 
    change: function(event, ui){ // inside your slider instance
        gBudgetBottom = Number(expon(ui.values[0])).toFixed(0);
        gBudgetTop = Number(expon(ui.values[1])).toFixed(0);
    }
}

$('#priceSlider').slider({ values: [Number(logposition(gBudgetBottom)).toFixed(0), Number(logposition(gBudgetTop)).toFixed(0)] });

我希望这对您有用:)

I found the solution here: Logarithmic slider

I've used your very helpful code for a project... this is what I have:

var gMinPrice = 100;
var gMaxPrice = 1000; //in my project these are dynamic

function expon(val){
    var minv = Math.log(gMinPrice);
    var maxv = Math.log(gMaxPrice);
    var scale = (maxv-minv) / (gMaxPrice-gMinPrice);
    return Math.exp(minv + scale*(val-gMinPrice));

}
function logposition(val){
    var minv = Math.log(gMinPrice);
    var maxv = Math.log(gMaxPrice);
    var scale = (maxv-minv) / (gMaxPrice-gMinPrice);
    return (Math.log(val)-minv) / scale + gMinPrice;
}

I then change my slider positions using this code:

var gBudgetBottom, gBudgetTop;
$('#priceSlider').slider({ 
    change: function(event, ui){ // inside your slider instance
        gBudgetBottom = Number(expon(ui.values[0])).toFixed(0);
        gBudgetTop = Number(expon(ui.values[1])).toFixed(0);
    }
}

$('#priceSlider').slider({ values: [Number(logposition(gBudgetBottom)).toFixed(0), Number(logposition(gBudgetTop)).toFixed(0)] });

I hope that works for you :)

鹿港小镇 2024-12-20 19:12:50

这就是我所做的:

var resStepValues = [0, .01, .02, .03, .04, .05, 1, 1.5, 2, 3, 4, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 150, 200, 250, 300, 500, 750, 1000, 1500];

var slider = $("#resolution_slider").slider({
    animate: true,
    min: 0,
    max: 29,
    range: 'min',
    values: [0, 29],
    slide: function (event, ui) {

        if (ui.values[0] > ui.values[1]) {
            $("#txtMaxRes").val(resStepValues[ui.values[1]]);
            $("#txtMinRes").val(resStepValues[ui.values[0]]);

        } else {
            $("#txtMaxRes").val(resStepValues[ui.values[0]]);
            $("#txtMinRes").val(resStepValues[ui.values[1]]);
        }
    }

});

This is what i did:

var resStepValues = [0, .01, .02, .03, .04, .05, 1, 1.5, 2, 3, 4, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 150, 200, 250, 300, 500, 750, 1000, 1500];

var slider = $("#resolution_slider").slider({
    animate: true,
    min: 0,
    max: 29,
    range: 'min',
    values: [0, 29],
    slide: function (event, ui) {

        if (ui.values[0] > ui.values[1]) {
            $("#txtMaxRes").val(resStepValues[ui.values[1]]);
            $("#txtMinRes").val(resStepValues[ui.values[0]]);

        } else {
            $("#txtMaxRes").val(resStepValues[ui.values[0]]);
            $("#txtMinRes").val(resStepValues[ui.values[1]]);
        }
    }

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文