我想制作一个滑块,当值改变时改变点的大小。 所以值 1 的大小为 10px,值 100 的大小为 100px。
如何使用 javascript 更改 .slider::-webkit-slider-thumb 高度?
var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
output.innerHTML = slider.value;
slider.oninput = function() {
output.innerHTML = this.value;
}
.slidecontainer {
width: 100%;
}
.slider {
-webkit-appearance: none;
width: 100%;
height: 15px;
border-radius: 5px;
background: #d3d3d3;
outline: none;
opacity: 0.7;
-webkit-transition: .2s;
transition: opacity .2s;
}
.slider:hover {
opacity: 1;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 25px;
height: 25px;
border-radius: 50%;
background: #4CAF50;
cursor: pointer;
}
.slider::-moz-range-thumb {
width: 25px;
height: 25px;
border-radius: 50%;
background: #4CAF50;
cursor: pointer;
}
<h1>Round Range Slider</h1>
<div class="" "slidecontainer">
<input type="range" min="1" max="100" value="50" class="slider" id="myRange">
<p>Value: <span id="demo"></span></p>
</div>
我试过的=
document.getElementById("myRange").style.webkitTransform = "height: 100px";
好吧,所以,在回答问题之前:
::-webkit-slider-thumb
是一个伪元素。这些元素不是 DOM 的一部分。因此,在 JavaScript 中更改它们可能会有点麻烦。
一个合适的解决方案是用另一个元素完全替换拇指。
现在回答这个问题:当滑块值发生变化时,您可以将样式注入具有给定属性的
<style>
元素。
所以你会添加
<style data="test" type="text/css"></style>
到您的标题,然后定位元素并像这样向其添加CSS:
var style = document.querySelector('[data="test"]');
style.innerHTML = ".class { property: value; }";
结合您在此链接 提供的代码 将解决您的问题。
演示:
var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
var style = document.querySelector('[data="test"]');
setData(slider.value);
slider.oninput = function() {
setData(this.value);
}
function setData(x){
output.innerHTML = x;
style.innerHTML = ".slider::-webkit-slider-thumb { width: " + x + "px !important; height: " + x + "px !important; }";
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 25px;
height: 25px;
border-radius: 50%;
background: #4CAF50;
cursor: pointer;
}
.slider {
margin-top: 40px;
-webkit-appearance: none;
width: 100%;
height: 15px;
border-radius: 5px;
background: #d3d3d3;
outline: none;
}
<style data="test" type="text/css"></style>
<input type="range" min="1" max="100" value="50" class="slider" id="myRange">
<p>Value: <span id="demo"></span></p>
另外:你说
所以值 1 的大小为 10px,值 100 的大小为 100px。
如果你想要像值 1 到 10 等于 10px 的方法,值 10 以上的所有内容都是以像素为单位的实际值,你可以更改行
style.innerHTML = "...";
到
style.innerHTML = ".slider::-webkit-slider-thumb { width: " + (x < 10 ? 10 : x) + "px !important; height: " + (x < 10 ? 10 : x) + "px !important; }";
可以找到此版本的现场演示这里
接受的答案有效,但代码很多,难以理解。这几天可以更容易地完成......
在您的 CSS 中只需添加一个这样的变量
--sliderSize
:
.slider {
--sliderSize: 25px; /* <---- Add this */
-webkit-appearance: none;
width: 100%;
height: 15px;
border-radius: 5px;
background: #d3d3d3;
outline: none;
opacity: 0.7;
-webkit-transition: .2s;
transition: opacity .2s;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: var(--sliderSize); /* <---- Set the variable here */
height: var(--sliderSize); /* <--- and here */
border-radius: 50%;
background: #4CAF50;
cursor: pointer;
}
setProperty
将变量设置为您想要的任何值:
const newSliderSize = '100px'; // this could be a passed in dynamic value, etc.
document.getElementById("myRange").style.setProperty('--sliderSize', newSliderSize);
const newSliderSize = '50px'; // this could be a passed in dynamic value, etc.
document.getElementById("myRange").style.setProperty('--sliderSize', newSliderSize);
.slider {
--sliderSize: 25px; /* <---- Add this */
-webkit-appearance: none;
width: 100%;
height: 15px;
border-radius: 5px;
background: #d3d3d3;
outline: none;
opacity: 0.7;
-webkit-transition: .2s;
transition: opacity .2s;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: var(--sliderSize); /* <---- Set the variable here */
height: var(--sliderSize); /* <--- and here */
border-radius: 50%;
background: #4CAF50;
cursor: pointer;
}
<input type="range" min="1" max="100" id="myRange" class="slider" />