You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2-ui/1-document/11-coordinates/2-position-at/task.md
+8-5Lines changed: 8 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,12 +4,15 @@ importance: 5
4
4
5
5
# Show a note near the element
6
6
7
-
Create a function `positionAt(anchor, position, elem)` that positions `elem`, depending on `position`either at the top (`"top"`), right (`"right"`) or bottom (`"bottom"`) of the element`anchor`.
7
+
Create a function `positionAt(anchor, position, elem)` that positions `elem`, depending on `position`near `anchor`element.
8
8
9
-
Call it inside the function `showNote(anchor, position, html)` that shows an element with the class `"note"` and the text `html` at the given position near the anchor.
9
+
The `position` must be a string with any one of 3 values:
10
+
-`"top"` - position `elem` right above `anchor`
11
+
-`"right"` - position `elem` immediately at the right of `anchor`
12
+
-`"bottom"` - position `elem` right below `anchor`
10
13
11
-
Show the notes like here:
14
+
It's used inside function `showNote(anchor, position, html)`, provided in the task source code, that creates a "note" element with given `html` and shows it at the given `position` near the `anchor`.
Copy file name to clipboardExpand all lines: 2-ui/1-document/11-coordinates/article.md
+61-49Lines changed: 61 additions & 49 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,56 +4,68 @@ To move elements around we should be familiar with coordinates.
4
4
5
5
Most JavaScript methods deal with one of two coordinate systems:
6
6
7
-
1. Relative to the window(or another viewport) top/left.
8
-
2. Relative to the document top/left.
7
+
1.**Relative to the window** - similar to `position:fixed`, calculated from the window top/left edge.
8
+
- we'll denote these coordinates as `clientX/clientY`, the reasoning for such name will become clear later, when we study event properties.
9
+
2.**Relative to the document** - similar to `position:absolute` in the document root, calculated from the document top/left edge.
10
+
- we'll denote them `pageX/pageY`.
9
11
10
-
It's important to understand the difference and which type is where.
12
+
When the page is scrolled to the very beginning, so that the top/left corner of the window is exactly the document top/left corner, these coordinates equal each other. But after the document shifts, window-relative coordinates of elements change, as elements move across the window, while document-relative coordinates remain the same.
11
13
12
-
## Window coordinates: getBoundingClientRect
14
+
On this picture we take a point in the document and demonstrate its coordinates before the scroll (left) and after it (right):
13
15
14
-
Window coordinates start at the upper-left corner of the window.
16
+

