Skip to content

Instantly share code, notes, and snippets.

@shubich
Created March 22, 2018 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shubich/f00ac2d72ec900f790028e5074c8f7b4 to your computer and use it in GitHub Desktop.
Save shubich/f00ac2d72ec900f790028e5074c8f7b4 to your computer and use it in GitHub Desktop.
Draggable range slider
<h3>A simple range slider with <span class="gsap">GreenSock</span> draggable</span></h3>
<div id="min">Min: 0</div>
<div id="max">Max: 0</div>
<div id="container">
<div class="knob" id='knob1'></div>
<div class="knob" id='knob2'></div>
<!-- <div class="range"></div> -->
</div>
var minrange = 55;
var maxrange = 200;
var min = document.getElementById("min");
var max = document.getElementById("max");
let leftKnob = new Draggable("#knob1", {
type: "x",
bounds: "#container",
onDrag: updateLeft,
onPress: leftKnobScope,
});
let rightKnob = new Draggable("#knob2", {
type: "x",
bounds: "#container",
onDrag: updateRight,
onPress: rightKnobScope
});
function updateLeft() {
min.innerHTML = "Min: " + leftKnob.x;
}
function updateRight() {
max.innerHTML = "Max: " + rightKnob.x;
}
function leftKnobScope() {
const { minX } = this;
this.applyBounds({ minX, maxX: rightKnob.x });
}
function rightKnobScope() {
const { maxX } = this;
this.applyBounds({ minX: leftKnob.x, maxX });
}
function updateSlider() {
TweenLite.set(leftKnob.target, {
x: minrange,
onUpdate: leftKnob.update,
onUpdateScope: leftKnob
});
TweenLite.set(rightKnob.target, {
x: maxrange,
onUpdate: rightKnob.update,
onUpdateScope: rightKnob
});
min.innerHTML = "Min: " + minrange;
max.innerHTML = "Max: " + maxrange;
}
updateSlider();
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://www.greensock.com/js/src/utils/Draggable.min.js"></script>
<script src="https://www.greensock.com/js/src/plugins/ThrowPropsPlugin.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenLite.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/latest/plugins/CSSPlugin.min.js"></script>
body {
background-color:white;
font-family: Signika Negative, Asap, sans-serif;
}
#container {
box-shadow:inset 0px 0px 10px rgba(0,0,0,1);
background-color: #444444;
height:20px;
top:50px;
overflow:visible;
padding:0;
position:relative;
width: 400px;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
}
#knob1 {
background: green;
}
#knob2 {
background: red;
}
.knob {
text-align: center;
font-family: Asap, Avenir, Arial, sans-serif;
width: 15px;
height: 50px;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
line-height: 100px;
color: black;
position: absolute;
top:-17px;
z-index:2;
border: 2px solid rgba(0,0,0,0.4);
}
.range {
background-color:lime;
height:15px;
width: 10px;
position:relative;
top : 2px;
box-shadow:inset 0px 0px 10px black ;
}
.gsap {
color : green ;
}
.ancient {
font-size:14px;
color:#555;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment