使用JS确定范围滑块背景宽度

Using JS to determine range slider background widths

我正在尝试使用 jQuery 控制范围滑块轨道背景宽度。然而,我一直在摸不着头脑,为什么背景似乎与滑块的位置不匹配。

您可以在此处查看工作页面:https://qh.brightchoicemarketing.com/hosting/cloud-vps-hosting/ (在 Chrome 或任何其他 Webkit 浏览器上查看)

这是我的代码:

// .chrome styling
    $( 'input' ).on( 'input', function( ) {
        var x = document.getElementById("cpu").max;
        var x = document.getElementById("ram").max;
        var x = document.getElementById("ssd").max;
        console.log(x);
        y = this.value - 1;
        console.log(y);
        var percentWidth = ((y / x) * 100);
        console.log(percentWidth);
        $( this ).css( 'background', 'linear-gradient(to right, #da0266 0%, #00aeff '+percentWidth +'%, #eef1f8 ' + percentWidth + '%, #eef1f8 100%)' );
    } );

我正在将值记录到控制台,百分比似乎是正确的。我接下来想也许我已经设置了一个手动偏移量来移动滑块拇指,但我没有,当你从左向右移动滑块拇指时,背景似乎从太小变为太大(这让我认为它是一个乘数问题...)。

我的数学正确吗?我从滑块值中减去 1 来处理“0”位置,然后生成滑块拇指应该处于的百分比宽度。

编辑: 除了第一个滑块之外,我正在向所有滑块添加自定义值(这是为了确保它们在推送到商店之前显示用户实际选择的内容):

// Output value for CPU range slider
var sliderCPU = document.getElementById("cpu");
var outputCPU = document.getElementById("valueCPU");
outputCPU.innerHTML = sliderCPU.value;

sliderCPU.oninput = function() {
  outputCPU.innerHTML = this.value;
}

// Set and output custom values for Memory range slider
var valuesRAM = [0,"256 MB","512 MB","1 GB","2 GB","3 GB","4 GB","5 GB","6 GB","7 GB","8 GB","9 GB","10 GB","11 GB","12 GB"];

var sliderRAM = document.getElementById('ram'),
   outputRAM = document.getElementById('valueRAM');
sliderRAM.oninput = function(){
    outputRAM.innerHTML = valuesRAM[this.value];
};
sliderRAM.oninput();

// Set and output custom values for SSD range slider
var valuesSSD = [0,"10 GB","20 GB","30 GB","40 GB","50 GB","60 GB","70 GB","80 GB","90 GB","100 GB","200 GB","300 GB","400 GB","500 GB"];

var sliderSSD = document.getElementById('ssd'),
   outputSSD = document.getElementById('valueSSD');
sliderSSD.oninput = function(){
    outputSSD.innerHTML = valuesSSD[this.value];
};
sliderSSD.oninput();

background-size: 105% 100% !important 添加到此选择器:

input[type="range"] {
        outline: none; 
        transition: background 450ms ease-in;
        -webkit-appearance: none;
        cursor: pointer;
        border-radius: 16px;
        height: 15px;
        -webkit-box-shadow: 0px 2px 2px rgba(255, 255, 255, 0.25), inset 0px 1px 3px rgba(0, 0, 0, 0.3);
        background-size: 105% 100% !important
    }

似乎在 Chrome 和 Firefox

中对我有用