Skip to content

Commit 3d4fa7c

Browse files
committed
minor
1 parent 9bfd54a commit 3d4fa7c

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

2-ui/3-event-details/8-onscroll/1-endless-page/solution.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ Let's use window-relative coordinates.
88

99
The document is represented (and contained) within `<html>` tag, that is `document.documentElement`.
1010

11-
We can get window-relative coordinates of the whole document as `document.documentElement.getBoundingClientRect()`. And the `bottom` property will be window-relative coordinate of the document end.
11+
We can get window-relative coordinates of the whole document as `document.documentElement.getBoundingClientRect()`, the `bottom` property will be window-relative coordinate of the document bottom.
1212

13-
For instance, if the height of the whole HTML document is 2000px, then:
13+
For instance, if the height of the whole HTML document is `2000px`, then:
1414

1515
```js
16-
// When we're on the top of the page
16+
// when we're on the top of the page
1717
// window-relative top = 0
1818
document.documentElement.getBoundingClientRect().top = 0
1919

@@ -41,11 +41,11 @@ document.documentElement.getBoundingClientRect().top = -1400
4141
document.documentElement.getBoundingClientRect().bottom = 600
4242
```
4343

44-
Please note that the bottom can't be 0, because it never reaches the window top. The lowest limit of the bottom coordinate is the window height, we can't scroll it any more up.
44+
Please note that the `bottom` can't be `0`, because it never reaches the window top. The lowest limit of the `bottom` coordinate is the window height (we assumed it to be `600`), we can't scroll it any more up.
4545

46-
And the window height is `document.documentElement.clientHeight`.
46+
We can obtain the window height as `document.documentElement.clientHeight`.
4747

48-
We want the document bottom be no more than `100px` away from it.
48+
For our task, we need to know when the document bottom is not no more than `100px` away from it (that is: `600-700px`, if the height is `600`).
4949

5050
So here's the function:
5151

@@ -55,12 +55,11 @@ function populate() {
5555
// document bottom
5656
let windowRelativeBottom = document.documentElement.getBoundingClientRect().bottom;
5757

58-
// if it's greater than window height + 100px, then we're not at the page back
59-
// (see examples above, big bottom means we need to scroll more)
60-
if (windowRelativeBottom > document.documentElement.clientHeight + 100) break;
61-
62-
// otherwise let's add more data
63-
document.body.insertAdjacentHTML("beforeend", `<p>Date: ${new Date()}</p>`);
58+
// if the user scrolled far enough (<100px to the end)
59+
if (windowRelativeBottom < document.documentElement.clientHeight + 100) {
60+
// let's add more data
61+
document.body.insertAdjacentHTML("beforeend", `<p>Date: ${new Date()}</p>`);
62+
}
6463
}
6564
}
6665
```

2-ui/3-event-details/8-onscroll/2-updown-button/task.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ It should work like this:
1111
- When the page is scrolled down more than the window height -- there appears an "upwards" arrow in the left-top corner. If the page is scrolled back, it disappears.
1212
- When the arrow is clicked, the page scrolls to the top.
1313

14-
Like this:
14+
Like this (top-left corner, scroll to see):
1515

1616
[iframe border="1" height="200" link src="solution"]
Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
The `onscroll` handler should check which images are visible and show them.
22

3-
We also may want to run it when the page loads, to detect immediately visible images prior to any scrolling and load them.
3+
We also want to run it when the page loads, to detect immediately visible images and load them.
44

5-
If we put it at the `<body>` bottom, then it runs when the page content is loaded.
5+
The code should execute when the document is loaded, so that it has access to its content.
6+
7+
Or put it at the `<body>` bottom:
68

79
```js
810
// ...the page content is above...
@@ -13,19 +15,35 @@ function isVisible(elem) {
1315

1416
let windowHeight = document.documentElement.clientHeight;
1517

16-
// top elem edge is visible OR bottom elem edge is visible
18+
// top elem edge is visible?
1719
let topVisible = coords.top > 0 && coords.top < windowHeight;
20+
21+
// bottom elem edge is visible?
1822
let bottomVisible = coords.bottom < windowHeight && coords.bottom > 0;
1923

2024
return topVisible || bottomVisible;
2125
}
26+
```
27+
28+
The `showVisible()` function uses the visibility check, implemented by `isVisible()`, to load visible images:
29+
30+
```js
31+
function showVisible() {
32+
for (let img of document.querySelectorAll('img')) {
33+
let realSrc = img.dataset.src;
34+
if (!realSrc) continue;
35+
36+
if (isVisible(img)) {
37+
img.src = realSrc;
38+
img.dataset.src = '';
39+
}
40+
}
41+
}
2242

2343
*!*
2444
showVisible();
2545
window.onscroll = showVisible;
2646
*/!*
2747
```
2848

29-
For visible images we can take `img.dataset.src` and assign it to `img.src` (if not did it yet).
30-
31-
P.S. The solution also has a variant of `isVisible` that "pre-loads" images that are within 1 page above/below (the page height is `document.documentElement.clientHeight`).
49+
P.S. The solution also has a variant of `isVisible` that "preloads" images that are within 1 page above/below the current document scroll.

2-ui/3-event-details/8-onscroll/article.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Scrolling
22

3-
Scroll events allow to react on a page or element scrolling. There are quite a few good things we can do here.
3+
The `scroll` event allows to react on a page or element scrolling. There are quite a few good things we can do here.
44

55
For instance:
66
- Show/hide additional controls or information depending on where in the document the user is.
@@ -24,16 +24,14 @@ The `scroll` event works both on the `window` and on scrollable elements.
2424

2525
## Prevent scrolling
2626

27-
How do we make something unscrollable? We can't prevent scrolling by using `event.preventDefault()` in `onscroll` listener, because it triggers *after* the scroll has already happened.
27+
How do we make something unscrollable?
2828

29-
But we can prevent scrolling by `event.preventDefault()` on an event that causes the scroll.
29+
We can't prevent scrolling by using `event.preventDefault()` in `onscroll` listener, because it triggers *after* the scroll has already happened.
3030

31-
For instance:
32-
- `wheel` event -- a mouse wheel roll (a "scrolling" touchpad action generates it too).
33-
- `keydown` event for `key:pageUp` and `key:pageDown`.
31+
But we can prevent scrolling by `event.preventDefault()` on an event that causes the scroll, for instance `keydown` event for `key:pageUp` and `key:pageDown`.
3432

3533
If we add an event handler to these events and `event.preventDefault()` in it, then the scroll won't start.
3634

37-
Sometimes that may help, but it's more reliable to use CSS to make something unscrollable, such as the `overflow` property.
35+
There are many ways to initiate a scroll, so it's more reliable to use CSS, `overflow` property.
3836

3937
Here are few tasks that you can solve or look through to see the applications on `onscroll`.

0 commit comments

Comments
 (0)