Skip to content

Commit 6a9b351

Browse files
Scrolling (#295)
* onscroll * edits onscroll * Update 2-ui/3-event-details/8-onscroll/1-endless-page/solution.md * Update 2-ui/3-event-details/8-onscroll/1-endless-page/source.view/index.html * Update 2-ui/3-event-details/8-onscroll/2-updown-button/source.view/index.html * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/solution.view/index.html * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/solution.view/index.html * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/source.view/index.html * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/source.view/index.html * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/source.view/index.html * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/source.view/index.html * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/source.view/index.html * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/solution.view/index.html * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/solution.view/index.html * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/solution.view/index.html * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/solution.view/index.html * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/solution.view/index.html * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/solution.view/index.html * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/solution.view/index.html * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/solution.view/index.html * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/task.md * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/task.md * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/source.view/index.html * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/source.view/index.html * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/source.view/index.html * Update 2-ui/3-event-details/8-onscroll/3-load-visible-img/source.view/index.html Co-authored-by: Mykola Sopiha <sopiha.mv@gmail.com>
1 parent 9b853d9 commit 6a9b351

File tree

12 files changed

+289
-289
lines changed

12 files changed

+289
-289
lines changed

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@
1-
The core of the solution is a function that adds more dates to the page (or loads more stuff in real-life) while we're at the page end.
1+
Основою рішення є функція, яка додає більше дат на сторінку (або завантажує більше речей у реальному житті), поки ми знаходимося в кінці сторінки.
22

3-
We can call it immediately and add as a `window.onscroll` handler.
3+
Ми можемо негайно викликати її та додати `window.onscroll` як обробник.
44

5-
The most important question is: "How do we detect that the page is scrolled to bottom?"
5+
Найважливіше запитання: "Як ми виявимо, що сторінка прокручується вниз?"
66

7-
Let's use window-relative coordinates.
7+
Скористаємось координатами, відносними до вікна.
88

9-
The document is represented (and contained) within `<html>` tag, that is `document.documentElement`.
9+
Документ представлений (і міститься) у тегу `<html>`, тобто `document.documentElement`.
1010

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.
11+
Ми можемо отримати відносні до вікна координати всього документа як `document.documentElement.getBoundingClientRect()`, властивість `bottom` буде відносною до вікна координатою низа документа.
1212

13-
For instance, if the height of the whole HTML document is `2000px`, then:
13+
Наприклад, якщо висота всього HTML-документа дорівнює `2000px`, тоді:
1414

1515
```js
16-
// when we're on the top of the page
17-
// window-relative top = 0
16+
// коли ми знаходимося вгорі сторінки
17+
// top відносний до вікна дорівнює 0
1818
document.documentElement.getBoundingClientRect().top = 0
1919

20-
// window-relative bottom = 2000
21-
// the document is long, so that is probably far beyond the window bottom
20+
// bottom відносний до вікна дорівнює 2000
21+
// документ великий, тому він, ймовірно, знаходиться далеко за нижньою частиною вікна
2222
document.documentElement.getBoundingClientRect().bottom = 2000
2323
```
2424

25-
If we scroll `500px` below, then:
25+
Якщо ми прокрутимо `500px` нижче, то:
2626

2727
```js
28-
// document top is above the window 500px
28+
// Верхня частина документа знаходиться над вікном на 500px
2929
document.documentElement.getBoundingClientRect().top = -500
30-
// document bottom is 500px closer
30+
// Нижня частина документа на 500px ближче
3131
document.documentElement.getBoundingClientRect().bottom = 1500
3232
```
3333

34-
When we scroll till the end, assuming that the window height is `600px`:
34+
Коли ми прокручуємо до кінця, припускаючи, що висота вікна дорівнює `600px`:
3535

3636

3737
```js
38-
// document top is above the window 1400px
38+
// Верхня частина документа знаходиться над вікном на 1400px
3939
document.documentElement.getBoundingClientRect().top = -1400
40-
// document bottom is below the window 600px
40+
// Нижня частина документа знаходиться під вікном на 600px
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 assumed it to be `600`), we can't scroll it any more up.
44+
Зауважте, що `bottom` не може бути `0`, оскільки він ніколи не досягає верхньої частини вікна. Найнижча межа `bottom` координати -- це висота вікна (ми припустили, що це `600`), ми більше не можемо прокручувати вгору.
4545

46-
We can obtain the window height as `document.documentElement.clientHeight`.
46+
Ми можемо отримати висоту вікна як `document.documentElement.clientHeight`.
4747

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`).
48+
Для нашого завдання нам потрібно знати, коли нижня частина документа знаходиться на відстані не більше ніж `100px` від неї (тобто: `600-700px`, якщо висота `600`).
4949

50-
So here's the function:
50+
Отже, ось функція:
5151

