在这里我有
我的简单滑块理论上将用于根据滑块位置控制步进电机的运动。我想让它将托管的网站看起来专业,所以我花了很长时间试图找出如何在滚动时更改按钮颜色(这是因为电机控制我的锅炉温度,所以视觉表示temp 会很好)但我的问题是改变 css 值。我知道使用document.getElementById("myRange")
得到.slider
css 但我需要定位.slider::-webkit-slider-thumb
部分作为其background
是按钮本身的颜色。事实证明,我很难找到一种方法来引用它,因为我不太了解双冒号位的作用。我研究发现:hover
其他单冒号类引用是在满足特定条件时进行的,例如鼠标悬停在上方,而双冒号意味着该引用在技术上不存在,但可以抓取用于样式设计?并且它被称为伪元素(我认为)。
我还遇到过很多这样的例子:document.getElementById("testDiv").pseudoStyle("before","color","purple");
和这个:
document.styleSheets[0].insertRule('#elid:hover { background-color: red; }', 0);
document.styleSheets[0].cssRules[0].style.backgroundColor= 'red';
我尝试将其适应我自己的使用,并做了其他事情,例如:
var sliderButton = document.getElementById("myRange");
sliderButton.pseudoStyle("::-webkit-slider-thumb","background",rgb(r, 0, b));
//where r and b are predefined values between 0 and 255
And:
document.styleSheets[0].addRule('.slider::-webkit-slider-thumb','background: green');
这些都不适合我,这可能是我做错的事情,但我一生都无法弄清楚。我所需要的只是一种改变的方法.slider::-webkit-slider-thumb
background
我的代码中的值。
感谢任何有想法的人
.slider {
-webkit-appearance: none;
width: 100%;
height: 15px;
border-radius: 5px;
background: #c6c6c6;
outline: none;
opacity: 0.7;
-webkit-transition: .3s;
transition: opacity .3s;
}
.slider:hover {
opacity: 1;
background: grey;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 25px;
height: 25px;
border-radius: 50%;
background: red;
cursor: pointer;
}
.slider::-moz-range-thumb {
width: 25px;
height: 25px;
border-radius: 50%;
background: #4CAF50;
cursor: pointer;
}
<input type="range" min="1" max="100" value="50" class="slider" id="myRange">