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 01e0736552..e4c3e0228a 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
@@ -1,7 +1,7 @@
-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/1-get-scroll-height-bottom/task.md b/2-ui/1-document/09-size-and-scroll/1-get-scroll-height-bottom/task.md
index b0f1f22140..afe70d13c6 100644
--- a/2-ui/1-document/09-size-and-scroll/1-get-scroll-height-bottom/task.md
+++ b/2-ui/1-document/09-size-and-scroll/1-get-scroll-height-bottom/task.md
@@ -2,10 +2,10 @@ importance: 5
---
-# What's the scroll from the bottom?
+# 相对于底部滚动了多少?
-The `elem.scrollTop` property is the size of the scrolled out part from the top. How to get "`scrollBottom`" -- the size from the bottom?
+ `elem.scrollTop` 属性是从顶部滚动出来的部分的大小,何获得 `scrollBottom` —— 从底部滚动的尺寸?
-Write the code that works for an arbitrary `elem`.
+编写适用于任意元素的代码。
-P.S. Please check your code: if there's no scroll or the element is fully scrolled down, then it should return `0`.
+P.S. 请检查您的代码:如果没有滚动条或元素完全向下滚动,那么它应该返回 “0”。
diff --git a/2-ui/1-document/09-size-and-scroll/2-scrollbar-width/solution.md b/2-ui/1-document/09-size-and-scroll/2-scrollbar-width/solution.md
index 1ba1e5e576..64891032df 100644
--- a/2-ui/1-document/09-size-and-scroll/2-scrollbar-width/solution.md
+++ b/2-ui/1-document/09-size-and-scroll/2-scrollbar-width/solution.md
@@ -1,16 +1,16 @@
-To get the scrollbar width, we can create an element with the scroll, but without borders and paddings.
+为了获得滚动条宽度,我们可以创建一个带有滚动条的元素,但是没有边框和内间距。
-Then the difference between its full width `offsetWidth` and the inner content area width `clientWidth` will be exactly the scrollbar:
+然后,它的完全宽度 `offsetWidth` 和内容宽度 `clientWidth` 之间的差就是滚动条的宽度:
```js run
-// create a div with the scroll
+// 创建一个包含滚动条的块
let div = document.createElement('div');
div.style.overflowY = 'scroll';
div.style.width = '50px';
div.style.height = '50px';
-// must put it in the document, otherwise sizes will be 0
+// 必须添加到文档中,否则尺寸为 0
document.body.append(div);
let scrollWidth = div.offsetWidth - div.clientWidth;
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 67787894a4..82ba162df7 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
@@ -2,10 +2,10 @@ importance: 3
---
-# What is the scrollbar width?
+# 怎样计算滚动条宽度?
-Write the code that returns the width of a standard scrollbar.
+编写返回标准滚动条宽度的代码。
-For Windows it usually varies between `12px` and `20px`. If the browser doesn't reserves any space for it, then it may be `0px`.
+对于Windows,它通常在 `12px` 和 `20px` 之间变化。如果浏览器不为它保留任何空间,那么它可能是 `0px`。
-P.S. The code should work for any HTML document, do not depend on its content.
+P.S. 代码应该适用于任何HTML文档,且不依赖于它的内容元素。
diff --git a/2-ui/1-document/09-size-and-scroll/4-put-ball-in-center/solution.md b/2-ui/1-document/09-size-and-scroll/4-put-ball-in-center/solution.md
index 69dec23ce8..87a501baac 100644
--- a/2-ui/1-document/09-size-and-scroll/4-put-ball-in-center/solution.md
+++ b/2-ui/1-document/09-size-and-scroll/4-put-ball-in-center/solution.md
@@ -1,50 +1,50 @@
-The ball has `position:absolute`. It means that its `left/top` coordinates are measured from the nearest positioned element, that is `#field` (because it has `position:relative`).
+为小球设置 `position:absolute`。这意味着它的 `left/top` 坐标根据相对它最近的并且设置了定位的元素来测量,这个元素的有效范围就是 `#field`(因为它有 `position:relative`)。
-The coordinates start from the inner left-upper corner of the field:
+坐标从设置了相对定位的最近的元素的左上角开始:

