javascript 需要 .slider::-webkit-slider-thumb
.slider::-webkit-slider-thumb needed in javascript
我想制作一个滑块,当值改变时改变点的大小。
所以值 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>
到您的 header,然后定位元素并像这样向其添加 CSS:
var style = document.querySelector('[data="test"]');
style.innerHTML = ".class { property: value; }";
这与您提供的代码相结合 at this link 可以解决您的问题。
演示:
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>
另外:你说
So value 1 will be a size of 10px and value 100 will be size 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; }";
可以找到此版本的现场演示 HERE
2022 - 使用 CSS 个变量
接受的答案工作正常,但它有很多代码且难以理解,现在可以更容易地完成...
在你的 CSS 中添加一个这样的变量:
.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;
}
然后在您的 JavaScript 中使用 setProperty
:
简单地将变量设置为您想要的任何值
const newSliderSize = '100px'; // this could be a passed in dynamic value, etc.
document.getElementById("myRange").style.setProperty('--sliderSize', newSliderSize);
我想制作一个滑块,当值改变时改变点的大小。 所以值 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>
到您的 header,然后定位元素并像这样向其添加 CSS:
var style = document.querySelector('[data="test"]');
style.innerHTML = ".class { property: value; }";
这与您提供的代码相结合 at this link 可以解决您的问题。
演示:
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>
另外:你说
So value 1 will be a size of 10px and value 100 will be size 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; }";
可以找到此版本的现场演示 HERE
2022 - 使用 CSS 个变量
接受的答案工作正常,但它有很多代码且难以理解,现在可以更容易地完成...
在你的 CSS 中添加一个这样的变量:
.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;
}
然后在您的 JavaScript 中使用 setProperty
:
const newSliderSize = '100px'; // this could be a passed in dynamic value, etc.
document.getElementById("myRange").style.setProperty('--sliderSize', newSliderSize);