x
Summary
Gets the x-coordinate of the mouse cursor, relative to the last positioned ancestor element.
Property of dom/MouseEventdom/MouseEvent
Syntax
Note: This property is read-only.
var xCoordinate = event.x;
Return Value
Returns an object of type NumberNumber
Gets the x-coordinate of the mouse pointer, relative to the last positioned ancestor element.
Examples
This example displays the current mouse position in the console.
document.body.onmousemove = function (e) { console.log('X = ' + e.x + ' Y = " + e.y); }
Notes
If the event firing element is relatively positioned, then the x-coordinate from the boundary of the element is returned. If the event firing element and all of its parent elements are not relatively positioned, then the x property returns a coordinate relative to the body element. The x property returns a coordinate relative to the body element. If the mouse or finger is outside the window when the event is called, this property returns -1
.
Related specifications
- CSSOM View
- Working Draft
Attributions
Microsoft Developer Network: [event.x Article]