-The inner field width/height is `clientWidth/clientHeight`. So the field center has coordinates `(clientWidth/2, clientHeight/2)`.
+该元素内容区域的宽/高是 `clientWidth/clientHeight`,所以该元素有效范围的中心的坐标为 `(clientWidth/2, clientHeight/2)`。
-...But if we set `ball.style.left/top` to such values, then not the ball as a whole, but the left-upper edge of the ball would be in the center:
+...不过如果我们将 `ball.style.left/top` 设置为上面计算出的值,那么最终的效果与有效范围的中心重合的不是小球的中心,而是小球左上角的坐标:
```js
ball.style.left = Math.round(field.clientWidth / 2) + 'px';
ball.style.top = Math.round(field.clientHeight / 2) + 'px';
```
-Here's how it looks:
+这是它将显示出的效果:
[iframe height=180 src="ball-half"]
-To align the ball center with the center of the field, we should move the ball to the half of its width to the left and to the half of its height to the top:
+为了把球中心和有效范围中心对准,我们应该把球移到左边宽度的一半,把 top 设置为有效范围高度的一半:
```js
ball.style.left = Math.round(field.clientWidth / 2 - ball.offsetWidth / 2) + 'px';
ball.style.top = Math.round(field.clientHeight / 2 - ball.offsetHeight / 2) + 'px';
```
-**Attention: the pitfall!**
+**注意:陷阱!**
-The code won't work reliably while `
` has no width/height:
+如果 `
` 没有宽/高,代码将无法正常工作:
```html
```
-When the browser does not know the width/height of an image (from tag attributes or CSS), then it assumes them to equal `0` until the image finishes loading.
+当浏览器还不知道图片的宽/高(图片的尺寸可能来自标签属性或 CSS)的时候它会假设它们的尺寸为 `0`直到图片加载完成。
-In real life after the first load browser usually caches the image, and on next loads it will have the size immediately.
+实际使用过程中,浏览器会在图片第一次加载完成后缓存该图片,方便下次再次访问时立即显示图片。
-But on the first load the value of `ball.offsetWidth` is `0`. That leads to wrong coordinates.
+但是在第一次加载时 `ball.offsetWidth` 的值为 `0`,这会导致错误的坐标出现。
-We should fix that by adding `width/height` to `
`:
+此时我们应该为 `
` 添加 `width/height` 属性:
```html
```
-...Or provide the size in CSS:
+...或者在 CSS 中提供尺寸:
```css
#ball {
diff --git a/2-ui/1-document/09-size-and-scroll/4-put-ball-in-center/task.md b/2-ui/1-document/09-size-and-scroll/4-put-ball-in-center/task.md
index 57746d9217..d46e982d62 100644
--- a/2-ui/1-document/09-size-and-scroll/4-put-ball-in-center/task.md
+++ b/2-ui/1-document/09-size-and-scroll/4-put-ball-in-center/task.md
@@ -2,19 +2,19 @@ importance: 5
---
-# Place the ball in the field center
+# 将小球置于有效范围的中心
-Here's how the source document looks:
+这是源文件:
[iframe src="source" edit link height=180]
-What are coordinates of the field center?
+有效范围的中心坐标是多少?
-Calculate them and use to place the ball into the center of the field:
+计算并将小球置于有效范围中心:
[iframe src="solution" height=180]
-- The element should be moved by JavaScript, not CSS.
-- The code should work with any ball size (`10`, `20`, `30` pixels) and any field size, not be bound to the given values.
+- 元素应该通过 JavaScript 移除,而不是 CSS。
+- 代码应该在不限制小球尺寸(`10`、`20`、`30` 像素)或任何有效范围内的尺寸的情况下正常运行,而不是直接为元素绑定尺寸。
-P.S. Sure, centering could be done with CSS, but here we want exactly JavaScript. Further we'll meet other topics and more complex situations when JavaScript must be used. Here we do a "warm-up".
+P.S. 当然了,置中的操作可以通过 CSS 完成,但是这里我们需要通过 JavaScript 完成。此外,当必须使用 JavaScript 时,我们可能会遇到其他话题或更加复杂的情况,这里我们只是做一个 “热身”。
diff --git a/2-ui/1-document/09-size-and-scroll/6-width-vs-clientwidth/solution.md b/2-ui/1-document/09-size-and-scroll/6-width-vs-clientwidth/solution.md
index a011d5a727..2e3860fc85 100644
--- a/2-ui/1-document/09-size-and-scroll/6-width-vs-clientwidth/solution.md
+++ b/2-ui/1-document/09-size-and-scroll/6-width-vs-clientwidth/solution.md
@@ -1,6 +1,6 @@
-Differences:
+不同点:
-1. `clientWidth` is numeric, while `getComputedStyle(elem).width` returns a string with `px` at the end.
-2. `getComputedStyle` may return non-numeric width like `"auto"` for an inline element.
-3. `clientWidth` is the inner content area of the element plus paddings, while CSS width (with standard `box-sizing`) is the inner conand sometent area *without paddings*.
-4. If there's a scrollbar and the browser reserves the space for it, some browser substract that space from CSS width (cause it's not available for content any more), and some do not. The `clientWidth` property is always the same: scrollbar size is substracted if reserved.
+1. `clientWidth` 值是数值,然而 `getComputedStyle(elem).width` 返回一个包含 `px` 的字符串。
+2. `getComputedStyle` 可能返回非数值的结果,例如内联元素的 `"auto"`。
+3. `clientWidth` 是元素的内部内容区域加上内间距,而 CSS 宽度(具有标准的 `box-sizing`)是内部**不包括内间距**的空间区域。
+4. 如果有一个滚动条,一般浏览器保留它的空间,有的浏览器从 CSS 宽度中减去这个空间(因为它不再用于内容),而有些则不这样做。`clientWidth` 属性总是相同的:如果保留了滚动条,那么它的宽度将被删去。
diff --git a/2-ui/1-document/09-size-and-scroll/6-width-vs-clientwidth/task.md b/2-ui/1-document/09-size-and-scroll/6-width-vs-clientwidth/task.md
index 5378c5915b..30a5de8497 100644
--- a/2-ui/1-document/09-size-and-scroll/6-width-vs-clientwidth/task.md
+++ b/2-ui/1-document/09-size-and-scroll/6-width-vs-clientwidth/task.md
@@ -2,8 +2,8 @@ importance: 5
---
-# The difference: CSS width versus clientWidth
+# 不同点:CSS 宽度与 clientWidth
-What's the difference between `getComputedStyle(elem).width` and `elem.clientWidth`?
+`getComputedStyle(elem).width` 与 `elem.clientWidth` 之间的差别在哪?
-Give at least 3 differences. The more the better.
+指出至少三种不同点,越多越好。
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 4fadba814e..6c553af3d5 100644
--- a/2-ui/1-document/09-size-and-scroll/article.md
+++ b/2-ui/1-document/09-size-and-scroll/article.md
@@ -1,13 +1,13 @@
-# Element size and scrolling
+# 元素的尺寸与滚动
-There are many JavaScript properties that allow us to read information about element width, height and other geometry features.
+JavaScript 中存在许多属性让我们能够读取元素的宽度、高度或其他具有几何特征的信息。
-We often need them when moving or positioning elements in JavaScript, to correctly calculate coordinates.
+在移动或定位 JavaScript 元素时,我们经常需要它们来计算元素的坐标。
-## Sample element
+## 示例元素
-As a sample element to demonstrate properties we'll use the one given below:
+作为演示属性的示例元素,我们将使用下面给出的一个元素:
```html no-beautify
@@ -24,49 +24,49 @@ As a sample element to demonstrate properties we'll use the one given below:
```
-It has the border, padding and scrolling. The full set of features. There are no margins, as they are not the part of the element itself, and there are no special properties for them.
+它有边框、内填充和滚动条等全部特征属性,但是这不包括外边距,因为它不是元素本身的一部分,并且它们没什么特殊的属性。
-The element looks like this:
+元素就像这样:

