-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-range.js
43 lines (31 loc) · 1.38 KB
/
custom-range.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
jQuery(document).ready(function () {
var sheet = document.createElement('style'),
$rangeInput = jQuery('.range input'),
prefs = ['webkit-slider-runnable-track', 'moz-range-track', 'ms-track'];
document.body.appendChild(sheet);
var getTrackStyle = function (el) {
console.log(el.value);
var curVal = el.value,
val = (curVal - 1) * 25,
style = '';
// Set active label
jQuery('.range-labels li').removeClass('active selected');
var curLabel = jQuery('.range-labels').find('li:nth-child(' + curVal + ')');
curLabel.addClass('active selected');
curLabel.prevAll().addClass('selected');
// Change background gradient
for (var i = 0; i < prefs.length; i++) {
style += '.range {background: linear-gradient(to right, #9E72E5 0%, #9E72E5 ' + val + '%, #fff ' + val + '%, #fff 100%)}';
style += '.range input::-' + prefs[i] + '{background: linear-gradient(to right, #9E72E5 0%, #9E72E5 ' + val + '%, #b2b2b2 ' + val + '%, #b2b2b2 100%)}';
}
return style;
}
$rangeInput.on('input', function () {
sheet.textContent = getTrackStyle(this);
});
// Change input value on label click
jQuery('.range-labels li').on('click', function () {
var index = jQuery(this).index();
$rangeInput.val(index + 1).trigger('input');
});
});