5252
```js
5353
function populate() {
5454
while(true) {
55-
// document bottom
55+
// нижня частина документа
5656
let windowRelativeBottom = document.documentElement.getBoundingClientRect().bottom;
5757

58-
// if the user hasn't scrolled far enough (>100px to the end)
58+
// якщо користувач не прокрутив достатньо далеко (>100px до кінця)
5959
if (windowRelativeBottom > document.documentElement.clientHeight + 100) break;
6060

61-
// let's add more data
61+
// додамо більше даних
6262
document.body.insertAdjacentHTML("beforeend", `<p>Date: ${new Date()}</p>`);
6363
}
6464
}

2-ui/3-event-details/8-onscroll/1-endless-page/solution.view/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<body>
88

9-
<h1>Scroll me</h1>
9+
<h1>Прокрути мене</h1>
1010

1111
<script>
1212
function populate() {
@@ -19,7 +19,7 @@ <h1>Scroll me</h1>
1919

2020
window.addEventListener('scroll', populate);
2121

22-
populate(); // init document
22+
populate(); // ініціалізація документа
2323
</script>
2424

2525
</body>

2-ui/3-event-details/8-onscroll/1-endless-page/source.view/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<h1>Scroll me</h1>
1010

1111
<script>
12-
// ... your code ...
12+
// ... ваш код ...
1313
</script>
1414

1515
</body>

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ importance: 5
22

33
---
44

5-
# Endless page
5+
# Нескінченна сторінка
66

7-
Create an endless page. When a visitor scrolls it to the end, it auto-appends current date-time to the text (so that a visitor can scroll more).
7+
Створіть нескінченну сторінку. Коли відвідувач прокручує її до кінця, поточна дата-час автоматично додається до тексту (щоб відвідувач міг прокручувати більше).
88

9-
Like this:
9+
ось таким чином:
1010

1111
[iframe src="solution" height=200]
1212

13-
Please note two important features of the scroll:
13+
Будь ласка, зверніть увагу на дві важливі особливості прокрутки:
1414

15-
1. **The scroll is "elastic".** We can scroll a little beyond the document start or end in some browsers/devices (empty space below is shown, and then the document will automatically "bounces back" to normal).
16-
2. **The scroll is imprecise.** When we scroll to page end, then we may be in fact like 0-50px away from the real document bottom.
15+
1. **Прокручування є "еластичним".** У деяких браузерах/пристроях ми можемо прокрутити трохи далі початку або кінця документа (буде показано пусте місце, а потім документ автоматично "повернеться" до нормального стану).
16+
2. **Прокрутка неточна.** Коли ми прокручуємо до кінця сторінки, насправді ми можемо бути на відстані 0-50 пікселів від реального низу документа.
1717

18-
So, "scrolling to the end" should mean that the visitor is no more than 100px away from the document end.
18+
Отже, "прокрутка до кінця" має означати, що відвідувач знаходиться на відстані не більше 100 пікселів від кінця документа.
1919

20-
P.S. In real life we may want to show "more messages" or "more goods".
20+
P.S. У реальному житті ми можемо захотіти показати "більше повідомлень" або "більше товарів".

2-ui/3-event-details/8-onscroll/2-updown-button/solution.view/index.html

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

5050
arrowTop.onclick = function() {
5151
window.scrollTo(pageXOffset, 0);
52-
// after scrollTo, there will be a "scroll" event, so the arrow will hide automatically
52+
// після scrollTo відбудеться подія "scroll", тому стрілка автоматично сховається
5353
};
5454

5555
window.addEventListener('scroll', function() {

2-ui/3-event-details/8-onscroll/2-updown-button/source.view/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<div id="arrowTop"></div>
4747

4848
<script>
49-
// ... your code ...
49+
// ... ваш код ...
5050
</script>
5151

5252
</body>

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ importance: 5
22

33
---
44

5-
# Up/down button
5+
# Кнопка вгору/вниз
66

7-
Create a "to the top" button to help with page scrolling.
7+
Створіть кнопку "вгору", щоб допомогти прокручувати сторінку.
88

9-
It should work like this:
10-
- While the page is not scrolled down at least for the window height -- it's invisible.
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-
- When the arrow is clicked, the page scrolls to the top.
9+
Вона має працювати так:
10+
- Поки сторінка не прокручена принаймні на висоту вікна -- вона невидима.
11+
- Якщо сторінка прокручена нижче висоти вікна, у верхньому лівому куті з’являється стрілка "вгору". Якщо сторінку прокрутити назад, вона зникне.
12+
- При натисканні стрілки сторінка прокручується вгору.
1313

14-
Like this (top-left corner, scroll to see):
14+
Ось так (верхній лівий кут, прокрутіть, щоб побачити):
1515

1616
[iframe border="1" height="200" link src="solution"]

2-ui/3-event-details/8-onscroll/3-load-visible-img/solution.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
The `onscroll` handler should check which images are visible and show them.
1+
Обробник `onscroll` повинен перевірити, які зображення є видимими, і показати їх.
22

3-
We also want to run it when the page loads, to detect immediately visible images and load them.
3+
Ми також хочемо запускати його під час завантаження сторінки, щоб виявляти видимі зображення відразу та завантажувати їх.
44

5-
The code should execute when the document is loaded, so that it has access to its content.
5+
Код повинен виконуватися під час завантаження документа, щоб він мав доступ до його вмісту.
66

7-
Or put it at the `<body>` bottom:
7+
Або розмістіть його внизу `<body>`:
88

99
```js
10-
// ...the page content is above...
10+
// ...вміст сторінки вище...
1111

1212
function isVisible(elem) {
1313

1414
let coords = elem.getBoundingClientRect();
1515

1616
let windowHeight = document.documentElement.clientHeight;
1717

18-
// top elem edge is visible?
18+
// видно верхній край елемента?
1919
let topVisible = coords.top > 0 && coords.top < windowHeight;
2020

21-
// bottom elem edge is visible?
21+
// видно нижній край елемента?
2222
let bottomVisible = coords.bottom < windowHeight && coords.bottom > 0;
2323

2424
return topVisible || bottomVisible;
2525
}
2626
```
2727

28-
The `showVisible()` function uses the visibility check, implemented by `isVisible()`, to load visible images:
28+
Функція `showVisible()` використовує перевірку видимості, реалізовану `isVisible()`, для завантаження видимих зображень:
2929

3030
```js
3131
function showVisible() {
@@ -46,4 +46,4 @@ window.onscroll = showVisible;
4646
*/!*
4747
```
4848

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.
49+
P.S. Рішення також має варіант `isVisible`, який "попередньо завантажує" зображення, які знаходяться в межах 1 сторінки вище/під поточною прокруткою документа.

0 commit comments

Comments
 (0)