-You can [open the document in the sandbox](sandbox:metric).
+你可以 [在sandbox中打开文档](sandbox:metric).
-```smart header="Mind the scrollbar"
-The picture above demonstrates the most complex case when the element has a scrollbar. Some browsers (not all) reserve the space for it by taking it from the content.
+```smart header="注意滚动条"
+上例图片演示了当元素有滚动条时最复杂的情况,一些浏览器(不是全部)通过牺牲内容空间来保留滚动条。
-So, without scrollbar the content width would be `300px`, but if the scrollbar is `16px` wide (the width may vary between devices and browsers) then only `300 - 16 = 284px` remains, and we should take it into account. That's why examples from this chapter assume that there's a scrollbar. If there's no scrollbar, then things are just a bit simpler.
+因此,没有滚动条时,内容宽度将是 `300 px`,但是如果滚动条宽度是 `16px`(不同的设备和浏览器,宽度可能会不一样),那么还剩下 `300 - 16=280px`,这是我们必须考虑到的事。这就是为什么本章的例子总是假设有滚动条的原因。如果没有滚动条,那么事情就会更简单一些。
```
-```smart header="The `padding-bottom` may be filled with text"
-Usually paddings are shown empty on illustrations, but if there's a lot of text in the element and it overflows, then browsers show the "overflowing" text at `padding-bottom`, so you can see that in examples. But the padding is still there, unless specified otherwise.
+```smart header="文本可能会溢出到 `padding-bottom` 中"
+内填充通常在插图中显示的是空的,但是如果元素中有很多文本,并且溢出,那么浏览器在 `padding-bottom` 中显示“溢出”文本,这可以在示例中看到。但是填充物仍然存在,除非另有说明。
```
-## Geometry
+## 几何学
-Element properties that provide width, height and other geometry are always numbers. They are assumed to be in pixels.
+提供宽度、高度和其他几何形状的元素属性通过数值来计算。它们被假定为像素。
-Here's the overall picture:
+以下是显示总体情况的图片:

