wait-till-i.com wait-till-i.com

Quick solution: getting the mouse position on an element regardless of positioning

As I was upgrading an older codebase of mine that used layerX and layerY I looked into a very succinct way of finding the current mouse position on any element regardless of its position, scrolling, padding, margin and such and I found this to work for me, so maybe it is good for you, too. const getposition = ev => { let x = ev.clientX; let y = ev.clientY; let pos = ev.target.getBoundingClientRect(); // the bitwise shift |0 rounds...