JSFiddle - React, Tailwind, and code Playground
by Allie Yu
HTML
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>rangeslider.js fiddle with label and index numbers</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="rangeslider-wrap">
<input type="range" min="150" max="210" step="0.1" labels="150, 160, 170, 180, 190, 200, 210">
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
<script src='https://rawgit.com/andreruffert/rangeslider.js/develop/dist/rangeslider.min.js'></script>
<script src="js/index.js"></script>
</body>
CSS
body {
font-family: Helvetica, sans-serif;
}
.rangeslider-wrap {
padding-top: 100px;
}
.rangeslider {
position: relative;
height: 4px;
border-radius: 5px;
width: 100%;
background-color: gray;
}
.rangeslider__handle {
transition: background-color .2s;
box-sizing: border-box;
width: 20px;
height: 20px;
border-radius: 100%;
background-color: #0099FF;
touch-action: pan-y;
cursor: pointer;
display: inline-block;
position: absolute;
z-index: 3;
top: -8px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.5), inset 0 0 0 2px white;
}
.rangeslider__handle__value {
transition: background-color .2s, box-shadow .1s, transform .1s;
box-sizing: border-box;
width: 90px;
text-align: center;
padding: 10px;
background-color: #0099FF;
border-radius: 5px;
color: white;
left: -35px;
top: -55px;
position: absolute;
white-space: nowrap;
border-top: 1px solid #007acc;
box-shadow: 0 -4px 1px rgba(0, 0, 0, 0.07), 0 -5px 20px rgba(0, 0, 0, 0.3);
}
.rangeslider__handle__value:before {
transition: border-top-color .2s;
position: absolute;
bottom: -10px;
left: calc(50% - 10px);
content: "";
width: 0;
height: 0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-top: 10px solid;
border-top-color: #0099FF;
}
.rangeslider__handle__value:after {
content: " cm";
}
.rangeslider__fill {
position: absolute;
top: 0;
z-index: 1;
height: 100%;
background-color: #0099FF;
border-radius: 5px;
}
.rangeslider__labels {
position: absolute;
width: 100%;
z-index: 2;
display: flex;
justify-content: space-between;
}
.rangeslider__labels__label {
font-size: 0.75em;
position: relative;
padding-top: 15px;
color: gray;
}
.rangeslider__labels__label:before {
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
content: "";
width: 1px;
height: 9px;
border-radius: 1px;
background-color: rgba(128, 128, 128,...
JavaScript
$('input[type="range"]').rangeslider({
// Feature detection the default is `true`.
// Set this to `false` if you want to use
// the polyfill also in Browsers which support
// the native <input type="range"> element.
polyfill: false,
// Default CSS classes
rangeClass: 'rangeslider',
disabledClass: 'rangeslider--disabled',
horizontalClass: 'rangeslider--horizontal',
fillClass: 'rangeslider__fill',
handleClass: 'rangeslider__handle',
// Callback function
onInit: function() {
$rangeEl = this.$range;
// add value label to handle
var $handle = $rangeEl.find('.rangeslider__handle');
var handleValue = '<div class="rangeslider__handle__value">' + this.value + '</div>';
$handle.append(handleValue);
// get range index labels
var rangeLabels = this.$element.attr('labels');
rangeLabels = rangeLabels.split(', ');
// add labels
$rangeEl.append('<div class="rangeslider__labels"></div>');
$(rangeLabels).each(function(index, value) {
$rangeEl.find('.rangeslider__labels').append('<span class="rangeslider__labels__label">' + value + '</span>');
})
},
// Callback function
onSlide: function(position, value) {
var $handle = this.$range.find('.rangeslider__handle__value');
$handle.text(this.value);
},
// Callback function
onSlideEnd: function(position, value) {}
});