样式输入范围前后

时间:2015-01-26 22:25:04

标签: html css html5 css3

我在CSS-Tricks中关注this method来设置输入范围的样式,并尝试使用前后伪类。以下是我尝试过的代码:

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

这似乎没有办法。有人可以帮助我如何设置滑块轨道的样式。我需要一个纯CSS解决方案。基本上我想要它look like this

我还会粘贴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;
}

4 个答案:

答案 0 :(得分:3)

可以在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

答案 1 :(得分:1)

这里有一个延迟回复:

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

实现这一目标的方法

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

您还可以在对类似问题的答案中找到相关信息:Style lower and upper fill in HTML5 range input

答案 2 :(得分:0)

据我所知,完全在 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;" />

答案 3 :(得分:0)

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

一些 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">