-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
115 lines (95 loc) · 3.16 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
---
title: "MouseEvent: layerX property"
short-title: layerX
slug: Web/API/MouseEvent/layerX
page-type: web-api-instance-property
status:
- non-standard
browser-compat: api.MouseEvent.layerX
---
{{APIRef("UI Events")}}{{Non-standard_Header}}
The **`MouseEvent.layerX`** read-only property returns the
horizontal coordinate of the event relative to the current layer.
This property takes scrolling of the page into account and returns a value relative to
the whole of the document unless the event occurs inside a positioned element, where the
returned value is relative to the top left of the positioned element.
## Value
An integer value in pixels for the x-coordinate of the mouse pointer, when the mouse event fired.
## Examples
```html
<html lang="en">
<head>
<title>pageX\pageY & layerX\layerY example</title>
<script>
function showCoords(evt) {
const form = document.forms.form_coords;
const parent_id = evt.target.parentNode.id;
form.parentId.value = parent_id;
form.pageXCoords.value = evt.pageX;
form.pageYCoords.value = evt.pageY;
form.layerXCoords.value = evt.layerX;
form.layerYCoords.value = evt.layerY;
}
</script>
<style>
#d1 {
border: solid blue 1px;
padding: 20px;
}
#d2 {
position: absolute;
top: 180px;
left: 80%;
right: auto;
width: 40%;
border: solid blue 1px;
padding: 20px;
}
#d3 {
position: absolute;
top: 240px;
left: 20%;
width: 50%;
border: solid blue 1px;
padding: 10px;
}
</style>
</head>
<body onmousedown="showCoords(event)">
<p>To display the mouse coordinates please click anywhere on the page.</p>
<div id="d1">
<span>
This is an un-positioned div so clicking it will return layerX/layerY
values almost the same as pageX/PageY values.
</span>
</div>
<div id="d2">
<span>
This is a positioned div so clicking it will return layerX/layerY values
that are relative to the top-left corner of this positioned element.
Note the pageX\pageY properties still return the absolute position in
the document, including page scrolling.
</span>
<span>
Make the page scroll more! This is a positioned div so clicking it will
return layerX/layerY values that are relative to the top-left corner of
this positioned element. Note the pageX\pageY properties still return
the absolute position in the document, including page scrolling.
</span>
</div>
<div id="d3">
<form name="form_coords" id="form1">
Parent Element id: <input type="text" name="parentId" size="7" /><br />
pageX: <input type="text" name="pageXCoords" size="7" /> pageY:
<input type="text" name="pageYCoords" size="7" /><br />
layerX: <input type="text" name="layerXCoords" size="7" /> layerY:
<input type="text" name="layerYCoords" size="7" />
</form>
</div>
</body>
</html>
```
## Specifications
_This property is not part of any specification._
## Browser compatibility
{{Compat}}