Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

style input range to look like a progress bar

Tags:

html

css

i have a video element and i want to create my own controls player.

i'm using range input for the seek bar. i want to style it like this:

enter image description here

so the orange is what you have seen and the teal is the time left.

i managed to style the input like this: https://jsfiddle.net/d3oeztwt/ but i don't know about the orange.

i know i can use progress bar but i can't find a way to trigger the slider with progress bar.

<input type="range">


input[type=range] {
    /*removes default webkit styles*/
    -webkit-appearance: none;
    /*required for proper track sizing in FF*/
    width: 300px;
}
input[type=range]::-webkit-slider-runnable-track {
    width: 300px;
    height: 10px;
    background: #009999;
    border: none;
    border-radius: 3px;
}
input[type=range]::-webkit-slider-thumb {
    -webkit-appearance: none;
    border: none;
    height: 20px;
    width: 20px;
    border-radius: 50%;
    background: #99FFFF;
    margin-top: -4px;
}
input[type=range]:focus {
    outline: none;
}
like image 705
user2587454 Avatar asked Jun 29 '16 09:06

user2587454


3 Answers

I did a cross-browser solution (+ie9, ff, chrome, safari) with SCSS, without JavaScript.

http://codepen.io/nlfonseca/pen/MwbovQ

@import 'bourbon';

$slider-width-number: 240;
$slider-width: #{$slider-width-number}px;
$slider-height: 2px;
$background-slider: #c7c7c7;
$background-filled-slider: #e33d44;
$thumb-width: 28px;
$thumb-height: 18px;
$thumb-radius: 8px;
$thumb-background: #fff;
$thumb-border: 1px solid #777;
$shadow-size: -8px;
$fit-thumb-in-slider: -8px;

@function makelongshadow($color, $size) {
  $val: 5px 0 0 $size $color;

  @for $i from 6 through $slider-width-number {
    $val: #{$val}, #{$i}px 0 0 $size #{$color};
  }

  @return $val;
}

div {
  height: 100px;
  display: flex;
  justify-content: center;
}

input {
  align-items: center;
  appearance: none;
  background: none;
  cursor: pointer;
  display: flex;
  height: 100%;
  min-height: 50px;
  overflow: hidden;
  width: $slider-width;

  &:focus {
    box-shadow: none;
    outline: none;
  }

  &::-webkit-slider-runnable-track {
    background: $background-filled-slider;
    content: '';
    height: $slider-height;
    pointer-events: none;
  }

  &::-webkit-slider-thumb {
    @include size($thumb-width $thumb-height);

    appearance: none;
    background: $thumb-background;
    border-radius: $thumb-radius;
    box-shadow: makelongshadow($background-slider, $shadow-size);
    margin-top: $fit-thumb-in-slider;
    border: $thumb-border;
  }


  &::-moz-range-track {
    width: $slider-width;
    height: $slider-height;
  }

  &::-moz-range-thumb {
    @include size($thumb-width $thumb-height);

    background: $thumb-background;
    border-radius: $thumb-radius;
    border: $thumb-border;
    position: relative;
  }

  &::-moz-range-progress {
    height: $slider-height;
    background: $background-filled-slider;
    border: 0;
    margin-top: 0;
  }

  &::-ms-track {
    background: transparent;
    border: 0;
    border-color: transparent;
    border-radius: 0;
    border-width: 0;
    color: transparent;
    height: $slider-height;
    margin-top: 10px;
    width: $slider-width;
  }

  &::-ms-thumb {
    @include size($thumb-width $thumb-height);

    background: $thumb-background;
    border-radius: $thumb-radius;
    border: $thumb-border;
  }

  &::-ms-fill-lower {
    background: $background-filled-slider;
    border-radius: 0;
  }

  &::-ms-fill-upper {
    background: $background-slider;
    border-radius: 0;
  }

  &::-ms-tooltip {
    display: none;
  }
}
like image 94
nlfonseca Avatar answered Nov 07 '22 03:11

nlfonseca


Webkit/Blink/Gecko based browsers (basically, Chrome, Opera, Firefox) only support the track as a single entity.

In IE, input type=range is supported in IE10 and above. and you can use ::-ms-fill-lower and ::-ms-fill-upper to further customize the look of the track on either size of the thumb.

More detailed information at: http://brennaobrien.com/blog/2014/05/style-input-type-range-in-every-browser.html

Unfortunately, therefore, this is not currently possible in the majority of modern browsers using the standard pseudo-elements absent javascript.

like image 37
Paulie_D Avatar answered Nov 07 '22 04:11

Paulie_D


In onChange function you may use this:

  const onChangeFunction = (e) => {
    const el = e.target;
    el.style.setProperty("--value", el.value);
    el.style.setProperty("--min", el.min === "" ? "0" : el.min);
    el.style.setProperty("--max", el.max === "" ? "100" : el.max);
    el.style.setProperty("--value", el.value);
  };

And css part to add:

      input[type=range] {
        height: 3px;
        -webkit-appearance: none;
        cursor: pointer;
      }
      
      input[type=range] {
        --range: calc(var(--max) - var(--min));
        --ratio: calc((var(--value) - var(--min)) / var(--range));
        --sx: calc(0.5 * 10px + var(--ratio) * (100% - 10px));
      }
      
      input[type=range]:focus {
        outline: none;
      }
      
 
      input[type=range]::-webkit-slider-thumb {
        width: 10px;
        height: 10px;
        border-radius: 1em;
        background: #FF0000;
        border: none;
        box-shadow: 0 0 2px black;
        margin-top: calc(3px * 0.5 - 10px * 0.5);
        -webkit-appearance: none;
      }
      
      input[type=range]::-webkit-slider-runnable-track {
        height: 3px;
        border-radius: 0.5em;
        background: #efefef;
        border: none;
        box-shadow: none;
      }

      input[type=range]::-webkit-slider-thumb:hover {
        background: #FF0000;
      }
      
      input[type=range]:hover::-webkit-slider-runnable-track {
        background: #e5e5e5;
      }
      
      input[type=range]::-webkit-slider-thumb:active {
        background: #F00000;
      }
      
      input[type=range]:active::-webkit-slider-runnable-track {
        background: #f5f5f5;
      }
      
      input[type=range]::-webkit-slider-runnable-track {
        background: linear-gradient(#F80B00,#F80B00) 0/var(--sx) 100% no-repeat, #efefef;
      }
      
      input[type=range]:hover::-webkit-slider-runnable-track {
        background: linear-gradient(#FF0000,#FF0000) 0/var(--sx) 100% no-repeat, #e5e5e5;
      }
      
      input[type=range]:active::-webkit-slider-runnable-track {
        background: linear-gradient(#F90000,#F90000) 0/var(--sx) 100% no-repeat, #f5f5f5;
      }

You should receive this: input range with progress

like image 3
Aleksandrov Sergey Avatar answered Nov 07 '22 05:11

Aleksandrov Sergey