简体   繁体   中英

Is there a function to get cursor position within an element no matter what the padding/margin is on it or its parents?

Do any of the browsers provide this directly? or may be through one of the libraries?

All I could find was some examples of how to iterate through the hierarchy to add up all the margins and paddings. Is that the only way?

Modern browsers provide some pointer-related properties of an event object, allowing you determine the X,Y coordinates of the mouse pointer for events such as clicking a button or moving the mouse.

Of such properties, you are likely to find one relative to the event's target , although this may depend on the browser and may not be consistent across different browser vendors.

Use jQuery's mouse position features to abstract away differences between browsers.

ScreenX/ScreenY has the correct cursor position Demo

Properties documented here: http://www.quirksmode.org/js/events_properties.html#position

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM