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/article.md
+23-27Lines changed: 23 additions & 27 deletions
Original file line number
Diff line number
Diff line change
@@ -5,27 +5,27 @@ To move elements around we should be familiar with coordinates.
5
5
Most JavaScript methods deal with one of two coordinate systems:
6
6
7
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 reason for such name will become clear after next chapters ,
8
+
- we'll denote these coordinates as `clientX/clientY`, the reasoning for such name will become clear later, when we study event properties.
9
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`
10
+
- we'll denote them `pageX/pageY`.
11
11
12
-
When the page is scrolled to the top, so that the window top/left corner 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.
12
+
When the page is scrolled to the top, 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.
13
13
14
-
Here's the picture, the left part is before the scroll, and the right part - after scroll up:
14
+
Here's the picture, the left part is before the scroll, and the right part - after scrolling the page up:
15
15
16
16

17
17
18
18
As the document moved up:
19
-
-`pageY` - document-relative coordinate of the same point is still the same, it's counted from the document top (scrolled out).
20
-
-`clientY` - window-relative coordinate did change (the arrow became shorter), as the same point now became closer to window top.
19
+
-`pageY` - document-relative coordinate of the same point 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.
21
21
22
22
We'll see more examples of window and document coordinates through this chapter.
23
23
24
24
## Element coordinates: getBoundingClientRect
25
25
26
26
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.
27
27
28
-
Main `DomRect` properties:
28
+
Main `DOMRect` properties:
29
29
30
30
-`x/y` -- X/Y-coordinates of the rectangle origin relative to window,
31
31
-`width/height` -- width/height of the rectangle (can be negative).
@@ -36,7 +36,7 @@ Additionally, there are derived properties:
36
36
-`left/right` -- X-coordinate for the left/right edge.
37
37
38
38
```online
39
-
Click the button to see its window coordinates:
39
+
For instance click this button to see its window coordinates:
40
40
41
41
<p><input id="brTest" type="button" value="Get coordinates using button.getBoundingClientRect() for this button" onclick='showRect(this)'/></p>
42
42
@@ -55,42 +55,38 @@ right:${r.right}
55
55
}
56
56
</script>
57
57
58
-
Please, scroll the page and repeat. You will notice that as window-relative button position changes, its window coordinates (`y/top/bottom` if you scroll vertically) change as well.
58
+
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.
59
59
```
60
60
61
-
Here's the picture:
61
+
Here's the picture of `elem.getBoundingClientRect()` output:
62
62
63
63

64
64
65
-
As you can see, derived properties can be easily calculated:
66
-
-`top = y`
65
+
As you can see, `x/y` and `width/height` fully describe the rectangle. Derived properties can be easily calculated:
67
66
-`left = x`
68
-
-`bottom = y + height`
67
+
-`top = y`
69
68
-`right = x + width`
69
+
-`bottom = y + height`
70
+
71
+
Also:
72
+
73
+
- Coordinates may be decimal fractions, such as `10.5`. That's normal, internally browser uses fractions in calculations. We don't have to round them when setting to `style.position.left/top`.
74
+
- 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.
70
75
71
-
**Why derived properties are needed? Why does `top/left` exist if there's `x/y`?**
76
+
```smart header="Why derived properties are needed? Why does `top/left` exist if there's `x/y`?"
72
77
73
78
The reason is that technically it's possible for `width/height` to be negative. A rectangle starts at `(x,y)` and `(width,height)` is actually the direction vector where it goes.
74
79
75
-
That may be useful e.g. for mouse selections, to properly mark selection start and end.
80
+
Negative `width/height`may be useful for directed rectangles, e.g. to represent mouse selections with properly marked start and end.
76
81
77
-
Here's a vectorized example with negative `width` and `height`:
82
+
Here's a rectangle with negative `width` and `height` (e.g. `width=-200`, `height=-100`):
78
83
79
84

80
85
81
86
The rectangle above starts at its bottom-right corner and then spans left/up.
82
87
83
-
As you can see, `left/top` are not `x/y` any more. The formula can be adjusted to cover negative `width/height`. That's simple enough to do, but rarely needed.
84
-
85
-
**All element coordinates are returned with positive width/height.**
86
-
87
-
The main reason why negative rectangles are covered here is to explain the need for derived propeties. Otherwise it would be odd to see `top/left` duplicating `x/y`.
88
-
89
-
Also:
90
-
91
-
- Coordinates may be decimal fractions, such as `10.5`. That's normal, internally browser uses fractions in calculations. We don't have to round them when setting to `style.position.left/top`.
92
-
- 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.
93
-
88
+
As you can see, `left/top` are not `x/y` here. So these are actually not duplicates. The formula can be adjusted to cover negative `width/height`. That's simple enough to do, but rarely needed, as the result of `elem.getBoundingClientRect()` always has positive width/height.
89
+
```
94
90
95
91
```warn header="Internet Explorer and Edge: no support for `x/y`"
96
92
Internet Explorer and Edge don't support `x/y` properties for historical reasons.
Copy file name to clipboardExpand all lines: 2-ui/3-event-details/4-mouse-drag-and-drop/article.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -294,4 +294,4 @@ We can lay a lot on this foundation.
294
294
- We can use event delegation for `mousedown/up`. A large-area event handler that checks `event.target` can manage Drag'n'Drop for hundreds of elements.
295
295
- And so on.
296
296
297
-
There are frameworks that build architecture over it: `DragZone`, `Droppable`, `Draggable` and other classes. Most of them do the similar stuff to described above, so it should be easy to understand them now. Or roll our own, because you already know how to handle the process, and it may be more flexible than to adapt something else.
297
+
There are frameworks that build architecture over it: `DragZone`, `Droppable`, `Draggable` and other classes. Most of them do the similar stuff to described above, so it should be easy to understand them now. Or roll our own, as you can see that's easy enough to do, maybe easier than adapting something else.
0 commit comments