Skip to content

Commit d3ad4c3

Browse files
committed
Fix grammar and construction in 2.1.10
1 parent 524d598 commit d3ad4c3

File tree

1 file changed

+26
-26
lines changed
  • 2-ui/1-document/10-size-and-scroll-window

1 file changed

+26
-26
lines changed

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
How do we find the width and height of the browser window? How do we get the full width and height of the document, including the scrolled out part? How do we scroll the page using JavaScript?
44

5-
For most such requests, we can use the root document element `document.documentElement`, that corresponds to the `<html>` tag. But there are additional methods and peculiarities important enough to consider.
5+
For this type of information, we can use the root document element `document.documentElement`, that corresponds to the `<html>` tag. But there are additional methods and peculiarities to consider.
66

77
## Width/height of the window
88

9-
To get window width and height we can use `clientWidth/clientHeight` of `document.documentElement`:
9+
To get window width and height, we can use the `clientWidth/clientHeight` of `document.documentElement`:
1010

1111
![](document-client-width-height.svg)
1212

@@ -16,20 +16,20 @@ For instance, this button shows the height of your window:
1616
<button onclick="alert(document.documentElement.clientHeight)">alert(document.documentElement.clientHeight)</button>
1717
```
1818

19-
````warn header="Not `window.innerWidth/Height`"
20-
Browsers also support properties `window.innerWidth/innerHeight`. They look like what we want. So why not to use them instead?
19+
````warn header="Not `window.innerWidth/innerHeight`"
20+
Browsers also support properties like `window.innerWidth/innerHeight`. They look like what we want, so why not to use them instead?
2121

22-
If there exists a scrollbar, and it occupies some space, `clientWidth/clientHeight` provide the width/height without it (subtract it). In other words, they return width/height of the visible part of the document, available for the content.
22+
If there exists a scrollbar, and it occupies some space, `clientWidth/clientHeight` provide the width/height without it (subtract it). In other words, they return the width/height of the visible part of the document, available for the content.
2323

24-
...And `window.innerWidth/innerHeight` include the scrollbar.
24+
`window.innerWidth/innerHeight` includes the scrollbar.
2525

2626
If there's a scrollbar, and it occupies some space, then these two lines show different values:
2727
```js run
2828
alert( window.innerWidth ); // full window width
2929
alert( document.documentElement.clientWidth ); // window width minus the scrollbar
3030
```
3131

32-
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`.
32+
In most cases, we need the *available* window width in order to draw or position something within scrollbars (if there are any), so we should use `documentElement.clientHeight/clientWidth`.
3333
````
3434

3535
```warn header="`DOCTYPE` is important"
@@ -40,9 +40,9 @@ In modern HTML we should always write `DOCTYPE`.
4040

4141
## Width/height of the document
4242

43-
Theoretically, as the root document element is `document.documentElement`, and it encloses all the content, we could measure document full size as `document.documentElement.scrollWidth/scrollHeight`.
43+
Theoretically, as the root document element is `document.documentElement`, and it encloses all the content, we could measure the document's full size as `document.documentElement.scrollWidth/scrollHeight`.
4444

45-
But on that element, 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`! Sounds like a nonsense, weird, right?
45+
But on that element, 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`! Weird, right?
4646

4747
To reliably obtain the full document height, we should take the maximum of these properties:
4848

@@ -62,7 +62,7 @@ Why so? Better don't ask. These inconsistencies come from ancient times, not a "
6262

6363
DOM elements have their current scroll state in `elem.scrollLeft/scrollTop`.
6464

65-
For document scroll `document.documentElement.scrollLeft/Top` works in most browsers, except older WebKit-based ones, like Safari (bug [5991](https://bugs.webkit.org/show_bug.cgi?id=5991)), where we should use `document.body` instead of `document.documentElement`.
65+
For document scroll, `document.documentElement.scrollLeft/scrollTop` works in most browsers, except older WebKit-based ones, like Safari (bug [5991](https://bugs.webkit.org/show_bug.cgi?id=5991)), where we should use `document.body` instead of `document.documentElement`.
6666

6767
Luckily, we don't have to remember these peculiarities at all, because the scroll is available in the special properties `window.pageXOffset/pageYOffset`:
6868

@@ -76,16 +76,16 @@ These properties are read-only.
7676
## Scrolling: scrollTo, scrollBy, scrollIntoView [#window-scroll]
7777

7878
```warn
79-
To scroll the page from JavaScript, its DOM must be fully built.
79+
To scroll the page with JavaScript, its DOM must be fully built.
8080