-They are many properties, it's difficult to fit them all in the single picture, but their values are simple and easy to understand.
+它们有很多属性,很难将它们全部放在单个图片中,但是它们的值很简单,容易理解。
-Let's start exploring them from the outside of the element.
+让我们从元素的外部开始探索它们。
## offsetParent, offsetLeft/Top
-These properties are rarely needed, but still they are the "most outer" geometry properties, so we'll start with them.
+这些属性很少出现,但它们仍然是“最外层”的几何属性,所以我们将从它们开始。
-The `offsetParent` is the nearest ancestor that is:
+ `offsetParent` 是最近的祖先元素:
-1. CSS-positioned (`position` is `absolute`, `relative` or `fixed`),
-2. or `
`, ` | `, ``,
-2. or ``.
+1. CSS 定位(`position` 为 `absolute`、`relative` 或 `fixed`),
+2. 或者 `| `、` | `、``,
+2. 或者 ``。
-In most practical cases we can use `offsetParent` to get the nearest CSS-positioned ancestor. And `offsetLeft/offsetTop` provide x/y coordinates relative to it's left-upper corner.
+在大多数实际情况下,我们可以使用 `offsetParent` 来获得最近的 CSS 定位祖先。`offsetLeft/offsetTop` 提供相对于元素左上角的 x/y 坐标。
-In the example below the inner `` has ` ` as `offsetParent` and `offsetLeft/offsetTop` shifts from its left-upper corner (`180`):
+在下面的例子中,内部 `` 有 ` ` 作为 `offsetParent`,并且 `offsetLeft/offsetTop` 让它从左上角位移(`180`):
```html run height=10
@@ -84,33 +84,33 @@ In the example below the inner `` has ` ` as `offsetParent` and `offse

-There are several occasions when `offsetParent` is `null`:
+有以下几种情况 `offsetParent` 的值为 `null`:
-1. For not shown elements (`display:none` or not in the document).
-2. For `` and ``.
-3. For elements with `position:fixed`.
+1. 未显示的元素(`display:none` 或者不在文档中)。
+2. `` 与 ``。
+3. `position:fixed` 的元素。
## offsetWidth/Height
-Now let's move on to the element itself.
+现在让我们关注元素本身。
-These two properties are the simplest ones. They provide the "outer" width/height of the element. Or, in other words, its full size including borders.
+这两个属性是最简单的。它们提供元素的“外部”宽度/高度。换句话说,它的完整大小包括边框。

