You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2-ui/3-event-details/8-onscroll/1-endless-page/solution.md
+11-12Lines changed: 11 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -8,12 +8,12 @@ Let's use window-relative coordinates.
8
8
9
9
The document is represented (and contained) within `<html>` tag, that is `document.documentElement`.
10
10
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.
12
12
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:
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.
45
45
46
-
And the window height is`document.documentElement.clientHeight`.
46
+
We can obtain the window height as`document.documentElement.clientHeight`.
47
47
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`).
49
49
50
50
So here's the function:
51
51
@@ -55,12 +55,11 @@ function populate() {
55
55
// document bottom
56
56
let windowRelativeBottom =document.documentElement.getBoundingClientRect().bottom;
57
57
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;
Copy file name to clipboardExpand all lines: 2-ui/3-event-details/8-onscroll/2-updown-button/task.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,6 @@ It should work like this:
11
11
- 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.
12
12
- When the arrow is clicked, the page scrolls to the top.
13
13
14
-
Like this:
14
+
Like this (top-left corner, scroll to see):
15
15
16
16
[iframe border="1" height="200" link src="solution"]
The `onscroll` handler should check which images are visible and show them.
2
2
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.
4
4
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:
6
8
7
9
```js
8
10
// ...the page content is above...
@@ -13,19 +15,35 @@ function isVisible(elem) {
13
15
14
16
let windowHeight =document.documentElement.clientHeight;
15
17
16
-
// top elem edge is visible OR bottom elem edge is visible
18
+
// top elem edge is visible?
17
19
let topVisible =coords.top>0&&coords.top< windowHeight;
20
+
21
+
// bottom elem edge is visible?
18
22
let bottomVisible =coords.bottom< windowHeight &&coords.bottom>0;
19
23
20
24
return topVisible || bottomVisible;
21
25
}
26
+
```
27
+
28
+
The `showVisible()` function uses the visibility check, implemented by `isVisible()`, to load visible images:
29
+
30
+
```js
31
+
functionshowVisible() {
32
+
for (let img ofdocument.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
+
}
22
42
23
43
*!*
24
44
showVisible();
25
45
window.onscroll= showVisible;
26
46
*/!*
27
47
```
28
48
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.
Copy file name to clipboardExpand all lines: 2-ui/3-event-details/8-onscroll/article.md
+5-7Lines changed: 5 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Scrolling
2
2
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.
4
4
5
5
For instance:
6
6
- 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.
24
24
25
25
## Prevent scrolling
26
26
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?
28
28
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.
30
30
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`.
34
32
35
33
If we add an event handler to these events and `event.preventDefault()` in it, then the scroll won't start.
36
34
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.
38
36
39
37
Here are few tasks that you can solve or look through to see the applications on `onscroll`.
0 commit comments