diff --git a/2-ui/1-document/08-styles-and-classes/article.md b/2-ui/1-document/08-styles-and-classes/article.md
index bf0fdf10..9cbb7975 100644
--- a/2-ui/1-document/08-styles-and-classes/article.md
+++ b/2-ui/1-document/08-styles-and-classes/article.md
@@ -250,12 +250,12 @@ For instance:
```smart header="Computed and resolved values"
There are two concepts in [CSS](https://drafts.csswg.org/cssom/#resolved-values):
-1. A *computed* style value is the value after all CSS rules and CSS inheritance is applied, as the result of the CSS cascade. If can look like `width:auto` or `font-size:125%`.
-2. A *resolved* style value is the one finally applied to the element. Values like `auto` or `125%` are still abstract. The browser takes the computed value and makes all units fixed and absolute, for instance: `width:212px` or `font-size:16px`. For geometry properties resolved values may have a floating point, like `width:50.5px`.
+1. A *computed* style value is the value after all CSS rules and CSS inheritance is applied, as the result of the CSS cascade. If can look like `height:1em` or `font-size:125%`.
+2. A *resolved* style value is the one finally applied to the element. Values like `1em` or `125%` are relative. The browser takes the computed value and makes all units fixed and absolute, for instance: `height:20px` or `font-size:16px`. For geometry properties resolved values may have a floating point, like `width:50.5px`.
Long time ago `getComputedStyle` was created to get computed values, but it turned out that resolved values are much more convenient, and the standard changed.
-So nowadays `getComputedStyle` actually returns the final, resolved value in absolute units.
+So nowadays `getComputedStyle` actually returns the resolved value of the property.
```
````warn header="`getComputedStyle` requires the full property name"
diff --git a/2-ui/1-document/09-size-and-scroll/1-get-scroll-height-bottom/solution.md b/2-ui/1-document/09-size-and-scroll/1-get-scroll-height-bottom/solution.md
index f6971382..01e07365 100644
--- a/2-ui/1-document/09-size-and-scroll/1-get-scroll-height-bottom/solution.md
+++ b/2-ui/1-document/09-size-and-scroll/1-get-scroll-height-bottom/solution.md
@@ -3,3 +3,5 @@ The solution is:
```js
let scrollBottom = elem.scrollHeight - elem.scrollTop - elem.clientHeight;
```
+
+In other words: (full height) minus (scrolled out top part) minus (visible part) -- that's exactly the scrolled out bottom part.
diff --git a/2-ui/1-document/09-size-and-scroll/2-scrollbar-width/task.md b/2-ui/1-document/09-size-and-scroll/2-scrollbar-width/task.md
index 392404c8..67787894 100644
--- a/2-ui/1-document/09-size-and-scroll/2-scrollbar-width/task.md
+++ b/2-ui/1-document/09-size-and-scroll/2-scrollbar-width/task.md
@@ -6,6 +6,6 @@ importance: 3
Write the code that returns the width of a standard scrollbar.
-For Windows it usually varies between `12px` and `20px`, but if the browser reserves no space for it, then it may be `0px`.
+For Windows it usually varies between `12px` and `20px`. If the browser doesn't reserves any space for it, then it may be `0px`.
-P.S. The code should work in any HTML document, do not depend on its content.
+P.S. The code should work for any HTML document, do not depend on its content.
diff --git a/2-ui/1-document/09-size-and-scroll/article.md b/2-ui/1-document/09-size-and-scroll/article.md
index 42c2b4e6..3a1b7bd0 100644
--- a/2-ui/1-document/09-size-and-scroll/article.md
+++ b/2-ui/1-document/09-size-and-scroll/article.md
@@ -154,7 +154,7 @@ They include the content width together with paddings, but without the scrollbar

