up · timzekid/javascript-tutorial-en@aeb7409 · GitHub
Skip to content

Commit aeb7409

Browse files
committed
up
1 parent ae4deef commit aeb7409

18 files changed

Lines changed: 191 additions & 224 deletions

File tree

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

Lines changed: 3 additions & 3 deletions

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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 22 additions & 21 deletions
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.
1.02 KB
Loading
2.01 KB
Loading
-2.15 KB
Loading
-3.1 KB
Loading

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

Lines changed: 25 additions & 26 deletions
-34 Bytes
Loading

0 commit comments

Comments
 (0)