Michiel van Oosterhout
Michiel van Oosterhout

Reputation: 23094

jQuery event.layerX/Y is not relative to the element that triggered the event, is that correct?

If I use for example a mousemove event handler on a div and check the layerX property of the event, it changes when my mouse enters a positioned element inside that div (like an image).

According to the jQuery Event object documentation it should follow the W3C DOM Level 3 specifications. But there's no mention of the layerX/Y property for the MouseEvent interface, so I'm wondering what behavior is according to specification?

To me it seems that you always want the layerX/Y to be relative to the layer that fires the event, if I wanted the layerX/Y of a nested element, I would check the event in a different phase (bubbling) or would attach my handler to that nested element.

My question is (somewhat subjective): how should layerX/Y work?

Upvotes: 0

Views: 3490

Answers (2)

Kevin
Kevin

Reputation: 126

you can also use:

var x = e.pageX - this.offsetLeft;

var y = e.pageY - this.offsetTop;

It's the pretty much the same as using jQuery element.offset() properties, but instead this method uses the native offsetTop and offsetLeft properties.

found near the bottom of: jQuery - Tutorials: Mouse Position

Upvotes: 2

Bob Fincheimer
Bob Fincheimer

Reputation: 18076

I would think that layerx/y would be the offset from the first parent node of the firing element that has position (relative or absolute). Either way, jQuery has better ways of finding positions like this and also LayerX/Y cannot be guaranteed to be the same in every browser, so I wouldn't use it

Upvotes: 1

Related Questions