Input Slider CSS渲染不符合预期 寻求解决方案
HTML5 Range Slider 样式渲染异常问题
我遇到了Input Slider CSS渲染不符合预期的问题:使用滑块样式生成工具生成的CSS在其网页上显示正常,但独立使用时仅滑块圆圈被正确样式化。此前找到的唯一支持此类滑块定制的方案依赖伪元素样式化,兼容性不佳,希望获取跨浏览器的非JS解决方案。
更新2:缺失的是用于更新value属性的JS代码,可查看已采纳答案获取对应代码。
相关代码示例
CSS 代码
input[type=range].slider { height: 23px; -webkit-appearance: none; } /*progress support*/ input[type=range].slider.slider-progress { --range: calc(var(--max) - var(--min)); --ratio: calc((var(--value) - var(--min)) / var(--range)); --sx: calc(0.5 * 16px + var(--ratio) * (100% - 16px)); } input[type=range].slider:focus { outline: none; } /*webkit*/ input[type=range].slider::-webkit-slider-thumb { -webkit-appearance: none; width: 16px; height: 16px; border-radius: 1em; background: #FFFFFF; border: none; box-shadow: 0 0 2px black; margin-top: calc(max((1em - 1px - 1px) * 0.5,0px) - 16px * 0.5); } input[type=range].slider::-webkit-slider-runnable-track { height: 1em; border: 1px solid #b2b2b2; border-radius: 0.5em; background: #B5B5B5; box-shadow: none; } input[type=range].slider::-webkit-slider-thumb:hover { background: #20BBE1; } input[type=range].slider:hover::-webkit-slider-runnable-track { background: #B5B5B5; border-color: #9a9a9a; } input[type=range].slider::-webkit-slider-thumb:active { background: #1C89A9; } input[type=range].slider:active::-webkit-slider-runnable-track { background: #B5B5B5; border-color: #c1c1c1; } input[type=range].slider.slider-progress::-webkit-slider-runnable-track { background: linear-gradient(#20BBE1,#20BBE1) 0/var(--sx) 100% no-repeat, #B5B5B5; } input[type=range].slider.slider-progress:hover::-webkit-slider-runnable-track { background: linear-gradient(#20BBE1,#20BBE1) 0/var(--sx) 100% no-repeat, #B5B5B5; } input[type=range].slider.slider-progress:active::-webkit-slider-runnable-track { background: linear-gradient(#20BBE1,#20BBE1) 0/var(--sx) 100% no-repeat, #B5B5B5; } /*mozilla*/ input[type=range].slider::-moz-range-thumb { width: 16px; height: 16px; border-radius: 1em; background: #FFFFFF; border: none; box-shadow: 0 0 2px black; } input[type=range].slider::-moz-range-track { height: max(calc(1em - 1px - 1px),0px); border: 1px solid #b2b2b2; border-radius: 0.5em; background: #B5B5B5; box-shadow: none; } input[type=range].slider::-moz-range-thumb:hover { background: #20BBE1; } input[type=range].slider:hover::-moz-range-track { background: #B5B5B5; border-color: #9a9a9a; } input[type=range].slider::-moz-range-thumb:active { background: #1C89A9; } input[type=range].slider:active::-moz-range-track { background: #B5B5B5; border-color: #c1c1c1; } input[type=range].slider.slider-progress::-moz-range-track { background: linear-gradient(#20BBE1,#20BBE1) 0/var(--sx) 100% no-repeat, #B5B5B5; } input[type=range].slider.slider-progress:hover::-moz-range-track { background: linear-gradient(#20BBE1,#20BBE1) 0/var(--sx) 100% no-repeat, #B5B5B5; } input[type=range].slider.slider-progress:active::-moz-range-track { background: linear-gradient(#20BBE1,#20BBE1) 0/var(--sx) 100% no-repeat, #B5B5B5; } /*ms*/ input[type=range].slider::-ms-fill-upper { background: transparent; border-color: transparent; } input[type=range].slider::-ms-fill-lower { background: transparent; border-color: transparent; } input[type=range].slider::-ms-thumb { width: 16px; height: 16px; border-radius: 1em; background: #FFFFFF; border: none; box-shadow: 0 0 2px black; margin-top: 0; box-sizing: border-box; } input[type=range].slider::-ms-track { height: 1em; border-radius: 0.5em; background: #B5B5B5; border: 1px solid #b2b2b2; box-shadow: none; box-sizing: border-box; } input[type=range].slider::-ms-thumb:hover { background: #20BBE1; } input[type=range].slider:hover::-ms-track { background: #B5B5B5; border-color: #9a9a9a; } input[type=range].slider::-ms-thumb:active { background: #1C89A9; } input[type=range].slider:active::-ms-track { background: #B5B5B5; border-color: #c1c1c1; } input[type=range].slider.slider-progress::-ms-fill-lower { height: max(calc(1em - 1px - 1px),0px); border-radius: 0.5em 0 0 0.5em; margin: -1px 0 -1px -1px; background: #20BBE1; border: 1px solid #b2b2b2; border-right-width: 0; } input[type=range].slider.slider-progress:hover::-ms-fill-lower { background: #20BBE1; border-color: #9a9a9a; } input[type=range].slider.slider-progress:active::-ms-fill-lower { background: #20BBE1; border-color: #c1c1c1; }
HTML 代码
<div> <input type="range" class="slider slider-progress"> </div>
内容的提问来源于stack exchange,提问作者mseifert
相关产品推荐
相关产品推荐

