Pon*_*lar 9 css html5 pseudo-element input-type-range
我想自定义输入类型范围喜欢这样.
我尝试使用以下代码更改拇指,如下所示,但未更改选定的范围颜色.
HTML:
<input type="range" />
Run Code Online (Sandbox Code Playgroud)
CSS:
input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
height: 20px;
width: 20px;
border-radius: 10px;
background: red;
cursor: pointer;
border: none;
margin-top: -8px;
}
input[type=range] {
-webkit-appearance: none;
outline: 0;
margin: 0;
padding: 0;
height: 30px;
width: 100%;
}
Run Code Online (Sandbox Code Playgroud)
有人可以建议我更改选定的可运行轨道背景颜色吗?
Ionic框架使用有趣的方法仅使用CSS来设置范围输入轨道的样式.他们正在添加一个::before伪元素::-webkit-slider-thumb,使其尽可能宽,然后将其定位在轨道的顶部.(我无法border-radius使用它.)
input[type='range'] {
width: 210px;
height: 30px;
overflow: hidden;
cursor: pointer;
}
input[type='range'],
input[type='range']::-webkit-slider-runnable-track,
input[type='range']::-webkit-slider-thumb {
-webkit-appearance: none;
}
input[type='range']::-webkit-slider-runnable-track {
width: 200px;
height: 10px;
background: #AAA;
}
input[type='range']::-webkit-slider-thumb {
position: relative;
height: 30px;
width: 30px;
margin-top: -10px;
background: steelblue;
border-radius: 50%;
border: 2px solid white;
}
input[type='range']::-webkit-slider-thumb::before {
position: absolute;
content: '';
height: 10px; /* equal to height of runnable track */
width: 500px; /* make this bigger than the widest range input element */
left: -502px; /* this should be -2px - width */
top: 8px; /* don't change this */
background: #777;
}Run Code Online (Sandbox Code Playgroud)
<div class="container">
<input type="range" min="0" max="100" value="10" />
</div>Run Code Online (Sandbox Code Playgroud)
据我所知,对于Webkit驱动的浏览器(和FF),没有纯CSS方法可以做到这一点.IE提供了一种使用的方式来设置轨道的两个部分的样式-ms-fill-lower,-ms-fill-upper但是在WebKit中没有等价物,因此需要JavaScript.
您可以使用linear-gradient可运行轨道的背景图像,然后更改background-size使用JavaScript以实现所需的效果.由于问题是针对Webkit的,因此目前提供的代码段仅适用于Webkit驱动的浏览器.此方法允许我们向border-radius轨道添加a .
这个片段改编自Ana Tudor的CodePen演示.该演示还有一些方法可以让它在其他浏览器中运行.
window.onload = function() {
var input = document.querySelector('input[type=range]'),
style_el = document.createElement('style'),
styles = [],
track_sel = ['::-webkit-slider-runnable-track'];
document.body.appendChild(style_el);
styles.push('');
input.addEventListener('input', function() {
var min = this.min || 0,
max = this.max || 100,
c_style, u, edge_w, val, str = '';
this.setAttribute('value', this.value);
val = this.value + '% 100%';
str += 'input[type="range"]' + track_sel[0] + '{background-size:' + val + '}';
styles[0] = str;
style_el.textContent = styles.join('');
}, false);
}Run Code Online (Sandbox Code Playgroud)
input[type='range'] {
width: 210px;
height: 50px;
cursor: pointer;
}
input[type='range'],
input[type='range']::-webkit-slider-runnable-track,
input[type='range']::-webkit-slider-thumb {
-webkit-appearance: none;
}
input[type='range']::-webkit-slider-runnable-track {
width: 200px;
height: 10px;
background: linear-gradient(to right, #777, #777), #AAA;
background-size: 10% 100%;
background-repeat: no-repeat;
border-radius: 5px;
}
input[type='range']::-webkit-slider-thumb {
height: 30px;
width: 30px;
margin-top: -10px;
background: steelblue;
border-radius: 50%;
border: 2px solid white;
}Run Code Online (Sandbox Code Playgroud)
<div class="container">
<input type="range" min="0" max="100" value="10" />
</div>Run Code Online (Sandbox Code Playgroud)
小智 8
我得到了一个不使用样式标签但使用css 变量的解决方案:
const input = document.querySelector("input");
function setBackgroundSize(input) {
input.style.setProperty("--background-size", `${getBackgroundSize(input)}%`);
}
setBackgroundSize(input);
input.addEventListener("input", () => setBackgroundSize(input));
function getBackgroundSize(input) {
const min = +input.min || 0;
const max = +input.max || 100;
const value = +input.value;
const size = (value - min) / (max - min) * 100;
return size;
}Run Code Online (Sandbox Code Playgroud)
input[type='range'] {
width: 400px;
}
input[type='range'],
input[type='range']::-webkit-slider-runnable-track,
input[type='range']::-webkit-slider-thumb {
-webkit-appearance: none;
}
input[type='range']::-webkit-slider-runnable-track {
height: 3px;
background: linear-gradient(to right, #293043, #293043), #D7D7D7;
background-size: var(--background-size, 0%) 100%;
background-repeat: no-repeat;
border-radius: 5px;
}
input[type='range']::-webkit-slider-thumb {
width: 15px;
height: 15px;
cursor: pointer;
background: #293043;
border: solid white 1px;
border-radius: 50%;
margin-top: -6px;
box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.4);
}
/** FF*/
input[type="range"]::-moz-range-progress {
background-color: #293043;
border-radius: 5px;
}
input[type="range"]::-moz-range-track {
background-color: #D7D7D7;
border-radius: 5px;
}
input[type="range"]::-moz-range-thumb {
width: 15px;
height: 15px;
cursor: pointer;
background: #293043;
border: solid white 1px;
border-radius: 50%;
margin-top: -6px;
box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.4);
}Run Code Online (Sandbox Code Playgroud)
<input type="range" min="5" max="95" step="1" value="15">Run Code Online (Sandbox Code Playgroud)
这适用于 Chrome 和 Firefox :)。