I try to detect a middle mouse button click on a HTML Custom Element by adding a listener for the auxclick
-Event to it. As long as the page is not scrollable, this works totally fine in Chrome on a Win 10 machine.
As soon as the page is scrollable however, the event is not fired anymore and the standard bahavior of a middle click kicks in (scrolling the page by moving the mouse).
Does anyone know a way to work around this issue?
I've created a small CodePen to demonstrate the behavior.
Without scrolling it just works fine, but if you uncomment line 2 in the CSS pane (min-height: 200vh;
) the middle click triggers the scrolling behaviour.
document.getElementsByClassName("middle-click")[0].onmousedown = function(e) { if (e.which === 2) { console.log('Disabled'); return false; }};
– Jaquesdocument.getElementsByClassName("middle-click")[0].onmousedown = function(e) { if (e.which === 2) { console.log('Disabled'); const counter = document.querySelector('.counter'); counter.innerText = Number.parseInt(counter.innerText) + 1; }};
– JaquespreventDefault()
on the Mouse Down Event, it seems to work: codepen.io/nioe/pen/rNwWWZj – Aleman