15
17
16
-
The method `elem.getBoundingClientRect()` returns window coordinates for `elem` as an object with properties:
18
+
When the document scrolled:
19
+
-`pageY` - document-relative coordinate stayed the same, it's counted from the document top (now scrolled out).
20
+
-`clientY` - window-relative coordinate did change (the arrow became shorter), as the same point became closer to window top.
17
21
18
-
-`top` -- Y-coordinate for the top element edge,
19
-
-`left` -- X-coordinate for the left element edge,
20
-
-`right` -- X-coordinate for the right element edge,
21
-
-`bottom` -- Y-coordinate for the bottom element edge.
22
+
## Element coordinates: getBoundingClientRect
22
23
23
-
Like this:
24
+
The method `elem.getBoundingClientRect()` returns window coordinates for a minimal rectangle that encloses `elem` as an object of built-in [DOMRect](https://www.w3.org/TR/geometry-1/#domrect) class.
24
25
25
-

26
+
Main `DOMRect` properties:
26
27
28
+
-`x/y` -- X/Y-coordinates of the rectangle origin relative to window,
29
+
-`width/height` -- width/height of the rectangle (can be negative).
27
30
28
-
Window coordinates do not take the scrolled out part of the document into account, they are calculated from the window's upper-left corner.
31
+
Additionally, there are derived properties:
29
32
30
-
In other words, when we scroll the page, the element goes up or down, *its window coordinates change*. That's very important.
33
+
-`top/bottom` -- Y-coordinate for the top/bottom rectangle edge,
34
+
-`left/right` -- X-coordinate for the left/right rectangle edge.
31
35
32
36
```online
33
-
Click the button to see its window coordinates:
37
+
For instance click this button to see its window coordinates:
34
38
35
-
<input id="brTest" type="button" value="Show button.getBoundingClientRect() for this button" onclick='showRect(this)'/>
39
+
<p><input id="brTest" type="button" value="Get coordinates using button.getBoundingClientRect() for this button" onclick='showRect(this)'/></p>
If you scroll the page, the button position changes, and window coordinates as well.
56
+
If you scroll the page and repeat, you'll notice that as window-relative button position changes, its window coordinates (`y/top/bottom` if you scroll vertically) change as well.
45
57
```
46
58
47
-
Also:
59
+
Here's the picture of `elem.getBoundingClientRect()` output:
48
60
49
-
- Coordinates may be decimal fractions. That's normal, internally browser uses them for calculations. We don't have to round them when setting to `style.position.left/top`, the browser is fine with fractions.
50
-
- Coordinates may be negative. For instance, if the page is scrolled down and the top `elem` is now above the window. Then, `elem.getBoundingClientRect().top` is negative.
51
-
- Some browsers (like Chrome) provide additional properties, `width` and `height` of the element that invoked the method to `getBoundingClientRect` as the result. We can also get them by subtraction: `height=bottom-top`, `width=right-left`.
61
+

52
62
53
-
```warn header="Coordinates right/bottom are different from CSS properties"
54
-
If we compare window coordinates versus CSS positioning, then there are obvious similarities to `position:fixed`. The positioning of an element is also relative to the viewport.
63
+
As you can see, `x/y` and `width/height` fully describe the rectangle. Derived properties can be easily calculated from them:
55
64
56
-
But in CSS, the `right` property means the distance from the right edge, and the `bottom` property means the distance from the bottom edge.
65
+
-`left = x`
66
+
-`top = y`
67
+
-`right = x + width`
68
+
-`bottom = y + height`
57
69
58
70
Please note:
59
71
@@ -119,8 +131,6 @@ The method `document.elementFromPoint(x,y)` only works if `(x,y)` are inside the
119
131
120
132
If any of the coordinates is negative or exceeds the window width/height, then it returns `null`.
121
133
122
-
In most cases such behavior is not a problem, but we should keep that in mind.
123
-
124
134
Here's a typical error that may occur if we don't check for it:
Most of time we need coordinates to position something. In CSS, to position an element relative to the viewport we use `position:fixed` together with `left/top` (or `right/bottom`).
147
+
Most of time we need coordinates in order to position something.
138
148
139
-
We can use `getBoundingClientRect` to get coordinates of an element, and then to show something near it.
149
+
To show something near an element, we can use `getBoundingClientRect` to get its coordinates, and then CSS `position` together with `left/top` (or `right/bottom`).
140
150
141
151
For instance, the function `createMessageUnder(elem, html)` below shows the message under `elem`:
142
152
@@ -183,32 +193,14 @@ The reason is obvious: the message element relies on `position:fixed`, so it rem
183
193
184
194
To change that, we need to use document-based coordinates and `position:absolute`.
185
195
186
-
## Document coordinates
196
+
## Document coordinates [#getCoords]
187
197
188
198
Document-relative coordinates start from the upper-left corner of the document, not the window.
189
199
190
200
In CSS, window coordinates correspond to `position:fixed`, while document coordinates are similar to `position:absolute` on top.
191
201
192
202
We can use `position:absolute` and `top/left` to put something at a certain place of the document, so that it remains there during a page scroll. But we need the right coordinates first.
193
203
194
-
For clarity we'll call window coordinates `(clientX,clientY)` and document coordinates `(pageX,pageY)`.
195
-
196
-
When the page is not scrolled, then window coordinate and document coordinates are actually the same. Their zero points match too:
197
-
198
-

199
-
200
-
And if we scroll it, then `(clientX,clientY)` change, because they are relative to the window, but `(pageX,pageY)` remain the same.
201
-
202
-
Here's the same page after the vertical scroll:
203
-
204
-

205
-
206
-
- `clientY` of the header `"From today's featured article"` became `0`, because the element is now on window top.
207
-
- `clientX` didn't change, as we didn't scroll horizontally.
208
-
- `pageX` and `pageY` coordinates of the element are still the same, because they are relative to the document.
209
-
210
-
## Getting document coordinates [#getCoords]
211
-
212
204
There's no standard method to get the document coordinates of an element. But it's easy to write it.
213
205
214
206
The two coordinate systems are connected by the formula:
@@ -231,6 +223,26 @@ function getCoords(elem) {
231
223
}
232
224
```
233
225
226
+
If in the example above we used it with `position:absolute`, then the message would stay near the element on scroll.
0 commit comments