|
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 | +El núcleo de la solución es una función que añade más fechas a la página (o carga más cosas en la vida real) mientras estamos en el final de la página. |
2 | 2 |
|
3 |
| -We can call it immediately and add as a `window.onscroll` handler. |
| 3 | +Podemos llamarlo inmediatamente o agregarlo como un manejador de `window.onscroll`. |
4 | 4 |
|
5 |
| -The most important question is: "How do we detect that the page is scrolled to bottom?" |
| 5 | +La pregunta más importante es: "¿Cómo detectamos que la página se desplaza hasta el fondo?" |
6 | 6 |
|
7 |
| -Let's use window-relative coordinates. |
| 7 | +Usaremos las coordenadas de la ventana. |
8 | 8 |
|
9 |
| -The document is represented (and contained) within `<html>` tag, that is `document.documentElement`. |
| 9 | +El documento está representado (y contenido) dentro de la etiqueta `<html>`, que es `document.documentElement`. |
10 | 10 |
|
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 | +Podemos obtener las coordenadas relativas a la ventana de todo el documento como `document.documentElement.getBoundingClientRect()`, la propiedad `bottom` será la coordenada relativa a la ventana del fondo del documento. |
12 | 12 |
|
13 |
| -For instance, if the height of the whole HTML document is `2000px`, then: |
| 13 | +Por ejemplo, si la altura de todo el documento es `2000px`, entonces: |
14 | 14 |
|
15 | 15 | ```js
|
16 |
| -// when we're on the top of the page |
17 |
| -// window-relative top = 0 |
| 16 | +// cuando estamos en la parte superior de la página |
| 17 | +// window-relative top = 0 (relativo a la ventana, límite superior = 0 ) |
18 | 18 | document.documentElement.getBoundingClientRect().top = 0
|
19 | 19 |
|
20 |
| -// window-relative bottom = 2000 |
21 |
| -// the document is long, so that is probably far beyond the window bottom |
| 20 | +// window-relative bottom = 2000 (relativo a la ventana, límite inferior = 2000) |
| 21 | +// el documento es largo, así que probablemente esté más allá del fondo de la ventana |
22 | 22 | document.documentElement.getBoundingClientRect().bottom = 2000
|
23 | 23 | ```
|
24 | 24 |
|
25 |
| -If we scroll `500px` below, then: |
| 25 | +Si nos desplazamos `500px` abajo, entonces: |
26 | 26 |
|
27 | 27 | ```js
|
28 |
| -// document top is above the window 500px |
| 28 | +// la parte superior del documento está 500px por encima de la ventana |
29 | 29 | document.documentElement.getBoundingClientRect().top = -500
|
30 |
| -// document bottom is 500px closer |
| 30 | +// la parte inferior del documento está 500px más cerca |
31 | 31 | document.documentElement.getBoundingClientRect().bottom = 1500
|
32 | 32 | ```
|
33 | 33 |
|
34 |
| -When we scroll till the end, assuming that the window height is `600px`: |
| 34 | +Cuando nos desplazamos hasta el final, asumiendo que la altura de la venta es `600px`: |
35 | 35 |
|
36 | 36 |
|
37 | 37 | ```js
|
38 |
| -// document top is above the window 1400px |
| 38 | +// La parte superior del documento está 1400px sobre la ventana |
39 | 39 | document.documentElement.getBoundingClientRect().top = -1400
|
40 |
| -// document bottom is below the window 600px |
| 40 | +// la parte inferior del documento está a 600px debajo de la ventana |
41 | 41 | document.documentElement.getBoundingClientRect().bottom = 600
|
42 | 42 | ```
|
43 | 43 |
|
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 | +Tened en cuenta que el fondo del documento `bottom` nunca puede ser `0`, porque nunca llega a la parte superior de la ventana. El límite más bajo de la coordenada `bottom` es la altura de la ventana (asumimos que es `600`), no podemos desplazarla más hacia arriba. |
45 | 45 |
|
46 |
| -We can obtain the window height as `document.documentElement.clientHeight`. |
| 46 | +Podemos obtener la altura de la ventana con `document.documentElement.clientHeight`. |
47 | 47 |
|
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 | +Para nuestra tarea, necesitamos saber cuando tenemos el final del documento a unos `100px` (esto es: `600-700px`, si la altura es de `600`). |
49 | 49 |
|
50 |
| -So here's the function: |
| 50 | +Así que aquí está la función: |
51 | 51 |
|
52 | 52 | ```js
|
53 | 53 | function populate() {
|
54 | 54 | while(true) {
|
55 |
| - // document bottom |
| 55 | + // final del documento |
56 | 56 | let windowRelativeBottom = document.documentElement.getBoundingClientRect().bottom;
|
57 | 57 |
|
58 |
| - // if the user scrolled far enough (<100px to the end) |
| 58 | + // si el usuario se desplaza lo suficiente (<100px hasta el final) |
59 | 59 | if (windowRelativeBottom < document.documentElement.clientHeight + 100) {
|
60 |
| - // let's add more data |
| 60 | + // vamos añadir más datos |
61 | 61 | document.body.insertAdjacentHTML("beforeend", `<p>Date: ${new Date()}</p>`);
|
62 | 62 | }
|
63 | 63 | }
|
|
0 commit comments