-For our sample element:
+在我们的示例元素中:
-- `offsetWidth = 390` -- the outer width, can be calculated as inner CSS-width (`300px`) plus paddings (`2 * 20px`) and borders (`2 * 25px`).
-- `offsetHeight = 290` -- the outer height.
+- `offsetWidth = 390` — 外部宽度,计算方法是内部 css 宽度(`300px`)加上内填充(`2 * 20px`)和边框宽度(`2 * 25px`)。
+- `offsetHeight = 290` — 外部高度。
-````smart header="Geometry properties for not shown elements are zero/null"
-Geometry properties are calculated only for shown elements.
+````smart header="未显示的几何元素的属性值为 0/null"
+几何属性仅为显示出来的元素计算。
-If an element (or any of its ancestors) has `display:none` or is not in the document, then all geometry properties are zero or `null` depending on what it is.
+如果元素(或其任何祖先)在文档中显示为 `display:none` 或本身不在文档中,则所有几何属性都是 0 或者值为 `null`,这取决于它是什么。
-For example, `offsetParent` is `null`, and `offsetWidth`, `offsetHeight` are `0`.
+例如,`offsetParent` 为 `null`,并且 `offsetWidth`,`offsetHeight` 为 `0`。
-We can use this to check if an element is hidden, like this:
+我们可以用它来检查一个元素是否被隐藏,像这样:
```js
function isHidden(elem) {
@@ -118,73 +118,73 @@ function isHidden(elem) {
}
```
-Please note that such `isHidden` returns `true` for elements that are on-screen, but have zero sizes (like an empty ``).
+请注意,`isHidden` 返回 `true` 表示元素不在页面中显示,但不代表尺寸为 0(like an empty ` `)。
````
## clientTop/Left
-Inside the element we have the borders.
+在元素内部,我们有边框。
-To measure them, there are properties `clientTop` and `clientLeft`.
+要测量它们,可使用 `clientTop` 和 `clientLeft`。
-In our example:
+在例子中:
-- `clientLeft = 25` -- left border width
-- `clientTop = 25` -- top border width
+- `clientLeft = 25` — 左边框宽度
+- `clientTop = 25` — 上边框宽度

-...But to be precise -- they are not borders, but relative coordinates of the inner side from the outer side.
+...但确切地说,它们不是边框,而是内侧与外侧的相对坐标。
-What's the difference?
+有什么区别?
-It becomes visible when the document is right-to-left (the operating system is in Arabic or Hebrew languages). The scrollbar is then not on the right, but on the left, and then `clientLeft` also includes the scrollbar width.
+当文档是从右向左渲染时就会显现出来(操作系统是阿拉伯语或希伯来语)。此时滚动条不在右边,而是在左边,而 `clientLeft` 则包含了滚动条的宽度。
-In that case, `clientLeft` would be not `25`, but with the scrollbar width `25 + 16 = 41`:
+在这种情况下,`clientLeft` 的值将不是 `25`,而是加上滚动条的宽度 `25 + 16 = 41`:

## clientWidth/Height
-These properties provide the size of the area inside the element borders.
+这些属性提供元素边框内区域的大小。
-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 it's exactly the sum of what's inside the borders: CSS-height `200px` plus top and bottom paddings (`2 * 20px`) total `240px`.
+在上面的图片中,我们首先考虑 `clientHeight`:这很容易计算。没有水平滚动条,所以它正好是边界内的总和:CSS 高度 `200px` 加上顶部和底部的内填充宽度(`2 * 20px`),总计 `240px`。
-Now `clientWidth` -- here the content width is not `300px`, but `284px`, because `16px` are occupied by the scrollbar. So the sum is `284px` plus left and right paddings, total `324px`.
+这种情况下的 `clientWidth` — 这里的内容宽度不是 `300px`,而是`284px`,因为 `16px` 是滚动条的宽度。所以加起来就是 `284px` 加上左右内间距,总和 `324px`。
-**If there are no paddings, then `clientWidth/Height` is exactly the content area, inside the borders and the scrollbar (if any).**
+**如果没有内间距,那么 `clientWidth/Height` 就是代表内容的宽度, 这里的内容指的是内间距和滚动条以内 (如果还有其他的)。**

-So when there's no padding we can use `clientWidth/clientHeight` to get the content area size.
+因此,当没有内填充时,我们可以使用 `clientWidth/clientHeight` 来获取内容区域大小。
## scrollWidth/Height
-- Properties `clientWidth/clientHeight` only account for the visible part of the element.
-- Properties `scrollWidth/scrollHeight` also include the scrolled out (hidden) parts:
+- 属性 `clientWidth/clientHeight` 只考虑元素的可见部分。
+- 属性 `scrollWidth/scrollHeight` 还包括滚动(隐藏)部分:

-On the picture above:
+上图:
-- `scrollHeight = 723` -- is the full inner height of the content area including the scrolled out parts.
-- `scrollWidth = 324` -- is the full inner width, here we have no horizontal scroll, so it equals `clientWidth`.
+- `scrollHeight = 723` — 是内容区域的完整内部高度,包括滚动部分。
+- `scrollWidth = 324` — 是完整的内部宽度,这里没有水平滚动条,所以它等于 `clientWidth`。
-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 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
@@ -193,33 +193,33 @@ Click the button to expand the element:
## scrollLeft/scrollTop
-Properties `scrollLeft/scrollTop` are the width/height of the hidden, scrolled out part of the element.
+属性 `scrollLeft/scrollTop` 是元素隐藏、滚动部分的宽度/高度。
-On the picture below we can see `scrollHeight` and `scrollTop` for a block with a vertical scroll.
+下面的图片我们可以看到 `scrollHeight` 和 `scrollTop` 是一个垂直滚动块的属性。

