javascript - 带 Shadow dom 的样式输入范围

标签 javascript jquery html css

我需要设置输入类型范围的样式 - 更改输入的背景颜色,并更改 slider 按钮的外观(宽度、高度、颜色)。到目前为止,我已经设法更改输入的背景颜色,但我找不到选择该 slider 按钮的方法。我猜想必须使用 Shadow DOM 来完成,但不知道如何实现。

<input id="my-range" type="range">

#my-range {
    -webkit-appearance: none;
    background-color: #592A71;
    width: 45%;
    height:15px;
    margin:30px;
}

Here is the example

最佳答案

您正在寻找的Shadow-dom can be found here ,这是一个示例(从链接复制):

input[type=range] {
    /*removes default webkit styles*/
    -webkit-appearance: none;
    
    /*fix for FF unable to apply focus style bug */
    border: 1px solid white;
    
    /*required for proper track sizing in FF*/
    width: 300px;
}
input[type=range]::-webkit-slider-runnable-track {
    width: 300px;
    height: 5px;
    background: #ddd;
    border: none;
    border-radius: 3px;
}
input[type=range]::-webkit-slider-thumb {
    -webkit-appearance: none;
    border: none;
    height: 16px;
    width: 16px;
    border-radius: 50%;
    background: goldenrod;
    margin-top: -4px;
}
input[type=range]:focus {
    outline: none;
}
input[type=range]:focus::-webkit-slider-runnable-track {
    background: #ccc;
}

input[type=range]::-moz-range-track {
    width: 300px;
    height: 5px;
    background: #ddd;
    border: none;
    border-radius: 3px;
}
input[type=range]::-moz-range-thumb {
    border: none;
    height: 16px;
    width: 16px;
    border-radius: 50%;
    background: goldenrod;
}

/*hide the outline behind the border*/
input[type=range]:-moz-focusring{
    outline: 1px solid white;
    outline-offset: -1px;
}

input[type=range]::-ms-track {
    width: 300px;
    height: 5px;
    
    /*remove bg colour from the track, we'll use ms-fill-lower and ms-fill-upper instead */
    background: transparent;
    
    /*leave room for the larger thumb to overflow with a transparent border */
    border-color: transparent;
    border-width: 6px 0;

    /*remove default tick marks*/
    color: transparent;
}
input[type=range]::-ms-fill-lower {
    background: #777;
    border-radius: 10px;
}
input[type=range]::-ms-fill-upper {
    background: #ddd;
    border-radius: 10px;
}
input[type=range]::-ms-thumb {
    border: none;
    height: 16px;
    width: 16px;
    border-radius: 50%;
    background: goldenrod;
}
input[type=range]:focus::-ms-fill-lower {
    background: #888;
}
input[type=range]:focus::-ms-fill-upper {
    background: #ccc;
}
<input id="my-range" type="range">

关于javascript - 带 Shadow dom 的样式输入范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35439317/

相关文章:

javascript - 长轮询 jQuery 在 IE 中不起作用

php - 如何在 PHP 中对 JSON 文件执行 GET 请求?

javascript - 单击后显示 div 的 Php 和 Javascript

HTML 下载页面而不是内容

javascript - 将 slider 的下一张和上一张按钮更改为下一张或上一张图片的预览

javascript - Zombie.js jQuery 加载错误 'j.getClientRects is not a function'

javascript - 类型错误 : _this2 is undefined REACTJS

jquery - 使用 Hammer.js 和 .on() 绑定(bind)事件

jquery - 如何获取 JSON 键和值?

html - 选择多个 : Issue on selecting long option