
Javascript offsetY property
The offsetY
property is a read-only property of the MouseEvent object in JavaScript. It represents the Y-coordinate of the mouse pointer’s position relative to the target element (the element that triggered the event) in pixels.
Here’s an example of how to use the offsetY
property in an event handler:
document.addEventListener('click', function(event) {
var offsetY = event.offsetY;
console.log('Y-coordinate relative to the target element:', offsetY);
});
In the example above, we add a click event listener to the document
object. When a click event occurs, the event handler function is called, and the MouseEvent object is passed as the event
parameter.
Within the event handler, we can access the offsetY
property of the MouseEvent object to retrieve the Y-coordinate of the mouse pointer relative to the target element. We then log the value to the console.
Note that the offsetY
property is only applicable to certain types of events, such as mouse events (click
, mousemove
, mouseover
, etc.), and it is not supported in all browsers. In some cases, you may need to use alternative properties, such as clientY
or pageY
, to get the Y-coordinate of the mouse pointer.