|
1 |
| -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`). |
| 1 | +为小球设置 `position:absolute`。这意味着它的 `left/top` 坐标根据相对它最近的并且设置了定位的元素来测量,这个元素的有效范围就是 `#field`(因为它有 `position:relative`)。 |
2 | 2 |
|
3 |
| -The coordinates start from the inner left-upper corner of the field: |
| 3 | +坐标从设置了相对定位的最近的元素的左上角开始: |
4 | 4 |
|
5 | 5 | 
|
6 | 6 |
|
7 |
| -The inner field width/height is `clientWidth/clientHeight`. So the field center has coordinates `(clientWidth/2, clientHeight/2)`. |
| 7 | +该元素内容区域的宽/高是 `clientWidth/clientHeight`,所以该元素有效范围的中心的坐标为 `(clientWidth/2, clientHeight/2)`。 |
8 | 8 |
|
9 |
| -...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: |
| 9 | +...不过如果我们将 `ball.style.left/top` 设置为上面计算出的值,那么最终的效果与有效范围的中心重合的不是小球的中心,而是小球左上角的坐标: |
10 | 10 |
|
11 | 11 | ```js
|
12 | 12 | ball.style.left = Math.round(field.clientWidth / 2) + 'px';
|
13 | 13 | ball.style.top = Math.round(field.clientHeight / 2) + 'px';
|
14 | 14 | ```
|
15 | 15 |
|
16 |
| -Here's how it looks: |
| 16 | +这是它将显示出的效果: |
17 | 17 |
|
18 | 18 | [iframe height=180 src="ball-half"]
|
19 | 19 |
|
20 |
| -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: |
| 20 | +为了把球中心和有效范围中心对准,我们应该把球移到左边宽度的一半,把 top 设置为有效范围高度的一半: |
21 | 21 |
|
22 | 22 | ```js
|
23 | 23 | ball.style.left = Math.round(field.clientWidth / 2 - ball.offsetWidth / 2) + 'px';
|
24 | 24 | ball.style.top = Math.round(field.clientHeight / 2 - ball.offsetHeight / 2) + 'px';
|
25 | 25 | ```
|
26 | 26 |
|
27 |
| -**Attention: the pitfall!** |
| 27 | +**注意:陷阱!** |
28 | 28 |
|
29 |
| -The code won't work reliably while `<img>` has no width/height: |
| 29 | +如果 `<img>` 没有宽/高,代码将无法正常工作: |
30 | 30 |
|
31 | 31 | ```html
|
32 | 32 | <img src="ball.png" id="ball">
|
33 | 33 | ```
|
34 | 34 |
|
35 |
| -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. |
| 35 | +当浏览器还不知道图片的宽/高(图片的尺寸可能来自标签属性或 CSS)的时候它会假设它们的尺寸为 `0`直到图片加载完成。 |
36 | 36 |
|
37 |
| -In real life after the first load browser usually caches the image, and on next loads it will have the size immediately. |
| 37 | +实际使用过程中,浏览器会在图片第一次加载完成后缓存该图片,方便下次再次访问时立即显示图片。 |
38 | 38 |
|
39 |
| -But on the first load the value of `ball.offsetWidth` is `0`. That leads to wrong coordinates. |
| 39 | +但是在第一次加载时 `ball.offsetWidth` 的值为 `0`,这会导致错误的坐标出现。 |
40 | 40 |
|
41 |
| -We should fix that by adding `width/height` to `<img>`: |
| 41 | +此时我们应该为 `<img>` 添加 `width/height` 属性: |
42 | 42 |
|
43 | 43 | ```html
|
44 | 44 | <img src="ball.png" *!*width="40" height="40"*/!* id="ball">
|
45 | 45 | ```
|
46 | 46 |
|
47 |
| -...Or provide the size in CSS: |
| 47 | +...或者在 CSS 中提供尺寸: |
48 | 48 |
|
49 | 49 | ```css
|
50 | 50 | #ball {
|
|
0 commit comments