对数滑块
我有一个值范围从 0 到 100 的滑块。
我想将它们映射到 100 到 10,000,000 的范围。
我在网上看到过一些函数,但它们都是用 C++ 编写的。 我需要它在 JavaScript 中。
有任何想法吗?
I have a slider with values ranging from 0 to 100.
I want to map them to a range from 100 to 10,000,000.
I've seen some functions scattered around the net but they're all in C++.
I need it in Javascript.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用如下函数:
结果值与对数刻度匹配:
反向函数将具有与
minp
、maxp
、minv
相同的定义code>、maxv
和scale
,根据如下值计算滑块位置:所有这些都封装在一个类中并作为功能代码片段,它将看起来像这样:
You can use a function like this:
The resulting values match a logarithmic scale:
The reverse function would, with the same definitions for
minp
,maxp
,minv
,maxv
andscale
, calculate a slider position from a value like this:All together, wrapped in a class and as a functional code snippet, it would look like this:
没有完全回答这个问题,但对于感兴趣的人来说,反向映射最后一行
只是为了记录。
注意该值必须> 0。
Not quite answering the question, but for people interested, the reverse maping the last line is
just to document.
NOTE the value must be > 0.
真正的对数滑块的问题在于低端,滑块上的多个点可能会导致重复值。
从纯粹的 UI 角度来看,它也没有为用户输入提供非常直观的输出。
我认为更好的选择是使用均匀分布的“步进”变换。
,我们指定了要使用的一系列增量(例如:1、10、100、1000)。 然后,我们根据定义的增量数量将滑块分成相等的部分。 当我们滑动不同的部分时,滑块输出将按相应的增量递增。
工作演示
反应代码
在上面的示例中,我们定义了
min
、max
和间隔
数组。然后,我们必须找到滑块必须具有的离散值的数量,以便它根据我们定义的间隔分布正确地从最小值到最大值。
在本例中为
535
。注意:您的滑块点不应超过滑块中的像素数
最后,我们只需使用上述算法转换输出即可。 该代码示例还做了一些工作,因此输出始终针对当前步骤间隔进行舍入。
The problem with a true Logarithmic slider is at the low end, multiple points on the slider will likely result in duplicate values.
From purely UI perspective, it also doesn't provide a very intuitive output for the users input.
I think a better option is to use an even-distribution "stepped" transform.
In other words, we specify a series of increments we want to use (ex: 1, 10, 100, 1000). Then we split the slider into equal parts based on the number of increments we defined. When we are sliding through our different sections, the slider output will increment by the respective increment.
WORKING DEMO
REACT CODE
In the above example, we define our
min
,max
&intervals
array.We then must find the number of discrete values our slider must have so that it properly goes from min to max based on our defined interval distributions.
In this case
535
.Note: Your slider points should not exceed the number of pixels in the slider
Finally, we just transform our output using the algorithm described above. The code example also does some work so the output is always round for the the current step interval.
我正在寻找 Angular 的对数滑块,但找不到任何内容,然后我遇到了这个答案,
并且我已经为 Angular 2+ 创建了它(演示位于 Angular 6 中):< a href="https://stackblitz.com/edit/angular-logarithmic-slider" rel="nofollow noreferrer">工作演示
感谢@sth,片段:
I was searching for Logarithmic slider For Angular but can't find any and then I came across this answer ,
And I have created that for Angular 2+ (Demo is in Angular 6) : WORKING DEMO
Thanks to @sth, for snippet :
POW() 函数
这里的 pow() 函数略有不同。 这允许设置控制输入分布的“偏斜”曲线。 输出曲线。 另一个重构版本允许从对数(指数)输入设置滑块。
var widthSliderCurve = 0.42; // -1 和 1 之间 - 控制范围倾斜的方式
widthSlider.oninput = function () {
var thisSlider = document.getElementById("widthSlider");
};let setLogSlider = 函数(值,widthSliderCurve,sliderType,outputName){
/*** 确保常量与 oninput 函数值匹配 ***/
var curve = Math.pow(10, widthSliderCurve);
var 原始最小值 = 1.0; // 这些是在 HTML 的滑块定义中设置的,确保它们匹配!
var 原始最大值 = 200.0; // 这些是在 HTML 的滑块定义中设置的
var 映射输出最小值 = 0.05; // 这是所需的输出最小值
var 映射输出最大值 = 10; // 这是所需的最大输出
var 原始范围 = 原始最大 - 原始最小;
var newRange=mappedOutputMax-mappedOutputMin;
var logToLinear = Math.pow((值 -mappedOutputMin)/newRange, 1/曲线)*originalRange +originalMin;
console.log("logToLinear", logToLinear);
// 设置输出框
var outbox = document.getElementById("wSliderOutput");
outbox.innerHTML = Number(值).toFixed(2);
// 设置滑块的线性比例
document.querySelectorAll(".optionSliders").forEach((optSlider) => {
if (optSlider.getAttribute("data-slider-type") == sliderType) {
optSlider.value = logToLinear;
}
var outBox = document.getElementById(outputName);
outBox.value = 值;
});
};
var widthSliderCurve = 0.42; // -1 和 1 之间 - 控制范围倾斜的方式
widthSlider.oninput = function () {
var thisSlider = document.getElementById("widthSlider");
};
POW() function
Here's a slightly different take with a pow() function. This allows setting a "skew" curve that governs the distribution of the input <-> output curve. Another refactored version allows setting the slider from logarithmic (exponential) input.
var widthSliderCurve = 0.42; // between -1 and 1 - governs the way the range is skewed
widthSlider.oninput = function () {
var thisSlider = document.getElementById("widthSlider");
};let setLogSlider = function (value, widthSliderCurve, sliderType, outputName){
/*** make sure constants match the oninput function values ***/
var curve = Math.pow(10, widthSliderCurve);
var originalMin = 1.0; // these are set in the slider defintion in HTML make sure they match!
var originalMax = 200.0; // these are set in the slider defintion in HTML
var mappedOutputMin = 0.05; // this is the desired output min
var mappedOutputMax = 10; // this is the desired output max
var originalRange = originalMax - originalMin;
var newRange = mappedOutputMax - mappedOutputMin;
var logToLinear = Math.pow((value - mappedOutputMin) / newRange, 1 / curve) * originalRange + originalMin;
console.log("logToLinear ", logToLinear);
// set the output box
var outbox = document.getElementById("wSliderOutput");
outbox.innerHTML = Number(value).toFixed(2);
// set the linear scale on the slider
document.querySelectorAll(".optionSliders").forEach((optSlider) => {
if (optSlider.getAttribute("data-slider-type") == sliderType) {
optSlider.value = logToLinear;
}
var outBox = document.getElementById(outputName);
outBox.value = value;
});
};
var widthSliderCurve = 0.42; // between -1 and 1 - governs the way the range is skewed
widthSlider.oninput = function () {
var thisSlider = document.getElementById("widthSlider");
};
为了获得你想要的分布,我认为你可以使用这个公式:
这是一个独立的页面,它将打印你将获得的 0-100 滑块的值,并通过该公式传递它们:
数字从 100 到在我生锈的数学眼光看来,10,000,000就是你想要的分布。 8-)
To get the distribution you want, I think you can use this formula:
Here's a self-contained page that will print the values you'll get for your 0-100 slider, having passed them through that formula:
The numbers go from 100 to 10,000,000 in what looks to my mathematically-rusty eye to be the distribution you want. 8-)