Skip to content

Commit aeb7409

Browse files
committed
up
1 parent ae4deef commit aeb7409

File tree

18 files changed

+191
-224
lines changed

18 files changed

+191
-224
lines changed

2-ui/1-document/08-styles-and-classes/article.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,12 @@ For instance:
250250
```smart header="Computed and resolved values"
251251
There are two concepts in [CSS](https://drafts.csswg.org/cssom/#resolved-values):
252252
253-
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%`.
254-
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`.
253+
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%`.
254+
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`.
255255
256256
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.
257257
258-
So nowadays `getComputedStyle` actually returns the final, resolved value in absolute units.
258+
So nowadays `getComputedStyle` actually returns the resolved value of the property.
259259
```
260260
261261
````warn header="`getComputedStyle` requires the full property name"

2-ui/1-document/09-size-and-scroll/1-get-scroll-height-bottom/solution.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ The solution is:
33
```js
44
let scrollBottom = elem.scrollHeight - elem.scrollTop - elem.clientHeight;
55
```
6+
7+
In other words: (full height) minus (scrolled out top part) minus (visible part) -- that's exactly the scrolled out bottom part.

2-ui/1-document/09-size-and-scroll/2-scrollbar-width/task.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ importance: 3
66

77
Write the code that returns the width of a standard scrollbar.
88

9-
For Windows it usually varies between `12px` and `20px`, but if the browser reserves no space for it, then it may be `0px`.
9+
For Windows it usually varies between `12px` and `20px`. If the browser doesn't reserves any space for it, then it may be `0px`.
1010

11-
P.S. The code should work in any HTML document, do not depend on its content.
11+
P.S. The code should work for any HTML document, do not depend on its content.

2-ui/1-document/09-size-and-scroll/article.md

+22-21
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ They include the content width together with paddings, but without the scrollbar
154154

155155
![](metric-client-width-height.png)
156156

157-
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`.
157+
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`.
158158

159159
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`.
160160

@@ -167,7 +167,7 @@ So when there's no padding we can use `clientWidth/clientHeight` to get the cont
167167
## scrollWidth/Height
168168

169169
- Properties `clientWidth/clientHeight` only account for the visible part of the element.
170-
- Properties `scrollWidth/scrollHeight` add the scrolled out (hidden) part:
170+
- Properties `scrollWidth/scrollHeight` also include the scrolled out (hidden) part:
171171

172172
![](metric-scroll-width-height.png)
173173

@@ -176,14 +176,17 @@ On the picture above:
176176
- `scrollHeight = 723` -- is the full inner height of the content area including the scrolled out part.
177177
- `scrollWidth = 324` -- is the full inner width, here we have no horizontal scroll, so it equals `clientWidth`.
178178

179-
We can use these properties to open the element wide to its full width/height, by the code:
179+
We can use these properties to expand the element wide to its full width/height.
180+
181+
Like this:
180182

181183
```js
184+
// expand the element to the full content height
182185
element.style.height = element.scrollHeight + 'px';
183186
```
184187

185188
```online
186-
Click the button to open wide the element:
189+
Click the button to expand the element:
187190
188191
<div id="element" style="width:300px;height:200px; padding: 0;overflow: auto; border:1px solid black;">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</div>
189192
@@ -192,43 +195,43 @@ Click the button to open wide the element:
192195

193196
## scrollLeft/scrollTop
194197

195-
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.
198+
Properties `scrollLeft/scrollTop` are the width/height of the hidden, scrolled out part of the element.
196199

197-
On the picture below we can see `scrollHeight` and `scrollTop` for a block with a vertical scroll:
200+
On the picture below we can see `scrollHeight` and `scrollTop` for a block with a vertical scroll.
198201

199202
![](metric-scroll-top.png)
200203

204+
In other words, `scrollTop` is "how much is scrolled up".
205+
201206
````smart header="`scrollLeft/scrollTop` can be modified"
202-
Unlike most other geometry properties that are read-only, `scrollLeft/scrollTop` can be changed, and the browser will scroll the element.
207+
Most geometry properties that are read-only, but `scrollLeft/scrollTop` can be changed, and the browser will scroll the element.
203208

204209
```online
205-
If you click the element below, the code `elem.scrollTop += 10` executes. That makes the element content scroll `10px` below.
210+
If you click the element below, the code `elem.scrollTop+=10` executes. That makes the element content scroll `10px` below.
206211
207212
<div onclick="this.scrollTop+=10" style="cursor:pointer;border:1px solid black;width:100px;height:80px;overflow:auto">Click<br>Me<br>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9</div>
208213
```
209214

210-
Setting `scrollTop` to `0` or `Infinity` will make the element scroll to the top/bottom respectively.
215+
Setting `scrollTop` to `0` or `Infinity` will make the element scroll to the very top/bottom respectively.
211216
````
212217
213218
## Don't take width/height from CSS
214219
215-
We've just covered geometry properties of DOM elements. They are normally used to get widths, heights and distances.
220+
We've just covered geometry properties of DOM elements. They are normally used to get widths, heights and calculate distances.
216221
217-
Now let's see what we should not use.
222+
But as we know from the chapter <info:styles-and-classes>, we can read CSS-height and width using `getComputedStyle`.
218223
219-
As we know from the chapter <info:styles-and-classes>, we can read CSS-height and width using `getComputedStyle`.
220-
221-
So we can try to read the width of an element like this:
224+
So why not to read the width of an element like this?
222225
223226
```js run
224227
let elem = document.body;
225228
226229
alert( getComputedStyle(elem).width ); // show CSS width for elem
227230
```
228231
229-
Why we should use geometry properties instead?
232+
Why we should use geometry properties instead? There are two reasons:
230233
231-
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.
234+
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.
232235
2. Second, CSS `width/height` may be `auto`, for instance for an inline element:
233236
234237
```html run
@@ -243,11 +246,9 @@ Why we should use geometry properties instead?
243246
244247
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.
245248
246-
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.
247-
248-
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.
249+
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.
249250
250-
...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.
251+
...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.
251252
252253
```online
253254
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
256257
257258
The element with text has CSS `width:300px`.
258259
259-
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.
260+
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.
260261
```
261262
262263
Please note that the described difference are only about reading `getComputedStyle(...).width` from JavaScript, visually everything is correct.
Loading
Loading
Loading
Loading

2-ui/1-document/10-size-and-scroll-window/article.md

+25-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Window sizes and scroll
22

3-
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?
3+
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?
44

55
From the DOM point of view, the root document element is `document.documentElement`. That element corresponds to `<html>` 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.
66

@@ -13,39 +13,38 @@ Properties `clientWidth/clientHeight` of `document.documentElement` is exactly w
1313
![](document-client-width-height.png)
1414

1515
```online
16-
For instance, the button below shows the height of your window:
16+
For instance, this button shows the height of your window:
17+
1718
<button onclick="alert(document.documentElement.clientHeight)">alert(document.documentElement.clientHeight)</button>
1819
```
1920

2021
````warn header="Not `window.innerWidth/Height`"
2122
Browsers also support properties `window.innerWidth/innerHeight`. They look like what we want. So what's the difference?
2223

23-
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.
24+
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.
2425

2526
And `window.innerWidth/innerHeight` ignore the scrollbar.
2627

27-
If there's a scrollbar and it occupies some space, then these two lines show different values:
28+
If there's a scrollbar, and it occupies some space, then these two lines show different values:
2829
```js run
2930
alert( window.innerWidth ); // full window width
3031
alert( document.documentElement.clientWidth ); // window width minus the scrollbar
3132
```
3233

33-
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`.
34+
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`.
3435
````
3536
3637
```warn header="`DOCTYPE` is important"
3738
Please note: top-level geometry properties may work a little bit differently when there's no `<!DOCTYPE HTML>` in HTML. Odd things are possible.
3839
39-
In modern HTML we should always write it. Generally that's not a JavaScript question, but here it affects JavaScript as well.
40+
In modern HTML we should always write `DOCTYPE`. Generally that's not a JavaScript question, but here it affects JavaScript as well.
4041
```
4142
4243
## Width/height of the document
4344
44-
Theoretically, as the visibble part of the document is `documentElement.clientWidth/Height`, the full size should be `documentElement.scrollWidth/scrollHeight`.
45-
46-
That's correct for regular elements.
45+
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`.
4746
48-
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.
47+
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.
4948
5049
To have a reliable full window size, we should take the maximum of these properties:
5150
@@ -63,11 +62,11 @@ Why so? Better don't ask. These inconsistencies come from ancient times, not a "
6362
6463
## Get the current scroll [#page-scroll]
6564
66-
Regular elements have their current scroll state in `scrollLeft/scrollTop`.
65+
Regular elements have their current scroll state in `elem.scrollLeft/scrollTop`.
6766
68-
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.
67+
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.
6968
70-
Luckily, we don't have to remember that at all, because of the special properties `window.pageXOffset/pageYOffset`:
69+
Luckily, we don't have to remember these pecularities at all, because of the special properties `window.pageXOffset/pageYOffset`:
7170
7271
```js run
7372
alert('Current scroll from the top: ' + window.pageYOffset);
@@ -79,7 +78,9 @@ These properties are read-only.
7978
## Scrolling: scrollTo, scrollBy, scrollIntoView [#window-scroll]
8079
8180
```warn
82-
To scroll the page from JavaScript, its DOM must be fully loaded.
81+
To scroll the page from JavaScript, its DOM must be fully built.
82+
83+
For instance, if we try to scroll the page from the script in `<head>`, it won't work.
8384
```
8485
8586
Regular elements can be scrolled by changing `scrollTop/scrollLeft`.
@@ -88,9 +89,7 @@ We can do the same for the page:
8889
- For all browsers except Chrome/Safari/Opera: modify `document.documentElement.scrollTop/Left`.
8990
- In Chrome/Safari/Opera: use `document.body.scrollTop/Left` instead.
9091
91-
It should work.
92-
93-
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).
92+
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).
9493
9594
- The method `scrollBy(x,y)` scrolls the page relative to its current position. For instance, `scrollBy(0,10)` scrolls the page `10px` down.
9695
@@ -107,16 +106,16 @@ But there's a simpler, more universal solution: special methods [window.scrollB
107106
<button onclick="window.scrollTo(0,0)">window.scrollTo(0,0)</button>
108107
```
109108
110-
These properties are cross-browser.
109+
These methods work for all browsers the same way.
111110
112111
## scrollIntoView
113112
114113
For completeness, let's cover one more method: [elem.scrollIntoView(top)](mdn:api/Element/scrollIntoView).
115114
116-
The call to `elem.scrollIntoView(top)` scrolls the page to make `elem` visible. It has one argument `top`:
115+
The call to `elem.scrollIntoView(top)` scrolls the page to make `elem` visible. It has one argument:
117116
118-
- 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.
119-
- if `top=false`, then the element bottom is aligned with the window bottom.
117+
- 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.
118+
- 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.
120119
121120
```online
122121
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:
130129
131130
## Forbid the scrolling
132131
133-
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.
132+
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.
134133
135134
To make the document unscrollable, its enough to set `document.body.style.overflow = "hidden"`. The page will freeze on its current scroll.
136135
@@ -148,7 +147,7 @@ We can use the same technique to "freeze" the scroll for other elements, not jus
148147
149148
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.
150149
151-
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`.
150+
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.
152151
153152
## Summary
154153
@@ -168,8 +167,8 @@ Geometry:
168167
Scrolling:
169168
170169
- Read the current scroll: `window.pageYOffset/pageXOffset`.
171-
- Scroll the page:
170+
- Change the current scroll:
172171
173172
- `window.scrollTo(pageX,pageY)` -- absolute coordinates,
174-
- `window.scrollBy(x,y)` -- scroll relative the current place?
175-
- `elem.scrollIntoView(top)` -- scroll to make `elem` visible.
173+
- `window.scrollBy(x,y)` -- scroll relative the current place,
174+
- `elem.scrollIntoView(top)` -- scroll to make `elem` visible (align with the top/bottom of the window).
Loading
Loading

0 commit comments

Comments
 (0)