A Pen by Trevan Hetzel on CodePen.
Forked from anonymous/Custom range input slider with labels.markdown
Created
September 18, 2015 07:19
-
-
Save CodeMyUI/50cc862cd7b6d062f756 to your computer and use it in GitHub Desktop.
Custom range input slider with labels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="range"> | |
<input type="range" min="1" max="7" steps="1" value="1"> | |
</div> | |
<ul class="range-labels"> | |
<li class="active selected">Today</li> | |
<li>2 days</li> | |
<li>3 days</li> | |
<li>4 days</li> | |
<li>5 days</li> | |
<li>6 days</li> | |
<li>7 days</li> | |
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sheet = document.createElement('style'), | |
$rangeInput = $('.range input'), | |
prefs = ['webkit-slider-runnable-track', 'moz-range-track', 'ms-track']; | |
document.body.appendChild(sheet); | |
var getTrackStyle = function (el) { | |
var curVal = el.value, | |
val = (curVal - 1) * 16.666666667, | |
style = ''; | |
// Set active label | |
$('.range-labels li').removeClass('active selected'); | |
var curLabel = $('.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, #37adbf 0%, #37adbf ' + val + '%, #fff ' + val + '%, #fff 100%)}'; | |
style += '.range input::-' + prefs[i] + '{background: linear-gradient(to right, #37adbf 0%, #37adbf ' + val + '%, #b2b2b2 ' + val + '%, #b2b2b2 100%)}'; | |
} | |
return style; | |
} | |
$rangeInput.on('input', function () { | |
sheet.textContent = getTrackStyle(this); | |
}); | |
// Change input value on label click | |
$('.range-labels li').on('click', function () { | |
var index = $(this).index(); | |
$rangeInput.val(index + 1).trigger('input'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
padding: 100px; | |
} | |
@mixin rangeThumb { | |
width: 18px; | |
height: 18px; | |
margin: -8px 0 0; | |
border-radius: 50%; | |
background: #37adbf; | |
cursor: pointer; | |
border: 0 !important; | |
} | |
@mixin rangeTrack { | |
width: 100%; | |
height: 2px; | |
cursor: pointer; | |
background: #b2b2b2; | |
} | |
.range { | |
position: relative; | |
width: 550px; | |
height: 5px; | |
} | |
.range input { | |
width: 100%; | |
position: absolute; | |
top: 2px; | |
height: 0; | |
-webkit-appearance: none; | |
// Thumb | |
&::-webkit-slider-thumb { | |
-webkit-appearance: none; // needed again for Chrome & Safari | |
@include rangeThumb; | |
} | |
&::-moz-range-thumb { | |
@include rangeThumb; | |
} | |
&::-ms-thumb { | |
@include rangeThumb; | |
} | |
// Track | |
&::-webkit-slider-runnable-track { | |
@include rangeTrack; | |
} | |
&::-moz-range-track { | |
@include rangeTrack; | |
} | |
&::-ms-track { | |
@include rangeTrack; | |
} | |
&:focus { // override outline/background on focus | |
background: none; | |
outline: none; | |
} | |
&::-ms-track { // A little somethin' somethin' for IE | |
width: 100%; | |
cursor: pointer; | |
background: transparent; | |
border-color: transparent; | |
color: transparent; | |
} | |
} | |
// Labels below slider | |
.range-labels { | |
margin: 18px -41px 0; | |
padding: 0; | |
list-style: none; | |
li { | |
position: relative; | |
float: left; | |
width: 90.25px; | |
text-align: center; | |
color: #b2b2b2; | |
font-size: 14px; | |
cursor: pointer; | |
&::before { | |
position: absolute; | |
top: -25px; | |
right: 0; | |
left: 0; | |
content: ""; | |
margin: 0 auto; | |
width: 9px; | |
height: 9px; | |
background: #b2b2b2; | |
border-radius: 50%; | |
} | |
} | |
.active { | |
color: #37adbf; | |
} | |
.selected::before { | |
background: #37adbf; | |
} | |
.active.selected::before { | |
display: none; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to input more "< li >" itens...