Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fd81db7

Browse files
committed
closes #1812
1 parent 6700835 commit fd81db7

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

‎2-ui/1-document/09-size-and-scroll/4-put-ball-in-center/ball-half/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,7 +20,7 @@
2020

2121

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

@@ -38,4 +38,4 @@
3838

3939
</body>
4040

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

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,22 @@ ball.style.left = Math.round(field.clientWidth / 2 - ball.offsetWidth / 2) + 'px
2424
ball.style.top = Math.round(field.clientHeight / 2 - ball.offsetHeight / 2) + 'px';
2525
```
2626

27-
**Attention: the pitfall!**
27+
Now the ball is finally centered.
28+
29+
````warn header="Attention: the pitfall!"
2830
2931
The code won't work reliably while `<img>` has no width/height:
3032
3133
```html
3234
<img src="ball.png" id="ball">
3335
```
36+
````
3437

3538
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.
3639

37-
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.
40+
So the value of `ball.offsetWidth` will be `0` until the image loads. That leads to wrong coordinates in the code above.
41+
42+
After the first load, the browser usually caches the image, and on reloads it will have the size immediately. But on the first load the value of `ball.offsetWidth` is `0`.
3843

3944
We should fix that by adding `width/height` to `<img>`:
4045

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727

2828
<script>
29-
// ball.offsetWidth=0 before image loaded!
29+
// ball.offsetWidth=0 before image loaded!
3030
// to fix: set width
3131
ball.style.left = Math.round(field.clientWidth / 2 - ball.offsetWidth / 2) + 'px'
3232
ball.style.top = Math.round(field.clientHeight / 2 - ball.offsetHeight / 2) + 'px'

‎2-ui/1-document/09-size-and-scroll/4-put-ball-in-center/task.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Here's how the source document looks:
1010

1111
What are coordinates of the field center?
1212

13-
Calculate them and use to place the ball into the center of the field:
13+
Calculate them and use to place the ball into the center of the green field:
1414

1515
[iframe src="solution" height=180]
1616

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /