Skip to content

Commit 4272b7b

Browse files
committed
up
1 parent caede8f commit 4272b7b

File tree

32 files changed

+830
-205
lines changed

32 files changed

+830
-205
lines changed
7.11 KB
Loading
11.7 KB
Loading

2-ui/1-document/15-size-and-scroll/4-put-ball-in-center/solution.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The coordinates start from the inner left-upper corner of the field:
66

77
The inner field width/height is `clientWidth/clientHeight`. So the field center has coordinates `(clientWidth/2, clientHeight/2)`.
88

9-
...But if we set such values to `ball.style.left/top`, then not the ball as a whole, but the left-upper edge of the ball is in the center:
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:
1010

1111
```js
1212
ball.style.left = Math.round(field.clientWidth / 2) + 'px';
@@ -26,24 +26,29 @@ ball.style.top = Math.round(field.clientHeight / 2 - ball.offsetHeight / 2) + 'p
2626

2727
**Attention: the pitfall!**
2828

29-
The code won't work reliably while `<img>` width/height is not given to the browser:
29+
The code won't work reliably while `<img>` has no width/height:
3030

3131
```html
3232
<img src="ball.png" id="ball">
3333
```
3434

35-
When the browser meets such a tag, it tries to figure out its width/height. Either from the `<img width=... height=...>` attributes or (if not given) from CSS.
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.
3636

37-
If none is given, then the sizes are assumed to be `0` until the image loads.
37+
In real life after the first load browser usually caches the image, and on next loads it will have the size immediately.
3838

39-
TODO
39+
But on the first load the value of `ball.offsetWidth` is `0`. That leads to wrong coordinates.
4040

41-
После первой загрузки изображение уже будет в кеше браузера, и его размеры будут известны. Но когда браузер впервые видит документ -- он ничего не знает о картинке, поэтому значение `ball.offsetWidth` равно `0`. Вычислить координаты невозможно.
42-
43-
Чтобы это исправить, добавим `width/height` к картинке:
41+
We should fix that by adding `width/height` to `<img>`:
4442

4543
```html
4644
<img src="ball.png" *!*width="40" height="40"*/!* id="ball">
4745
```
4846

49-
Теперь браузер всегда знает ширину и высоту, так что все работает. Тот же эффект дало бы указание размеров в CSS.
47+
...Or provide the size in CSS:
48+
49+
```css
50+
#ball {
51+
width: 40px;
52+
height: 40px;
53+
}
54+
```

2-ui/1-document/15-size-and-scroll/4-put-ball-in-center/solution.view/index.html

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
background-color: #00FF00;
1010
position: relative;
1111
}
12-
12+
1313
#ball {
1414
position: absolute;
1515
}
@@ -20,15 +20,12 @@
2020

2121

2222
<div id="field">
23-
<img src="https://js.cx/clipart/ball.svg" width="40" height="40" id="ball"> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
23+
<img src="https://en.js.cx/clipart/ball.svg" width="40" height="40" id="ball"> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2424
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2525
</div>
2626

2727

2828
<script>
29-
var ball = document.getElementById('ball')
30-
var field = document.getElementById('field')
31-
3229
// ball.offsetWidth=0 before image loaded!
3330
// to fix: set width
3431
ball.style.left = Math.round(field.clientWidth / 2 - ball.offsetWidth / 2) + 'px'
@@ -38,4 +35,4 @@
3835

3936
</body>
4037

41-
</html>
38+
</html>

2-ui/1-document/15-size-and-scroll/4-put-ball-in-center/source.view/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
background-color: #00FF00;
1010
position: relative;
1111
}
12-
12+
1313
#ball {
1414
position: absolute;
1515
}
@@ -20,11 +20,11 @@
2020

2121

2222
<div id="field">
23-
<img src="https://js.cx/clipart/ball.svg" id="ball"> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
23+
<img src="https://en.js.cx/clipart/ball.svg" id="ball"> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2424
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2525
</div>
2626

2727

2828
</body>
2929

30-
</html>
30+
</html>

2-ui/1-document/15-size-and-scroll/5-expand-element/solution.md

Lines changed: 0 additions & 43 deletions
This file was deleted.

2-ui/1-document/15-size-and-scroll/5-expand-element/solution.view/index.html

Lines changed: 0 additions & 53 deletions
This file was deleted.

2-ui/1-document/15-size-and-scroll/5-expand-element/source.view/index.html

Lines changed: 0 additions & 39 deletions
This file was deleted.

2-ui/1-document/15-size-and-scroll/5-expand-element/task.md

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
Отличия:
2-
3-
1. `getComputedStyle` не работает в IE8-.
4-
2. `clientWidth` возвращает число, а `getComputedStyle(...).width` -- строку, на конце `px`.
5-
3. `getComputedStyle` не всегда даст ширину, он может вернуть, к примеру, `"auto"` для инлайнового элемента.
6-
4. `clientWidth` соответствует внутренней видимой области элемента, *включая `padding`, а CSS-ширина `width` при стандартном значении `box-sizing` соответствует зоне *внутри `padding`*.
7-
5. Если есть полоса прокрутки, то некоторые браузеры включают её ширину в `width`, а некоторые -- нет.
8-
9-
Свойство `clientWidth`, с другой стороны, полностью кросс-браузерно. Оно всегда обозначает размер *за вычетом прокрутки*, т.е. реально доступный для содержимого.
1+
Differences:
102

3+
1. `clientWidth` is numeric, while `getComputedStyle(elem).width` returns a string with `px` at the end.
4+
2. `getComputedStyle` may return non-numeric width like `"auto"` for an inline element.
5+
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*.
6+
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.

0 commit comments

Comments
 (0)