Skip to content

Instantly share code, notes, and snippets.

@Krazete
Last active February 20, 2021 04:25
Show Gist options
  • Save Krazete/21b0c5c603480bc0913a66376db2087f to your computer and use it in GitHub Desktop.
Save Krazete/21b0c5c603480bc0913a66376db2087f to your computer and use it in GitHub Desktop.
Add Scrubbing Functionality to all Number Inputs
function convertToScrubber(input) {
var i0 = 0;
var x0 = 0;
var style = document.createElement("style");
function nonNaN(n) {
var m = Number(n);
if (isNaN(m)) {
m = 0;
}
return m;
}
function onMouseUp(e) {
style.remove();
window.removeEventListener("mousemove", onMouseMove);
window.removeEventListener("mouseup", onMouseUp);
window.removeEventListener("touchmove", onMouseMove);
window.removeEventListener("touchend", onMouseUp);
}
function onMouseMove(e) {
var ex = 0;
if (e.touches) {
e.preventDefault();
ex = e.touches[0].clientX;
}
else {
ex = e.clientX;
}
var x1 = ex;
var dx = (x1 - x0);
if (input.step) {
dx *= nonNaN(input.step);
}
var i1 = i0 + dx;
if ("jsscrub" in input.dataset) {
if (input.dataset.jsscrub.includes("integer")) {
i1 = Math.round(i1);
}
if (input.dataset.jsscrub.includes("continuous")) {
var min = nonNaN(input.min);
var max = nonNaN(input.max);
var dm = max - min;
i1 = ((i1 - min) % dm + dm) % dm + min;
}
}
if (input.min) {
i1 = Math.max(i1, input.min);
}
if (input.max) {
i1 = Math.min(i1, input.max);
}
input.value = i1;
input.dispatchEvent(new InputEvent("input"));
}
function onMouseDown(e) {
var ex = 0;
if (e.touches) {
e.preventDefault();
ex = e.touches[0].clientX;
}
else {
ex = e.clientX;
}
i0 = nonNaN(input.value);
x0 = ex;
document.body.appendChild(style);
window.addEventListener("mousemove", onMouseMove);
window.addEventListener("mouseup", onMouseUp);
window.addEventListener("touchmove", onMouseMove, {"passive": false});
window.addEventListener("touchend", onMouseUp);
}
input.style.cursor = "ew-resize";
style.innerHTML = "html {cursor: ew-resize;} body {pointer-events: none;}";
input.addEventListener("mousedown", onMouseDown);
input.addEventListener("touchstart", onMouseDown);
}
function initScrubbers() {
var scrubbers = document.getElementsByClassName("jsscrub");
for (var i = 0; i < scrubbers.length; i++) {
if (scrubbers[i].tagName == "INPUT" && scrubbers[i].type == "number") {
convertToScrubber(scrubbers[i]);
}
}
}
window.addEventListener("load", initScrubbers);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment