use mousewheel to change a focused input[type=range]

This commit is contained in:
tophf 2020-11-09 22:59:42 +03:00
parent dc4819e7d0
commit 30b9378d2c

View File

@ -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();