-On the picture above let's first consider `clientHeight`: it's easier to evaluate. There's no horizontal scrollbar, so its exactly the sum of what's inside the borders: CSS-height `200px` plus top and bottom paddings (`2*20px`) total `240px`.
+On the picture above let's first consider `clientHeight`: it's easier to evaluate. There's no horizontal scrollbar, so it's exactly the sum of what's inside the borders: CSS-height `200px` plus top and bottom paddings (`2*20px`) total `240px`.
Now `clientWidth` -- here the content width is not `300px`, but `284px`, because `16px` are occupied by the scrollbbar. So the sum is `284px` plus left and right paddings, total `324px`.
@@ -167,7 +167,7 @@ So when there's no padding we can use `clientWidth/clientHeight` to get the cont
## scrollWidth/Height
- Properties `clientWidth/clientHeight` only account for the visible part of the element.
-- Properties `scrollWidth/scrollHeight` add the scrolled out (hidden) part:
+- Properties `scrollWidth/scrollHeight` also include the scrolled out (hidden) part:

@@ -176,14 +176,17 @@ On the picture above:
- `scrollHeight = 723` -- is the full inner height of the content area including the scrolled out part.
- `scrollWidth = 324` -- is the full inner width, here we have no horizontal scroll, so it equals `clientWidth`.
-We can use these properties to open the element wide to its full width/height, by the code:
+We can use these properties to expand the element wide to its full width/height.
+
+Like this:
```js
+// expand the element to the full content height
element.style.height = element.scrollHeight + 'px';
```
```online
-Click the button to open wide the element:
+Click the button to expand the element:
text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
@@ -192,33 +195,33 @@ Click the button to open wide the element:
## scrollLeft/scrollTop
-Properties `scrollLeft/scrollTop` show how much is hidden behind the scroll. It's the width/height of the hidden, scrolled out part of the element.
+Properties `scrollLeft/scrollTop` are the width/height of the hidden, scrolled out part of the element.
-On the picture below we can see `scrollHeight` and `scrollTop` for a block with a vertical scroll:
+On the picture below we can see `scrollHeight` and `scrollTop` for a block with a vertical scroll.

+In other words, `scrollTop` is "how much is scrolled up".
+
````smart header="`scrollLeft/scrollTop` can be modified"
-Unlike most other geometry properties that are read-only, `scrollLeft/scrollTop` can be changed, and the browser will scroll the element.
+Most geometry properties that are read-only, but `scrollLeft/scrollTop` can be changed, and the browser will scroll the element.
```online
-If you click the element below, the code `elem.scrollTop += 10` executes. That makes the element content scroll `10px` below.
+If you click the element below, the code `elem.scrollTop+=10` executes. That makes the element content scroll `10px` below.
Click Me 1 2 3 4 5 6 7 8 9
```
-Setting `scrollTop` to `0` or `Infinity` will make the element scroll to the top/bottom respectively.
+Setting `scrollTop` to `0` or `Infinity` will make the element scroll to the very top/bottom respectively.
````
## Don't take width/height from CSS
-We've just covered geometry properties of DOM elements. They are normally used to get widths, heights and distances.
+We've just covered geometry properties of DOM elements. They are normally used to get widths, heights and calculate distances.
-Now let's see what we should not use.
+But as we know from the chapter , we can read CSS-height and width using `getComputedStyle`.
-As we know from the chapter , we can read CSS-height and width using `getComputedStyle`.
-
-So we can try to read the width of an element like this:
+So why not to read the width of an element like this?
```js run
let elem = document.body;
@@ -226,9 +229,9 @@ let elem = document.body;
alert( getComputedStyle(elem).width ); // show CSS width for elem
```
-Why we should use geometry properties instead?
+Why we should use geometry properties instead? There are two reasons:
-1. First, CSS `width/height` depend on another property -- `box-sizing` that defines "what is" CSS width and height. A change in `box-sizing` for purposes of CSS may break such JavaScript.
+1. First, CSS width/height depend on another property: `box-sizing` that defines "what is" CSS width and height. A change in `box-sizing` for CSS purposes may break such JavaScript.
2. Second, CSS `width/height` may be `auto`, for instance for an inline element:
```html run
@@ -243,11 +246,9 @@ Why we should use geometry properties instead?
From the CSS standpoint, `width:auto` is perfectly normal, but in JavaScript we need an exact size in `px` that we can use in calculations. So here CSS width is useless at all.
-And there's one more reason. A scrollbar is the reason of many problems. The devil is in the detail. Sometimes the code that works fine without a scrollbar starts to bug with it.
+And there's one more reason: a scrollbar. Sometimes the code that works fine without a scrollbar starts to bug with it, because a scrollbar takes the space from the content in some browsers. So the real width available for the content is *less* than CSS width. And `clientWidth/clientHeight` take that into account.
-As we've seen a scrollbar takes the space from the content in some browsers. So the real width available for the content is *less* than CSS width. And `clientWidth/clientHeight` take that into account.
-
-...But some browsers also take that into account in `getComputedStyle(elem).width`. That is: some of them return real inner width and some of them -- CSS width. Such cross-browser differences is a reason not to use `getComputedStyle`, but rather rely on geometry propeties.
+...But with `getComputedStyle(elem).width` the situation is different. Some browsers (e.g. Chrome) return the real inner width, minus the scrollbar, and some of them (e.g. Firefox) -- CSS width (ignore the scrollbar). Such cross-browser differences is the reason not to use `getComputedStyle`, but rather rely on geometry propeties.
```online
If your browser reserves the space for a scrollbar (most browsers for Windows do), then you can test it below.
@@ -256,7 +257,7 @@ If your browser reserves the space for a scrollbar (most browsers for Windows do
The element with text has CSS `width:300px`.
-Desktop Windows Firefox, Chrome, Edge all reserve the space for the scrollbar. But Firefox shows `300px`, while Chrome and Edge show less. That's because Firefox returns the CSS width and other browsers return the "real" width.
+On a Desktop Windows OS, Firefox, Chrome, Edge all reserve the space for the scrollbar. But Firefox shows `300px`, while Chrome and Edge show less. That's because Firefox returns the CSS width and other browsers return the "real" width.
```
Please note that the described difference are only about reading `getComputedStyle(...).width` from JavaScript, visually everything is correct.
diff --git a/2-ui/1-document/09-size-and-scroll/metric-client-width-height.png b/2-ui/1-document/09-size-and-scroll/metric-client-width-height.png
index 3f9c6dec..ed44652d 100644
Binary files a/2-ui/1-document/09-size-and-scroll/metric-client-width-height.png and b/2-ui/1-document/09-size-and-scroll/metric-client-width-height.png differ
diff --git a/2-ui/1-document/09-size-and-scroll/metric-client-width-height@2x.png b/2-ui/1-document/09-size-and-scroll/metric-client-width-height@2x.png
index 0c61fa6a..f31f483e 100644
Binary files a/2-ui/1-document/09-size-and-scroll/metric-client-width-height@2x.png and b/2-ui/1-document/09-size-and-scroll/metric-client-width-height@2x.png differ
diff --git a/2-ui/1-document/09-size-and-scroll/metric-offset-width-height.png b/2-ui/1-document/09-size-and-scroll/metric-offset-width-height.png
index 4e7821b8..a13075f0 100644
Binary files a/2-ui/1-document/09-size-and-scroll/metric-offset-width-height.png and b/2-ui/1-document/09-size-and-scroll/metric-offset-width-height.png differ
diff --git a/2-ui/1-document/09-size-and-scroll/metric-offset-width-height@2x.png b/2-ui/1-document/09-size-and-scroll/metric-offset-width-height@2x.png
index 370821e7..7bfb24c2 100644
Binary files a/2-ui/1-document/09-size-and-scroll/metric-offset-width-height@2x.png and b/2-ui/1-document/09-size-and-scroll/metric-offset-width-height@2x.png differ
diff --git a/2-ui/1-document/10-size-and-scroll-window/article.md b/2-ui/1-document/10-size-and-scroll-window/article.md
index a3ca5095..1a210133 100644
--- a/2-ui/1-document/10-size-and-scroll-window/article.md
+++ b/2-ui/1-document/10-size-and-scroll-window/article.md
@@ -1,6 +1,6 @@
# Window sizes and scroll
-How to find out the browser window width? How to get the full height of the document, including the scrolled out part? How to scroll the page from JavaScript?
+How to find out the width of the browser window? How to get the full height of the document, including the scrolled out part? How to scroll the page using JavaScript?
From the DOM point of view, the root document element is `document.documentElement`. That element corresponds to `` and has geometry properties described in the [previous chapter](info:size-and-scroll). For some cases we can use it, but there are additional methods and pecularities important enough to consider.
@@ -13,39 +13,38 @@ Properties `clientWidth/clientHeight` of `document.documentElement` is exactly w

