Skip to content

Instantly share code, notes, and snippets.

@lindseycholmes
Created May 30, 2013 08:59
Show Gist options
  • Select an option

  • Save lindseycholmes/5676633 to your computer and use it in GitHub Desktop.

Select an option

Save lindseycholmes/5676633 to your computer and use it in GitHub Desktop.
<div class="slider">
<label id="slider-label" class="slider-label isRight"></label>
<input id="slider-input" class="slider-input" type="range" min="0" value="0" max="100">
</div>
var labelSide = "right";
$('#slider-input').change( function() {
var val = $(this).val();
var valRounded = Math.round( val * 10 ) / 10;
var mid = $(this).attr('max')/2;
$('#slider-label').text( valRounded );
if(val > mid && labelSide == "right") {
labelSide = "left";
$('#slider-label').removeClass("isRight").addClass("isLeft");
} else if(val <= mid && labelSide == "left") {
labelSide = "right";
$('#slider-label').removeClass("isLeft").addClass("isRight");
}
});
/* Number slider ------------------- */
.slider {
position: relative;
display: inline-block;
margin: -4px 6px 0 0;
}
/* Label ------------------- */
.slider-label {
position: absolute;
z-index: 1;
top: 4px;
font-size: 11px;
color: hsla(0,0%,0%,0);
pointer-events: none;
transition: transform .2s cubic-bezier(.26,.08,.15,1), color .6s .6s ease-out;
}
.slider-label.isRight {
right: 50%;
margin-right: -20px;
left: auto;
transform: translateX(20px);
}
.slider-label.isLeft {
right: auto;
margin-left: -20px;
left: 50%;
transform: translate(-20px);
}
.slider:active .slider-label {
color: hsla(0,0%,0%,.7);
transition-delay: 0;
}
/* Input ------------------- */
.slider-input {
appearance: none;
position: relative;
vertical-align: middle;
width: 100px;
border-radius: 20px;
background: hsl(0,0%,80%);
box-shadow: 0 1px 0 hsla(0,0%,100%,.6);
overflow: hidden;
}
.slider-input::-webkit-slider-thumb,
.slider-input::slider-thumb {
appearance: none;
border-radius: 20px;
width: 20px;
height: 20px;
border: 5px solid hsl(0,0%,80%);
cursor: pointer;
background: hsl(0,0%,40%);
box-shadow: inset 0 1px 2px hsla(0,0%,0%,.4);
transition: border-width .2s cubic-bezier(.26, .08, .15, 1);
}
.slider-input:active::-webkit-slider-thumb,
.slider-input:active::slider-thumb {
border: 0px solid hsl(0,0%,80%);
transition-duration: .1;
}
body {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-box-align: center;
-webkit-box-pack: center;
display: flex;
flex-direction: column;
flex-orient: column;
justify-content: center;
align-items: center;
height: 100%;
padding: 50px;
box-sizing: border-box;
background: hsl(0,0%,90%);
}
html {
height: 100%;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment