Material Design Style Range Sliders With jQuery And CSS3

Material Design Style Range Sliders With jQuery And CSS3
File Size: 2.29 KB
Views Total:
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

This is a set of Google Material Design inspired slider controls utilizing jQuery, CSS3 and HTML5 range slider inputs.

How to use it:

1. Create the normal range inputs as follows:

<input type="range" max="100" value="50">
<input type="range" max="100" value="50" class="focused">
<input type="range" max="100" value="50" class="clicked">
<input type="range" max="100" value="50" class="disabled">

2. The primary CSS rules to style the range inputs.

input[type=range] {
  -webkit-appearance: none;
  position: relative
}

input[type=range]::-webkit-slider-runnable-track {
 width: 300px;
 height: 2px;
 border: none;
 border-radius: 3px
}

input[type=range]::-webkit-slider-thumb {
 -webkit-appearance: none;
 border: none;
 height: 12px;
 width: 12px;
 border-radius: 510%;
 background: #141414;
 border: 2px solid #515151;
 margin-top: -5px;
 cursor: pointer
}

.focused::-webkit-slider-thumb {
 box-shadow: 0 0 0 10px rgba(255, 255, 255, 0.15)
}

.clicked::-webkit-slider-thumb {
 -webkit-transform: scale(1.5)
}

.disabled::-webkit-slider-thumb {
 -webkit-transform: scale(0.9);
 box-shadow: 0 0 0 3px #141414;
 background: #515151 !important;
 border-color: #515151 !important
}

input[type=range]:focus { outline: none }
 .rangeM input[type=range].disabled::-webkit-slider-runnable-track {
 background: #515151!important
}

.rangeM input[type=range]::-webkit-slider-thumb {
 background: #3f51b5;
 border-color: #3f51b5
}

.range:hover input[type=range]:before {
  color: white;
  content: '50';
  position: absolute;
  font-family: Roboto Slab;
  top: -49px;
  background: #3f51b5;
  padding: 8px 0 3px;
  font-size: 14px;
  width: 30px;
  text-align: center;
  border-radius: 100% 100% 0 0
}

.range:hover input[type=range]:after {
  content: '';
  position: absolute;
  top: -19px;
  left: 50px;
  border-left: 15px solid transparent;
  border-right: 15px solid transparent;
  border-top: 8px solid #3f51b5;
  font-family: Roboto Slab
}

3. Include the necessary jQuery JavaScript library at the bottom of the webpage.

<script src="//code.jquery.com/jquery-3.1.0.slim.min.js"></script> 

4. The core jQuery script to active the sliders.

$('input[type=range]').wrap("<div class='range'></div>");
var i = 1;

$('.range').each(function() {
  var n = this.getElementsByTagName('input')[0].value;
  var x = (n / 100) * (this.getElementsByTagName('input')[0].offsetWidth - 8) - 12;
  this.id = 'range' + i;
  if (this.getElementsByTagName('input')[0].value == 0) {
    this.className = "range"
  } else {
    this.className = "range rangeM"
  }
  this.innerHTML += "<style>#" + this.id + " input[type=range]::-webkit-slider-runnable-track {background:linear-gradient(to right, #3f51b5 0%, #3f51b5 " + n + "%, #515151 " + n + "%)} #" + this.id + ":hover input[type=range]:before{content:'" + n + "'!important;left: " + x + "px;} #" + this.id + ":hover input[type=range]:after{left: " + x + "px}</style>";
  i++
});

$('input[type=range]').on("input", function() {
  var a = this.value;
  var p = (a / 100) * (this.offsetWidth - 8) - 12;
  if (a == 0) {
    this.parentNode.className = "range"
  } else {
    this.parentNode.className = "range rangeM"
  }
  this.parentNode.getElementsByTagName('style')[0].innerHTML += "#" + this.parentNode.id + " input[type=range]::-webkit-slider-runnable-track {background:linear-gradient(to right, #3f51b5 0%, #3f51b5 " + a + "%, #515151 " + a + "%)} #" + this.parentNode.id + ":hover input[type=range]:before{content:'" + a + "'!important;left: " + p + "px;} #" + this.parentNode.id + ":hover input[type=range]:after{left: " + p + "px}";
})

This awesome jQuery plugin is developed by arianalynn. For more Advanced Usages, please check the demo page or visit the official website.