```online
-For instance, the button below shows the height of your window:
+For instance, this button shows the height of your window:
+
```
````warn header="Not `window.innerWidth/Height`"
Browsers also support properties `window.innerWidth/innerHeight`. They look like what we want. So what's the difference?
-Properties `clientWidth/clientHeight`, if there's a scrollbar occupying some space, return the width/height inside it. In other words, they return width/height of the visible part of the document, available for the content.
+If there's a scrollbar occupying some space, `clientWidth/clientHeight` provide the width/height inside it. In other words, they return width/height of the visible part of the document, available for the content.
And `window.innerWidth/innerHeight` ignore the scrollbar.
-If there's a scrollbar and it occupies some space, then these two lines show different values:
+If there's a scrollbar, and it occupies some space, then these two lines show different values:
```js run
alert( window.innerWidth ); // full window width
alert( document.documentElement.clientWidth ); // window width minus the scrollbar
```
-In most cases we need the *available* window width: to draw or position something. That is: inside scrollbars if there are any. So we should use `documentElement.clientWidth`.
+In most cases we need the *available* window width: to draw or position something. That is: inside scrollbars if there are any. So we should use `documentElement.clientHeight/Width`.
````
```warn header="`DOCTYPE` is important"
Please note: top-level geometry properties may work a little bit differently when there's no `` in HTML. Odd things are possible.
-In modern HTML we should always write it. Generally that's not a JavaScript question, but here it affects JavaScript as well.
+In modern HTML we should always write `DOCTYPE`. Generally that's not a JavaScript question, but here it affects JavaScript as well.
```
## Width/height of the document
-Theoretically, as the visibble part of the document is `documentElement.clientWidth/Height`, the full size should be `documentElement.scrollWidth/scrollHeight`.
+Theoretically, as the root document element is `documentElement.clientWidth/Height`, and it encloses all the content, we could measure its full size as `documentElement.scrollWidth/scrollHeight`.
-That's correct for regular elements.
-
-But for the whole page these properties do not work as intended. In Chrome/Safari/Opera if there's no scroll, then `documentElement.scrollHeight` may be even less than `documentElement.clientHeight`! For regular elements that's a nonsense.
+These properties work well for regular elements. But for the whole page these properties do not work as intended. In Chrome/Safari/Opera if there's no scroll, then `documentElement.scrollHeight` may be even less than `documentElement.clientHeight`! For regular elements that's a nonsense.
To have a reliable full window size, we should take the maximum of these properties:
@@ -63,11 +62,11 @@ Why so? Better don't ask. These inconsistencies come from ancient times, not a "
## Get the current scroll [#page-scroll]
-Regular elements have their current scroll state in `scrollLeft/scrollTop`.
+Regular elements have their current scroll state in `elem.scrollLeft/scrollTop`.
-What's with the page? Most browsers provide that for the whole page in `documentElement.scrollLeft/Top`, but Chrome/Safari/Opera have bugs (like [157855](https://code.google.com/p/chromium/issues/detail?id=157855), [106133](https://bugs.webkit.org/show_bug.cgi?id=106133)) and we should use `document.body` instead of `document.documentElement` there.
+What's with the page? Most browsers provide `documentElement.scrollLeft/Top` for the document scroll, but Chrome/Safari/Opera have bugs (like [157855](https://code.google.com/p/chromium/issues/detail?id=157855), [106133](https://bugs.webkit.org/show_bug.cgi?id=106133)) and we should use `document.body` instead of `document.documentElement` there.
-Luckily, we don't have to remember that at all, because of the special properties `window.pageXOffset/pageYOffset`:
+Luckily, we don't have to remember these pecularities at all, because of the special properties `window.pageXOffset/pageYOffset`:
```js run
alert('Current scroll from the top: ' + window.pageYOffset);
@@ -79,7 +78,9 @@ These properties are read-only.
## Scrolling: scrollTo, scrollBy, scrollIntoView [#window-scroll]
```warn
-To scroll the page from JavaScript, its DOM must be fully loaded.
+To scroll the page from JavaScript, its DOM must be fully built.
+
+For instance, if we try to scroll the page from the script in ``, it won't work.
```
Regular elements can be scrolled by changing `scrollTop/scrollLeft`.
@@ -88,9 +89,7 @@ We can do the same for the page:
- For all browsers except Chrome/Safari/Opera: modify `document.documentElement.scrollTop/Left`.
- In Chrome/Safari/Opera: use `document.body.scrollTop/Left` instead.
-It should work.
-
-But there's a simpler, more universal solution: special methods [window.scrollBy(x,y)](mdn:api/Window/scrollBy) and [window.scrollTo(pageX,pageY)](mdn:api/Window/scrollTo).
+It should work, but smells like cross-browser incompatibilities. Not good. Fortunately, there's a simpler, more universal solution: special methods [window.scrollBy(x,y)](mdn:api/Window/scrollBy) and [window.scrollTo(pageX,pageY)](mdn:api/Window/scrollTo).
- The method `scrollBy(x,y)` scrolls the page relative to its current position. For instance, `scrollBy(0,10)` scrolls the page `10px` down.
@@ -107,16 +106,16 @@ But there's a simpler, more universal solution: special methods [window.scrollB
```
-These properties are cross-browser.
+These methods work for all browsers the same way.
## scrollIntoView
For completeness, let's cover one more method: [elem.scrollIntoView(top)](mdn:api/Element/scrollIntoView).
-The call to `elem.scrollIntoView(top)` scrolls the page to make `elem` visible. It has one argument `top`:
+The call to `elem.scrollIntoView(top)` scrolls the page to make `elem` visible. It has one argument:
-- if `top=true` (that's the default), then the page will be scrolled to make `elem` appear on the top of the window, upper side of the element aligned with the window top.
-- if `top=false`, then the element bottom is aligned with the window bottom.
+- if `top=true` (that's the default), then the page will be scrolled to make `elem` appear on the top of the window. The upper edge of the element is aligned with the window top.
+- if `top=false`, then the page scrolls to make `elem` appear at the bottom. The bottom edge of the element is aligned with the window bottom.
```online
The button below scrolls the page to make itself show at the window top:
@@ -130,7 +129,7 @@ And this button scrolls the page to show it at the bottom:
## Forbid the scrolling
-Sometimes we need to make the document "unscrollable". For instance, when we need to show a large message over it, and we want the visitor to interact with that message, not with the document.
+Sometimes we need to make the document "unscrollable". For instance, when we need to cover it with a large message requiring immediate attention, and we want the visitor to interact with that message, not with the document.
To make the document unscrollable, its enough to set `document.body.style.overflow = "hidden"`. The page will freeze on its current scroll.
@@ -148,7 +147,7 @@ We can use the same technique to "freeze" the scroll for other elements, not jus
The drawback of the method is that the scrollbar disappears. If it occupied some space, then that space is now free, and the content "jumps" to fill it.
-That looks a bit odd, but can be worked around if we compare `clientWidth` before and after the freeze, and if it increased (the content area became wider) then add `padding` to `document.body`.
+That looks a bit odd, but can be worked around if we compare `clientWidth` before and after the freeze, and if it increased (the scrollbar disappeared) then add `padding` to `document.body` in place of the scrollbar, to keep the content width same.
## Summary
@@ -168,8 +167,8 @@ Geometry:
Scrolling:
- Read the current scroll: `window.pageYOffset/pageXOffset`.
-- Scroll the page:
+- Change the current scroll:
- `window.scrollTo(pageX,pageY)` -- absolute coordinates,
- - `window.scrollBy(x,y)` -- scroll relative the current place?
- - `elem.scrollIntoView(top)` -- scroll to make `elem` visible.
+ - `window.scrollBy(x,y)` -- scroll relative the current place,
+ - `elem.scrollIntoView(top)` -- scroll to make `elem` visible (align with the top/bottom of the window).
diff --git a/2-ui/1-document/10-size-and-scroll-window/document-client-width-height.png b/2-ui/1-document/10-size-and-scroll-window/document-client-width-height.png
index 74643b2b..76a45a7a 100644
Binary files a/2-ui/1-document/10-size-and-scroll-window/document-client-width-height.png and b/2-ui/1-document/10-size-and-scroll-window/document-client-width-height.png differ
diff --git a/2-ui/1-document/10-size-and-scroll-window/document-client-width-height@2x.png b/2-ui/1-document/10-size-and-scroll-window/document-client-width-height@2x.png
index 6935977e..249db0ed 100644
Binary files a/2-ui/1-document/10-size-and-scroll-window/document-client-width-height@2x.png and b/2-ui/1-document/10-size-and-scroll-window/document-client-width-height@2x.png differ
diff --git a/2-ui/1-document/11-coordinates/article.md b/2-ui/1-document/11-coordinates/article.md
index 1c1793d8..907cca2a 100644
--- a/2-ui/1-document/11-coordinates/article.md
+++ b/2-ui/1-document/11-coordinates/article.md
@@ -4,7 +4,7 @@ To move elements around we should be familiar with coordinates.
Most JavaScript methods deal with one of two coordinate systems:
-1. Relative to the window top/left.
+1. Relative to the window(or another viewport) top/left.
2. Relative to the document top/left.
It's important to understand the difference and which type is where.
@@ -27,9 +27,9 @@ Like this:

-Window coordinates do not take the scrolled out part of the document into account, they are calculated from the "window itself".
+Window coordinates do not take the scrolled out part of the document into account, they are calculated from the window left-upper corner.
-In other words, when we scroll the page, the element goes up or down and *its window coordinates change*. That's kind of important.
+In other words, when we scroll the page, the element goes up or down, *its window coordinates change*. That's very important.
```online
Click the button to see its window coordinates:
@@ -48,12 +48,12 @@ If you scroll the page, the button position changes, and window coordinates as w
Also:
-- 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` etc.
-- Coordinates may be negative. For instance, if the page is scrolled down and its top edge is above the window then `elem.getBoundingClientRect().top` is negative.
-- Some browsers also add to the result `getBoundingClientRect` properties `width` and `height`. We could also get the same by the substraction: `height=bottom-top`, `width=right-left`.
+- 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.
+- Coordinates may be negative. For instance, if the page is scrolled down and the `elem` top is now above the window then `elem.getBoundingClientRect().top` is negative.
+- Some browsers (like Chrome) also add to the result `getBoundingClientRect` properties `width` and `height`. We can get them also by substraction: `height=bottom-top`, `width=right-left`.
```warn header="Coordinates right/bottom are different from CSS properties"
-If we compare window coordinates vs CSS positioning, then they are closest to `position:fixed` -- the position relative to the viewport.
+If we compare window coordinates versus CSS positioning, then there are obvious similarities to `position:fixed` -- also the position relative to the viewport.
But in CSS the `right` property means the distance from the right edge, and the `bottom` -- from the bottom edge.
@@ -102,9 +102,11 @@ elem.style.background = ''; // Error!
```
````
-## Usage for position:fixed
+## Using for position:fixed
-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 the coorinates, usually `left/top`.
+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`).
+
+We can use `getBoundingClientRect` to get coordinates of an element, and then to show something near it.
For instance, the function `createMessageUnder(elem, html)` below shows the message under `elem`:
@@ -143,21 +145,21 @@ Click the button to run it:
```
-The code can be modified to show the message at the left, right, below, apply CSS animations to "fade it in" and so on.
+The code can be modified to show the message at the left, right, below, apply CSS animations to "fade it in" and so on. That's easy, as we have all the coordinates and sizes of the element.
-**But note the important detail: when the page is scrolled, the message flows away from the button.**
+But note the important detail: when the page is scrolled, the message flows away from the button.
-The reason is obvious: the message element uses `position: fixed`, so it remains at the same place while the page scrolls away.
+The reason is obvious: the message element relies on `position:fixed`, so it remains at the same place of the window while the page scrolls away.
-To change that, we need to use document-based coordinates. We'll cover them in the next chapter.
+To change that, we need to use document-based coordinates and `position:absolute`.
## Document coordinates
Document-relative coordinates start from the left-upper corner of the document, not the window.
-In CSS, window coordinates correspond to `position:fixed`, while document coordinates are similar to `position:absolute` (out of other positioned elements, of course).
+In CSS, window coordinates correspond to `position:fixed`, while document coordinates are similar to `position:absolute` on top.
-We need document coordinates to stick something at a certain place of the document, so that it remains there during a page scroll.
+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.
For clarity we'll call window coordinates `(clientX,clientY)` and document coordinates `(pageX,pageY)`.
@@ -167,25 +169,26 @@ When the page is not scrolled, then window coordinate and document coordinates a
And if we scroll it, then `(clientX,clientY)` change, because they are relative to the window, but `(pageX,pageY)` remain the same.
-Here's the same page after the vertical scroll.
-
-- `clientY` becomes `0`, because the element is now on window top.
-- `clientX` doesn't change, we didn't scroll horizontally.
-- `pageX` and `pageY` remain the same, because they are relative to the *document*.
+Here's the same page after the vertical scroll:

+- `clientY` of the header `"From today's featured article"` became `0`, because the element is now on window top.
+- `clientX` didn't change, as we didn't scroll horizontally.
+- `pageX` and `pageY` coordinates of the element are still the same, because they are relative to the document.
+
## Getting document coordinates [#getCoords]
-There's no standard method to get document coordinates. But it's easy to write it.
+There's no standard method to get document coordinates of an element. But it's easy to write it.
The two coordinate systems are connected by the formula:
-- `pageY = clientY + current vertical scroll`.
-- `pageX = clientX + current horizontal scroll`.
+- `pageY` = `clientY` + height of the scrolled-out vertical part of the document.
+- `pageX` = `clientX` + width of the scrolled-out horizontal part of the document.
-Our function `getCoords(elem)` will take window coordinates from `elem.getBoundingClientRect()` and add the current scroll to them.
+The function `getCoords(elem)` will take window coordinates from `elem.getBoundingClientRect()` and add the current scroll to them:
```js
+// get document coordinates of the element
function getCoords(elem) {
let box = elem.getBoundingClientRect();
@@ -201,8 +204,8 @@ function getCoords(elem) {
Any point on the page has coordinates:
1. Relative to the window -- `elem.getBoundingClientRect()`.
-2. Relative to the document -- `elem.getBoundingClientRect()` plus the current scroll.
+2. Relative to the document -- `elem.getBoundingClientRect()` plus the current page scroll.
Window coordinates are great to use with `position:fixed`, and document coordinates do well with `position:absolute`.
-Both coordinate systems have their "pro" and "contra", there are times we need one or the other one, just like CSS `position`.
+Both coordinate systems have their "pro" and "contra", there are times we need one or the other one, just like CSS `position` `absolute` and `fixed`.
diff --git a/2-ui/3-event-details/10-onload-ondomcontentloaded/article.md b/2-ui/3-event-details/10-onload-ondomcontentloaded/article.md
index 7e096984..6ee20af7 100644
--- a/2-ui/3-event-details/10-onload-ondomcontentloaded/article.md
+++ b/2-ui/3-event-details/10-onload-ondomcontentloaded/article.md
@@ -1,16 +1,16 @@
-# Page loading: DOMContentLoaded, load, beforeunload, unload [todo: where put async defer scripts? in DOM?]
+# Page lifecycle: DOMContentLoaded, load, beforeunload, unload
-The process of loading an HTML document may be split into three stages:
+The lifecycle of an HTML page has three important events:
-- `DOMContentLoaded` -- the browser fully loaded HTML and built DOM.
+- `DOMContentLoaded` -- the browser fully loaded HTML, and the DOM tree is built, but external resources like pictures `` and stylesheets may be not yet loaded.
- `load` -- the browser loaded all resources (images, styles etc).
-- `beforeunload/unload` -- leaving the page.
+- `beforeunload/unload` -- when the user is leaving the page.
-We can set a handler on every stage:
+Each event may be useful:
-- `DOMContentLoaded` event -- DOM is ready, we can lookup DOM nodes, initialize the interface. But images and styles may be not yet loaded.
-- `load` event -- the page and additional resources are loaded, it's rarely used, because usually we don't want to wait for that moment.
-- `beforeunload/unload` event -- we can check if the user saved changes he did in the page, ask him whether he's sure.
+- `DOMContentLoaded` event -- DOM is ready, so the handler can lookup DOM nodes, initialize the interface.
+- `load` event -- additional resources are loaded, we can get image sizes (if not specified in HTML/CSS) etc.
+- `beforeunload/unload` event -- the user is leaving: we can check if the user saved the changes and ask him whether he really wants to leave.
Let's explore the details of these events.
@@ -28,11 +28,13 @@ document.addEventListener("DOMContentLoaded", ready);
For instance:
-```html run height=150
+```html run height=200 refresh
-
+
```
In the example the `DOMContentLoaded` handler runs when the document is loaded, not waits for the page load. So `alert` shows zero sizes.
@@ -49,168 +51,113 @@ At the first sight `DOMContentLoaded` event is very simple. The DOM tree is read
### DOMContentLoaded and scripts
-If there are `` tags in the document, then the browser must execute them "at place" while building DOM.
+When the browser initially loads HTML and comes across a `` in the text, it can't continue building DOM. It must execute the script right now. So `DOMContentLoaded` may only happen after all such scripts are executed.
-External scripts `
```
-Такое поведение прописано в стандарте. Его причина -- скрипт может захотеть получить информацию со страницы, зависящую от стилей, например, ширину элемента, и поэтому обязан дождаться загрузки `style.css`.
+The reason is that the script may want to get coordinates and other style-dependent properties of elements, like in the example above. Naturally, it has to wait for styles to load.
-**Побочный эффект -- так как событие `DOMContentLoaded` будет ждать выполнения скрипта, то оно подождёт и загрузки стилей, которые идут перед `
-
-
+
+
```
## window.onunload
-Когда человек уходит со страницы или закрывает окно, на `window` срабатывает событие `unload`. В нём можно сделать что-то, не требующее ожидания, например, закрыть вспомогательные popup-окна, но отменить сам переход нельзя.
+When a visitor leaves the page, the `unload` event triggers on `window`. We can do something there that doesn't involve a delay, like closing related popup windows. But we can't cancel the transition to another page.
-Это позволяет другое событие -- `onbeforeunload`, которое поэтому используется гораздо чаще.
+For that we should use another event -- `onbeforeunload`.
## window.onbeforeunload [#window.onbeforeunload]
-Если посетитель инициировал переход на другую страницу или нажал "закрыть окно", то обработчик `onbeforeunload` может приостановить процесс и спросить подтверждение.
+If a visitor initiated leaving the page or tries to close the window, the `beforeunload` handler can ask for additional confirmation.
-Для этого ему нужно вернуть строку, которую браузеры покажут посетителю, спрашивая -- нужно ли переходить.
+It needs to return the string with the question. The browser will show it.
-Например:
+For instance:
```js
window.onbeforeunload = function() {
- return "Данные не сохранены. Точно перейти?";
+ return "There are unsaved changes. Leave now?";
};
```
-```warn header="Firefox игнорирует текст, он показывает своё сообщение"
-Firefox игнорирует текст, а всегда показывает своё сообщение. Это сделано в целях большей безопасности посетителя, чтобы его нельзя было ввести в заблуждение сообщением.
-```
-
```online
-Кликните на кнопку в `IFRAME'е` ниже, чтобы поставить обработчик, а затем по ссылке, чтобы увидеть его в действии:
+Click on the button in `