81-
For instance, if we try to scroll the page from the script in `<head>`, it won't work.
81+
For instance, if we try to scroll the page with a script in `<head>`, it won't work.
8282
```
8383

8484
Regular elements can be scrolled by changing `scrollTop/scrollLeft`.
8585

86-
We can do the same for the page using `document.documentElement.scrollTop/Left` (except Safari, where `document.body.scrollTop/Left` should be used instead).
86+
We can do the same for the page using `document.documentElement.scrollTop/scrollLeft` (except Safari, where `document.body.scrollTop/Left` should be used instead).
8787

88-
Alternatively, there's a simpler, universal solution: special methods [window.scrollBy(x,y)](mdn:api/Window/scrollBy) and [window.scrollTo(pageX,pageY)](mdn:api/Window/scrollTo).
88+
Alternatively, there's a simpler, universal solution: special methods [window.scrollBy(x,y)](mdn:api/Window/scrollBy) and [window.scrollTo(pageX,pageY)](mdn:api/Window/scrollTo).
8989

9090
- The method `scrollBy(x,y)` scrolls the page *relative to its current position*. For instance, `scrollBy(0,10)` scrolls the page `10px` down.
9191

@@ -106,28 +106,28 @@ These methods work for all browsers the same way.
106106

107107
## scrollIntoView
108108

109-
For completeness, let's cover one more method: [elem.scrollIntoView(top)](mdn:api/Element/scrollIntoView).
109+
For completeness, let's cover one more method: [elem.scrollIntoView(top)](mdn:api/Element/scrollIntoView).
110110

111111
The call to `elem.scrollIntoView(top)` scrolls the page to make `elem` visible. It has one argument:
112112

113-
- 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.
114-
- 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.
113+
- 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 will be aligned with the window top.
114+
- If `top=false`, then the page scrolls to make `elem` appear at the bottom. The bottom edge of the element will be aligned with the window bottom.
115115

116116
```online
117-
The button below scrolls the page to make itself show at the window top:
117+
The button below scrolls the page to position itself at the window top:
118118

119119
<button onclick="this.scrollIntoView()">this.scrollIntoView()</button>
120120

121-
And this button scrolls the page to show it at the bottom:
121+
And this button scrolls the page to position itself at the bottom:
122122

123123
<button onclick="this.scrollIntoView(false)">this.scrollIntoView(false)</button>
124124
```
125125

126126
## Forbid the scrolling
127127

128-
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.
128+
Sometimes we need to make the document "unscrollable". For instance, when we need to cover the page with a large message requiring immediate attention, and we want the visitor to interact with that message, not with the document.
129129

130-
To make the document unscrollable, it's enough to set `document.body.style.overflow = "hidden"`. The page will freeze on its current scroll.
130+
To make the document unscrollable, it's enough to set `document.body.style.overflow = "hidden"`. The page will "freeze" at its current scroll position.
131131

132132
```online
133133
Try it:
@@ -136,20 +136,20 @@ Try it:
136136

137137
<button onclick="document.body.style.overflow = ''">document.body.style.overflow = ''</button>
138138

139-
The first button freezes the scroll, the second one resumes it.
139+
The first button freezes the scroll, while the second one releases it.
140140
```
141141

142-
We can use the same technique to "freeze" the scroll for other elements, not just for `document.body`.
142+
We can use the same technique to freeze the scroll for other elements, not just for `document.body`.
143143

144-
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.
144+
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.
145145

146-
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 the same.
146+
That looks a bit odd, but can be worked around if we compare `clientWidth` before and after the freeze. If it increased (the scrollbar disappeared), then add `padding` to `document.body` in place of the scrollbar to keep the content width the same.
147147

148148
## Summary
149149

150150
Geometry:
151151

152-
- Width/height of the visible part of the document (content area width/height): `document.documentElement.clientWidth/Height`
152+
- Width/height of the visible part of the document (content area width/height): `document.documentElement.clientWidth/clientHeight`
153153
- Width/height of the whole document, with the scrolled out part:
154154

155155
```js

0 commit comments

Comments
 (0)