<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Custom range slider</title>
</head>
<body>
<div class="slider-wrapper">
<div id="slider1" class="range-slider"></div>
</div>
</body>
</html>
let that;
class CustomRangeSlider {
constructor(id, valueList = [], callback, min = 0, max = 100) {
this.id = id;
this.valueList = valueList;
this.min = min;
this.max = max;
this.callBack = callback;
that = this;
this.setup();
}
setup() {
that.slider = document.getElementById(this.id);
const sliderTemplate = "<div id='" + this.id + "thumb' class='slider-thumb'></div><div id='" + this.id + "track' class='slider-runnable-track'></div><div id='" + this.id + "fill' class='slider-fill'></div>"
that.slider.innerHTML = sliderTemplate;
that.thumb = document.getElementById(this.id + 'thumb');
that.sliderFill = document.getElementById(this.id + 'fill');
that.setupSliderWithValueList();
that.addEvents();
that.mouseDown = false;
that.initSlider();
}
getThumbWidth(){
return that.thumb.offsetWidth / that.slider.offsetWidth * 100 / 2;
}
setupSliderWithValueList(){
that.pointsList = [];
let diff = Math.floor(100/(that.valueList.length-1));
that.thumbWidth = that.getThumbWidth();
for(var i=0;i<that.valueList.length;i++){
that.pointsList.push((diff)*i-that.thumbWidth);
}
}
addEvents() {
if('ontouchstart' in window){
that.slider.addEventListener('touchstart', this.onMouseDown, false);
that.slider.addEventListener('touchmove', this.onMouseMove, false);
that.slider.addEventListener('touchend', this.onMouseUp, false);
}else if(window.navigator.msPointerEnabled){
that.slider.addEventListener('MSPointerDown', this.onMouseDown, false);
that.slider.addEventListener('MSPointerMove', this.onMouseMove, false);
that.slider.addEventListener('MSPointerUp', this.onMouseUp, false);
}else if(window.PointerEvent){
that.slider.addEventListener('pointerdown', this.onMouseDown, false);
that.slider.addEventListener('pointermove', this.onMouseMove, false);
that.slider.addEventListener('pointerup', this.onMouseUp, false);
}else {
that.slider.addEventListener('mousedown', this.onMouseDown, false);
that.slider.addEventListener('mousemove', this.onMouseMove, false);
that.slider.addEventListener('mouseup', this.onMouseUp, false);
}
that.slider.addEventListener('resize', this.onWindowResize, false);
if (window.addEventListener) {
window.addEventListener('orientationchange', that.onOrientationChange, false);
} else if (window.attachEvent) {
window.attachEvent('onorientationchange', that.onOrientationChange);
}
}
onWindowResize(e){
that.setupSliderWithValueList();
that.initSlider();
}
onOrientationChange(){
setTimeout(function () {
that.setupSliderWithValueList();
that.initSlider();
},0)
}
initSlider(val){
that.currentIndex = that.valueList.indexOf(val) || 0;
that.currentIndex = that.currentIndex < 0 ? 0 : that.currentIndex;
that.position = that.pointsList[that.currentIndex];
that.thumb.style.left = that.pointsList[that.currentIndex]+'%';
that.sliderFill.style.width = that.pointsList[that.currentIndex]+'%';
}
onMouseDown(e) {
e.stopImmediatePropagation();
let X = e.clientX || e.targetTouches[0].pageX;
that.sliderOffsetLeft = that.getOffsetLeft(that.slider).left;
that.sliderOffsetWidth = that.slider.offsetWidth;
that.thumbWidth = that.getThumbWidth();
that.update(X);
that.mouseDown = true;
}
onMouseMove(e) {
//e.stopImmediatePropagation();
if (!that.mouseDown) return;
let X = e.clientX || e.targetTouches[0].pageX;
that.update(X);
that.currentIndex = that.findClosestValueFromArray(that.position, that.pointsList);
that.callBack(that.valueList[that.currentIndex],that.currentIndex);
//that.updateThumbPositionAndFillSlider();
that.sliderMove = true;
}
calculateLeftValueInPercentage(clientX) {
return Math.abs([(clientX - that.sliderOffsetLeft) / that.sliderOffsetWidth] * 100) - that.thumbWidth;
}
onMouseUp(e) {
e.stopImmediatePropagation();
let X = e.clientX || e.targetTouches[0].pageX;
that.mouseDown = false;
that.sliderMove = false;
that.update(X);
that.updateThumbPositionAndFillSlider();
}
getOffsetLeft(element) {
let x = 0;
while (element && !isNaN(element.offsetLeft) && element.offsetLeft > 0) {
x = element.offsetLeft - element.scrollLeft;
element = element.offsetParent;
}
return { left: x };
}
update(x){
that.position= x - that.sliderOffsetLeft;
if(that.slider.getBoundingClientRect().x) that.position = Math.round((that.position/ that.slider.getBoundingClientRect().width)*100);
else that.position = Math.round((that.position/ that.slider.offsetWidth)*100);
//that.position-= that.thumbWidth;
if(that.position<0 || that.position>100){
that.updateThumbPositionAndFillSlider();
return;
}
that.thumb.style.left = that.position+'%';
that.sliderFill.style.width = that.position+'%'
}
updateThumbPositionAndFillSlider(){
that.currentIndex = that.findClosestValueFromArray(that.position, that.pointsList);
that.thumb.style.left = that.pointsList[that.currentIndex]+'%';
that.sliderFill.style.width = that.pointsList[that.currentIndex]<0?0:that.pointsList[that.currentIndex]+'%';
that.callBack(that.valueList[that.currentIndex],that.currentIndex);
}
findClosestValueFromArray(val,array){
let minDiff = val - array[0];
let closest = 0;
for (let i = 1; i < array.length; i++) {
let diff = Math.abs(val - array[i]);
if (diff < minDiff) {
minDiff = diff;
closest = i;
}
}
return closest;
}
}
(function initSlider() {
function onSliderChange(value) {
console.log(value)
}
let rangeSlider = new CustomRangeSlider('slider1', [10, 20, 30, 40],onSliderChange,null,null);
})();
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |