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
Loading

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

+14-9
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

+3-6
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

+3-3
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

-43
This file was deleted.

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

-53
This file was deleted.

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

-39
This file was deleted.

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

-18
This file was deleted.
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.

2-ui/1-document/15-size-and-scroll/6-width-vs-clientwidth/task.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ importance: 5
22

33
---
44

5-
# В чём отличие "width" и "clientWidth" ?
5+
# The difference: CSS width versus clientWidth
66

7-
В чём отличия между `getComputedStyle(elem).width` и `elem.clientWidth`?
7+
What's the difference between `getComputedStyle(elem).width` and `elem.clientWidth`?
88

9-
Укажите хотя бы три отличия, лучше -- больше.
9+
Give at least 3 differences. The more the better.

2-ui/1-document/15-size-and-scroll/metric.view/index.html

+17-22
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
margin: 0;
99
padding: 0;
1010
}
11-
11+
1212
#example {
1313
width: 300px;
1414
height: 200px;
1515
overflow: auto;
1616
border: 25px solid #E8C48F;
1717
padding: 20px;
1818
}
19-
19+
2020
.key {
2121
cursor: pointer;
2222
text-decoration: underline;
@@ -43,26 +43,22 @@ <h3>Introduction</h3>
4343
</div>
4444

4545

46-
<div id="mouse-wrap">Координаты мыши: <span id="mouse">...</span></div>
46+
<div id="mouse-wrap">Mouse coordinates: <span id="mouse">...</span></div>
4747
<div id="info"></div>
4848

4949

5050
<script>
51-
var example = document.getElementById('example')
52-
53-
var info = document.getElementById('info')
54-
55-
var props = {
56-
'размеры': ['clientLeft', 'clientTop', 'clientWidth', 'clientHeight', 'offsetWidth', 'offsetHeight', 'scrollWidth', 'scrollHeight'],
57-
'прокрутка': ['scrollLeft', 'scrollTop'],
58-
'позиционирование по рендерингу': ['offsetParent', 'offsetLeft', 'offsetTop']
59-
}
51+
let props = {
52+
geometry: ['clientLeft', 'clientTop', 'clientWidth', 'clientHeight', 'offsetWidth', 'offsetHeight', 'scrollWidth', 'scrollHeight'],
53+
scroll: ['scrollLeft', 'scrollTop'],
54+
offsetParent: ['offsetParent', 'offsetLeft', 'offsetTop']
55+
};
6056

61-
info.innerHTML = '<h3>Нажмите для просмотра значения:</h3>';
62-
for (var k in props) {
63-
info.innerHTML += '<h4>' + k + '</h4>';
64-
var prop = props[k];
65-
for (var i = 0; i < prop.length; i++) {
57+
info.innerHTML = '<h3>Click to see the value:</h3>';
58+
for (let k in props) {
59+
info.innerHTML += `<h4>${k}</h4>`;
60+
let prop = props[k];
61+
for (let i = 0; i < prop.length; i++) {
6662
info.innerHTML += "<span class='key'>" + prop[i] + '</span>: <span id="' + prop[i] + '">&nbsp;</span>' + " "
6763
i++;
6864
if (i < prop.length) {
@@ -74,11 +70,11 @@ <h3>Introduction</h3>
7470
}
7571

7672
document.onclick = function(event) {
77-
var target = event.target;
73+
let target = event.target;
7874
if (!target.classList.contains('key')) return;
7975

80-
var prop = target.innerHTML;
81-
var value = example[prop];
76+
let prop = target.innerHTML;
77+
let value = example[prop];
8278
value = value.tagName || value;
8379
document.getElementById(prop).innerHTML = value;
8480
};
@@ -90,5 +86,4 @@ <h3>Introduction</h3>
9086
</script>
9187

9288
</body>
93-
94-
</html>
89+
</html>

0 commit comments

Comments
 (0)