Murphy1976 Murphy1976 - 3 years ago 165
CSS Question

Styling the HTML5 Range Bar - Left of Thumb | Right of Thumb

I'm using a range slider for a video seek bar in my html5 video player and I would like to know if there is a way, without using jQuery or jQuery UI to style the input range bar so that the background to the LEFT of the thumb is one color, but the RIGHT is another color.

Imagine what you see on the YouTube video player, the unplayed portion of the video seek bar is that dark gray, but the portion that has been played is red. I want to recreate that, even if I have to use two empty divs and then control the width percentage with the slider through javascript...

But I'm hoping for a more stable built-in solution.

UPDATE



I have created a simple function that SHOULD update a css3 gradient in the background, but it doesn't seem to be updating. The alert I have in the function works exactly how it should, but for some reason the value I pass through the function is not setting the gradient stops along the way.

HTML:

<input id="seekslider" type="range" min="0" max="100" value="0" step="1" onchange="updateStyle(value)">


JS:

seekslider = document.getElementById("seekslider");

function updateStyle(v){
c = 100 - v;
seekslider.style.background = "-moz-linear-gradient(left, #ed1e24 0%, #ed1e24 "+ v +"%, #191919 "+ c +"%, #191919 100%)";
seekslider.style.background = "-webkit-gradient(linear, left top, right top, color-stop(0%,#ed1e24), color-stop("+ v +"%,#ed1e24), color-stop("+ c +"%,#191919), color-stop(100%,#191919))";
seekslider.style.background = "-webkit-linear-gradient(left, #ed1e24 0%,#ed1e24 "+ v +"%,#191919 "+ c +"%,#191919 100%)";
seekslider.style.background = "-o-linear-gradient(left, #ed1e24 0%,#ed1e24 "+ v +"%,#191919 "+ c +"%,#191919 100%)";
seekslider.style.background = "-ms-linear-gradient(left, #ed1e24 0%,#ed1e24 "+ v +"%,#191919 "+ c +"%,#191919 100%)";
seekslider.style.background = "linear-gradient(to right, #ed1e24 0%,#ed1e24 "+ v +"%,#191919 "+ c +"%,#191919 100%)";
alert(v +" + "+c+" = "+(Number(v)+Number(c)));
}


UPDATE



The script does work - but not if the value of the slider is under 50

Answer Source

Your script is only working when v exceeds 50 is because you are using v as a left-bound limit for the gradient and c as a right-bound limit — when v falls below 50, the value of c will be more than 50 causing the limits to overlap. In other words, this causes the colour stops to overlap, with the second colour stop being further away than the third colour stop. The browser will therefore ignore the invalid colour stops.

In fact, you only need to access v and need not perform any calculations for c. See fixed fiddle here: http://jsfiddle.net/teddyrised/qsLeo3bc/ I also recommend using event listeners instead of inline JS. For the HTML, simply remove the inline JS binding:

<input id="seekslider" type="range" min="0" max="100" value="0" step="1">

...and for the JS:

var seekslider = document.getElementById("seekslider");

seekslider.addEventListener('input', function() {
    var v = this.value;

    seekslider.style.background = "-moz-linear-gradient(left,  #ed1e24 0%, #ed1e24 "+ v +"%, #191919 "+ v +"%, #191919 100%)";
    seekslider.style.background = "-webkit-gradient(linear, left top, right top, color-stop(0%,#ed1e24), color-stop("+ v +"%,#ed1e24), color-stop("+ v +"%,#191919), color-stop(100%,#191919))";
    seekslider.style.background = "-webkit-linear-gradient(left,  #ed1e24 0%,#ed1e24 "+ v +"%,#191919 "+ v +"%,#191919 100%)";
    seekslider.style.background = "-o-linear-gradient(left,  #ed1e24 0%,#ed1e24 "+ v +"%,#191919 "+ v +"%,#191919 100%)";
    seekslider.style.background = "-ms-linear-gradient(left,  #ed1e24 0%,#ed1e24 "+ v +"%,#191919 "+ v +"%,#191919 100%)";
    seekslider.style.background = "linear-gradient(to right,  #ed1e24 0%,#ed1e24 "+ v +"%,#191919 "+ v +"%,#191919 100%)";
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download