Get Mouse/Finger Pointer Coordinate Relative to Element

function point(el, e) { var x = !!e.touches ? e.touches[0].pageX : e.pageX, y = !!e.touches ? e.touches[0].pageY : e.pageY, left = el.offsetLeft, top = el.offsetTop; while (el = el.offsetParent) { left += el.offsetLeft; top += el.offsetTop; } return { x: x - left, y: y - top }; } /* foo.onclick = function(e) { console.log(point(e.target, e)); }; */
Support for touch devices too.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.