From 30b9378d2cbf140816c185b8649489bc8cb6fb8a Mon Sep 17 00:00:00 2001 From: tophf Date: Mon, 9 Nov 2020 22:59:42 +0300 Subject: [PATCH] use mousewheel to change a focused input[type=range] --- js/dom.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/js/dom.js b/js/dom.js index 2babfac6..d8bafda8 100644 --- a/js/dom.js +++ b/js/dom.js @@ -97,10 +97,13 @@ document.on('wheel', event => { if (!el || el !== event.target && !el.contains(event.target)) { return; } - if (el.tagName === 'SELECT') { - const old = el.selectedIndex; - el.selectedIndex = Math.max(0, Math.min(el.length - 1, old + Math.sign(event.deltaY))); - if (el.selectedIndex !== old) { + const isSelect = el.tagName === 'SELECT'; + if (isSelect || el.tagName === 'INPUT' && el.type === 'range') { + const key = isSelect ? 'selectedIndex' : 'valueAsNumber'; + const old = el[key]; + const rawVal = old + Math.sign(event.deltaY) * (el.step || 1); + el[key] = Math.max(el.min || 0, Math.min(el.max || el.length - 1, rawVal)); + if (el[key] !== old) { el.dispatchEvent(new Event('change', {bubbles: true})); } event.preventDefault();