繁体   English   中英

样式输入范围前后

[英]Style input range before and after

我按照 CSS-Tricks 中的这种方法来设置输入范围的样式,并尝试使用前后伪类。 下面是我试过的代码:

input[type=range]::-webkit-slider-thumb:before {
    background: #fff;
}

这似乎并不能解决问题。 谁能帮我设置 slider 曲目的样式。 我需要一个纯 CSS 解决方案 基本上我希望它看起来像这样

我还将粘贴 CSS-Tricks 中的代码:

input[type=range] {
  -webkit-appearance: none;
  margin: 18px 0;
  width: 100%;
}
input[type=range]:focus {
  outline: none;
}
input[type=range]::-webkit-slider-runnable-track {
  width: 100%;
  height: 8.4px;
  cursor: pointer;
  animate: 0.2s;
  box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
  background: #3071a9;
  border-radius: 1.3px;
  border: 0.2px solid #010101;
}
input[type=range]::-webkit-slider-thumb {
  box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
  border: 1px solid #000000;
  height: 36px;
  width: 16px;
  border-radius: 3px;
  background: #ffffff;
  cursor: pointer;
  -webkit-appearance: none;
  margin-top: -14px;
}
input[type=range]:focus::-webkit-slider-runnable-track {
  background: #367ebd;
}
input[type=range]::-moz-range-track {
  width: 100%;
  height: 8.4px;
  cursor: pointer;
  animate: 0.2s;
  box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
  background: #3071a9;
  border-radius: 1.3px;
  border: 0.2px solid #010101;
}
input[type=range]::-moz-range-thumb {
  box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
  border: 1px solid #000000;
  height: 36px;
  width: 16px;
  border-radius: 3px;
  background: #ffffff;
  cursor: pointer;
}
input[type=range]::-ms-track {
  width: 100%;
  height: 8.4px;
  cursor: pointer;
  animate: 0.2s;
  background: transparent;
  border-color: transparent;
  border-width: 16px 0;
  color: transparent;
}
input[type=range]::-ms-fill-lower {
  background: #2a6495;
  border: 0.2px solid #010101;
  border-radius: 2.6px;
  box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
}
input[type=range]::-ms-fill-upper {
  background: #3071a9;
  border: 0.2px solid #010101;
  border-radius: 2.6px;
  box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
}
input[type=range]::-ms-thumb {
  box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
  border: 1px solid #000000;
  height: 36px;
  width: 16px;
  border-radius: 3px;
  background: #ffffff;
  cursor: pointer;
}
input[type=range]:focus::-ms-fill-lower {
  background: #3071a9;
}
input[type=range]:focus::-ms-fill-upper {
  background: #367ebd;
}

在 Firefox 和 IE 中是可能的 -

// Mozilla    
input[type="range"]::-moz-range-progress {
    background: #cc1a1a;
    height: 12px;
    border-radius: 12px;
}
// IE
input[type="range"]::-ms-fill-lower {
    background: #CC1A1A;
    border: 0 solid #000101;
    border-radius: 50px;
    box-shadow: 0 0 0 #000000, 0 0 0 #0d0d0d;
}
input[type="range"]::-ms-fill-upper {
    background: #c0c0c0;
    border: 0 solid #000101;
    border-radius: 50px;
    box-shadow: 0 0 0 #000000, 0 0 0 #0d0d0d;
}

只有一种方式适用于 Chrome :before:after ,不再受支持(自 2016 年 3 月起)。 我发现的最好的 - 是http://rangeslider.js.org/ 它也适用于 Angular JS - https://github.com/danielcrisp/angular-rangeslider

这里有点延迟回复:

纯 CSS 解决方案在这里不可能跨浏览器 - IE 是唯一通过::-ms-fill-lower::-ms-fill-upper

对于完整的跨浏览器解决方案,您需要使用 js。 这里有一个示例,它使用输入的值更新 CSS 渐变,以大致完成您的要求: http : //codepen.io/ryanttb/pen/fHyEJ

您还可以在对类似问题的回答中找到有关此问题的信息: HTML5 范围输入中的样式下限和上限填充

据我所知,完全在跨浏览器的CSS 中不可能进行进度指示(即在句柄的不同侧具有不同的样式)。

这是一个如何使用一些 javascript 代码轻松完成的想法:
https://css-tricks.com/sliding-nightmare-understanding-range-input/#the-range-progress-fill-component

这是一个基于该方法生成 CSS 的工具:
https://toughengineer.github.io/demo/slider-styler

例子:

 for (let e of document.querySelectorAll('input[type="range"].slider-progress')) { e.style.setProperty('--value', e.value); e.style.setProperty('--min', e.min == '' ? '0' : e.min); e.style.setProperty('--max', e.max == '' ? '100' : e.max); e.addEventListener('input', () => e.style.setProperty('--value', e.value)); }
 /*generated with Input range slider CSS style generator (version 20201223) https://toughengineer.github.io/demo/slider-styler*/ input[type=range].styled-slider { height: 2.8em; -webkit-appearance: none; } /*progress support*/ input[type=range].styled-slider.slider-progress { --range: calc(var(--max) - var(--min)); --ratio: calc((var(--value) - var(--min)) / var(--range)); --sx: calc(0.5 * 2.8em + var(--ratio) * (100% - 2.8em)); } input[type=range].styled-slider:focus { outline: none; } /*webkit*/ input[type=range].styled-slider::-webkit-slider-thumb { width: 2.8em; height: 2.8em; border-radius: 1.4em; background: #1381A9; border: 0.3em solid #FFFFFF; box-shadow: none; margin-top: calc(1em * 0.5 - max(2.8em * 0.5,0.3em)); -webkit-appearance: none; } input[type=range].styled-slider::-webkit-slider-runnable-track { height: 1em; border-radius: 0.5em; background: #D0D0D0; border: none; box-shadow: none; } input[type=range].styled-slider::-webkit-slider-thumb:hover { background: #0E6180; } input[type=range].styled-slider:hover::-webkit-slider-runnable-track { background: #A8A8A8; } input[type=range].styled-slider::-webkit-slider-thumb:active { background: #18A5D9; } input[type=range].styled-slider:active::-webkit-slider-runnable-track { background: #DBDBDB; } input[type=range].styled-slider.slider-progress::-webkit-slider-runnable-track { background: linear-gradient(#666666,#666666) 0/var(--sx) 100% no-repeat, #D0D0D0; } input[type=range].styled-slider.slider-progress:hover::-webkit-slider-runnable-track { background: linear-gradient(#404040,#404040) 0/var(--sx) 100% no-repeat, #A8A8A8; } input[type=range].styled-slider.slider-progress:active::-webkit-slider-runnable-track { background: linear-gradient(#8C8C8C,#8C8C8C) 0/var(--sx) 100% no-repeat, #DBDBDB; } /*mozilla*/ input[type=range].styled-slider::-moz-range-thumb { width: max(calc(2.8em - 0.3em - 0.3em),0px); height: max(calc(2.8em - 0.3em - 0.3em),0px); border-radius: 1.4em; background: #1381A9; border: 0.3em solid #FFFFFF; box-shadow: none; } input[type=range].styled-slider::-moz-range-track { height: 1em; border-radius: 0.5em; background: #D0D0D0; border: none; box-shadow: none; } input[type=range].styled-slider::-moz-range-thumb:hover { background: #0E6180; } input[type=range].styled-slider:hover::-moz-range-track { background: #A8A8A8; } input[type=range].styled-slider::-moz-range-thumb:active { background: #18A5D9; } input[type=range].styled-slider:active::-moz-range-track { background: #DBDBDB; } input[type=range].styled-slider.slider-progress::-moz-range-track { background: linear-gradient(#666666,#666666) 0/var(--sx) 100% no-repeat, #D0D0D0; } input[type=range].styled-slider.slider-progress:hover::-moz-range-track { background: linear-gradient(#404040,#404040) 0/var(--sx) 100% no-repeat, #A8A8A8; } input[type=range].styled-slider.slider-progress:active::-moz-range-track { background: linear-gradient(#8C8C8C,#8C8C8C) 0/var(--sx) 100% no-repeat, #DBDBDB; } /*ms*/ input[type=range].styled-slider::-ms-fill-upper { background: transparent; border-color: transparent; } input[type=range].styled-slider::-ms-fill-lower { background: transparent; border-color: transparent; } input[type=range].styled-slider::-ms-thumb { width: 2.8em; height: 2.8em; border-radius: 1.4em; background: #1381A9; border: 0.3em solid #FFFFFF; box-shadow: none; margin-top: 0; box-sizing: border-box; } input[type=range].styled-slider::-ms-track { height: 1em; border-radius: 0.5em; background: #D0D0D0; border: none; box-shadow: none; box-sizing: border-box; } input[type=range].styled-slider::-ms-thumb:hover { background: #0E6180; } input[type=range].styled-slider:hover::-ms-track { background: #A8A8A8; } input[type=range].styled-slider::-ms-thumb:active { background: #18A5D9; } input[type=range].styled-slider:active::-ms-track { background: #DBDBDB; } input[type=range].styled-slider.slider-progress::-ms-fill-lower { height: 1em; border-radius: 0.5em 0 0 0.5em; margin: -undefined 0 -undefined -undefined; background: #666666; border: none; border-right-width: 0; } input[type=range].styled-slider.slider-progress:hover::-ms-fill-lower { background: #404040; } input[type=range].styled-slider.slider-progress:active::-ms-fill-lower { background: #8C8C8C; }
 <input type="range" class="styled-slider slider-progress" style="width: 50em;" />

这是可能的! 如果您的拇指不必大于进度条。

一些 css 向导想出了这个: https : //codepen.io/noahblon/pen/OyajvN

它使用拇指上的框阴影使拇指之前的部分看起来不同的颜色。 重要的是范围隐藏了溢出以使其工作,这就是拇指不能大于轨道的原因。

 body { height: 100vh; margin: 0; display: flex; } input[type="range"] { margin: auto; -webkit-appearance: none; position: relative; overflow: hidden; height: 40px; width: 200px; cursor: pointer; border-radius: 0; /* iOS */ } ::-webkit-slider-runnable-track { background: #ddd; } /* * 1. Set to 0 width and remove border for a slider without a thumb */ ::-webkit-slider-thumb { -webkit-appearance: none; width: 20px; /* 1 */ height: 40px; background: #fff; box-shadow: -100vw 0 0 100vw dodgerblue; border: 2px solid #999; /* 1 */ } ::-moz-range-track { height: 40px; background: #ddd; } ::-moz-range-thumb { background: #fff; height: 40px; width: 20px; border: 3px solid #999; border-radius: 0 !important; box-shadow: -100vw 0 0 100vw dodgerblue; box-sizing: border-box; } ::-ms-fill-lower { background: dodgerblue; } ::-ms-thumb { background: #fff; border: 2px solid #999; height: 40px; width: 20px; box-sizing: border-box; } ::-ms-ticks-after { display: none; } ::-ms-ticks-before { display: none; } ::-ms-track { background: #ddd; color: transparent; height: 40px; border: none; } ::-ms-tooltip { display: none; }
 <input type="range" value="25">

如果有人面临同样的问题,这里有一个(自制的)生成器,用于为这样的进度条生成(纯)css:

https://antvil.github.io/css-sos/sos/progress/

生成示例:

示例进度条

已测试:firefox、chrome、edge、opera、safari

但请注意,对于 webkit 浏览器,它有时会中断,因为无法实现进度的边界半径

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM