javascript - Find element closest to click coordinates -
is there easy way detect closest child element coordinates of mouse click in container?. in case container div
ex:
<div style="white-space: pre-wrap"> <span>e1</span> <span>\n</span> <span>\n</span> <span>e4</span> <span>e5</span> </div>
in above case determine span closest top/left coordinates click took place in div. due line breaks spans don't cover div
in click event have access top/left through e.clienty;
however, having difficulty traversing spans detect span closest coordinates of click inside div.
click handler:
function(e){ var yop = e.clienty; }
here links might useful document.elementfrompoint
:
from summary on mdn link:
returns element document elementfrompoint method being called topmost element lies under given point. element, specify point via coordinates, in css pixels, relative upper-left-most point in window or frame containing document.
http://www.quirksmode.org/dom/w3c_cssom.html
https://developer.mozilla.org/en-us/docs/dom/document.elementfrompoint
so in nutshell, drops pin down z-index stack @ x, y
coordinate, , returns first element hits. super useful, though per quirksmode page support limited.
Comments
Post a Comment