-In other words, `scrollTop` is "how much is scrolled up".
+换种说法,`scrollTop` 就是 “滚动了多少” 的意思。
-````smart header="`scrollLeft/scrollTop` can be modified"
-Most geometry properties that are read-only, but `scrollLeft/scrollTop` can be changed, and the browser will scroll the element.
+````smart header="`scrollLeft/scrollTop` 可修改 "
+大多数几何属性是只读的,但是 `scrollLeft/scrollTop` 可以改变,浏览器将会直接滚动元素。
```online
-If you click the element below, the code `elem.scrollTop += 10` executes. That makes the element content scroll `10px` down.
+如果单击下面的元素,代码 `elem.scrollTop += 10` 将会执行,这使得元素向下滚动 `10px`。
Click Me 1 2 3 4 5 6 7 8 9
```
-Setting `scrollTop` to `0` or `Infinity` will make the element scroll to the very top/bottom respectively.
+将 `scrollTop` 设置为 `0` 或 `Infinity` 将使元素分别滚动到顶部/底部。
````
-## Don't take width/height from CSS
+## 不要从 CSS 中获取宽高
-We've just covered geometry properties of DOM elements. They are normally used to get widths, heights and calculate distances.
+我们刚刚介绍了 DOM 元素的几何属性。它们通常用于获得宽度、高度和计算距离。
-But as we know from the chapter , we can read CSS-height and width using `getComputedStyle`.
+但是,正如我们从《信息:样式和类》一章所知道的,我们可以使用 `getComputedStyle` 来读取CSS的高度和宽度。
-So why not to read the width of an element like this?
+那么为什么不像这样读取一个元素的高度呢?
```js run
let elem = document.body;
@@ -227,10 +227,10 @@ let elem = document.body;
alert( getComputedStyle(elem).width ); // show CSS width for elem
```
-Why should we 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 CSS purposes may break such JavaScript.
-2. Second, CSS `width/height` may be `auto`, for instance for an inline element:
+1. 首先,CSS 宽度/高度取决于另一个属性:`box-sizing`,它定义了 “什么是” CSS 宽度和高度。用作 CSS 样式的 `box-sizing` 的更改可能会破坏这样的 JavaScript 定义。
+2. 其次,CSS 的 `width/height` 可能是 `auto`,例如内联元素:
```html run
Hello!
@@ -242,34 +242,34 @@ Why should we use geometry properties instead? There are two reasons:
```
- 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.
+ 从 CSS 的观点来看,`width:auto` 是完全正常的,但是在 JavaScript 中,我们需要一个精确的 `px` 大小,以便我们在计算过程中使用它,所以 CSS 宽度根本没用。
-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.
+还有一个原因:滚动条。有时,没有滚动条的代码工作得很好,因为它在一些浏览器中占据了内容的一部分空间。因此,内容的实际宽度比 CSS 宽度要小。而 `clientWidth/clientHeight` 考虑到这一点。
-...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 properties.
+但是,`getComputedStyle(elem).width` 的情况是不同的。一些浏览器(例如 Chrome)返回真正的内部宽度,这种情况不考虑滚动条,以及其中一些(例如Firefox)— CSS 宽度(忽略滚动条)。这样的跨浏览器差异是不使用 `getComputedStyle` 样式的原因,而是依赖于几何属性。
```online
-If your browser reserves the space for a scrollbar (most browsers for Windows do), then you can test it below.
+如果浏览器保留滚动条的空间(大多数 Windows 中的浏览器),那么你可以在下面测试它。
[iframe src="cssWidthScroll" link border=1]
-The element with text has CSS `width:300px`.
+包含文本的元素有这样的样式 `width:300px`。
-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.
+在桌面 Windows 操作系统上,Firefox、Chrome、Edgy 都为滚动条保留空间,但 Firefox 显示 `300px`,而 Chrome 和 Edgy 显示较少。这是因为 Firefox 返回 CSS 宽度,其他浏览器返回“真实”宽度。
```
-Please note that the described difference is only about reading `getComputedStyle(...).width` from JavaScript, visually everything is correct.
+请注意,所描述的差异只是关于从 JavaScript 读取的 `getComputedStyle(...).width`,而视觉上一切都是正确的。
-## Summary
+## 总结
-Elements have the following geometry properties:
+元素具有以下几何属性:
-- `offsetParent` -- is the nearest positioned ancestor or `td`, `th`, `table`, `body`.
-- `offsetLeft/offsetTop` -- coordinates relative to the left-upper edge of `offsetParent`.
-- `offsetWidth/offsetHeight` -- "outer" width/height of an element including borders.
-- `clientLeft/clientTop` -- the distance from the left-upper outer corner to its left-upper inner corner. For left-to-right OS they are always the widths of left/top borders. For right-to-left OS the vertical scrollbar is on the left so `clientLeft` includes its width too.
-- `clientWidth/clientHeight` -- the width/height of the content including paddings, but without the scrollbar.
-- `scrollWidth/scrollHeight` -- the width/height of the content including the scrolled out parts. Also includes paddings, but not the scrollbar.
-- `scrollLeft/scrollTop` -- width/height of the scrolled out part of the element, starting from its left-upper corner.
+- `offsetParent` — 是最近的有定位属性的祖先元素,或者是 `td`、`th`、`table`、`body`。
+- `offsetLeft/offsetTop` — 是相对于 `offsetParent` 的左上角边缘坐标。
+- `offsetWidth/offsetHeight` — 元素的“外部”宽/高 ,边框尺寸计算在内。
+- `clientLeft/clientTop` — 从元素左上角外部到内部的距离,对于从左到右渲染元素的操作系统,它始终是左/顶部边界的宽度,而对于从右到左的操作系统,垂直滚动条在左边,所以 `clientLeft` 也包括滚动条的宽度。
+- `clientWidth/clientHeight` — 内容的宽度/高度,包括内间距,但没有滚动条。
+- `scrollWidth/scrollHeight` — 内容的宽度/高度,包括可滚动的可视区域外的尺寸,也包括内间距,但不包括滚动条。
+- `scrollLeft/scrollTop` — 从左上角开始的元素的滚动部分的宽度/高度。
-All properties are read-only except `scrollLeft/scrollTop`. They make the browser scroll the element if changed.
+除了 `scrollLeft/scrollTop` 之外,所有属性都是只读的。如果更改,浏览器会使元素滚动。
| |