Skip to content

Commit bf393d0

Browse files
authored
Merge pull request #433 from lgabrielvleon/lgabrielvleon-patch-1
Element size and scrolling
2 parents 2d9297b + 08f1899 commit bf393d0

File tree

10 files changed

+138
-138
lines changed

10 files changed

+138
-138
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
The solution is:
1+
La solución es:
22

33
```js
44
let scrollBottom = elem.scrollHeight - elem.scrollTop - elem.clientHeight;
55
```
66

7-
In other words: (full height) minus (scrolled out top part) minus (visible part) -- that's exactly the scrolled out bottom part.
7+
En otras palabras: (altura total) menos (parte superior desplazada) menos (parte visible) -- esa es exactamente la parte inferior desplazada.

2-ui/1-document/09-size-and-scroll/1-get-scroll-height-bottom/task.md

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

33
---
44

5-
# What's the scroll from the bottom?
5+
# ¿Qué es el desplazamiento desde la parte inferior?
66

7-
The `elem.scrollTop` property is the size of the scrolled out part from the top. How to get the size of the bottom scroll (let's call it `scrollBottom`)?
7+
La propiedad `elem.scrollTop` es el tamaño desplazado desde la parte superior. ¿Cómo obtener el tamaño de la parte inferior desplazada (vamos a llamarlo `scrollBottom`)?
88

9-
Write the code that works for an arbitrary `elem`.
9+
Escribe el código que funcione para un `elem` arbitrario.
1010

11-
P.S. Please check your code: if there's no scroll or the element is fully scrolled down, then it should return `0`.
11+
P.S. Por favor revisa tu código: si no hay desplazamiento o el elemento está complemamente desplazado, debería retornar `0`.

2-ui/1-document/09-size-and-scroll/2-scrollbar-width/solution.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
To get the scrollbar width, we can create an element with the scroll, but without borders and paddings.
1+
Para obtener el ancho de la barra de desplazamiento, podemos crear un elemento con el scroll, pero sin bordes ni rellenos.
22

3-
Then the difference between its full width `offsetWidth` and the inner content area width `clientWidth` will be exactly the scrollbar:
3+
Entonces la diferencia entre su ancho completo `offsetWidth` y el ancho del area interior `clientWidth` será exactamente la barra de desplazamiento:
44

55
```js run
6-
// create a div with the scroll
6+
// crea un div con el scroll
77
let div = document.createElement('div');
88

99
div.style.overflowY = 'scroll';
1010
div.style.width = '50px';
1111
div.style.height = '50px';
1212

13-
// must put it in the document, otherwise sizes will be 0
13+
// debe ponerlo en el documento, de lo contrario los tamaños serán 0
1414
document.body.append(div);
1515
let scrollWidth = div.offsetWidth - div.clientWidth;
1616

2-ui/1-document/09-size-and-scroll/2-scrollbar-width/task.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ importance: 3
22

33
---
44

5-
# What is the scrollbar width?
5+
# ¿Qué es el ancho de la barra de desplazamiento?
66

7-
Write the code that returns the width of a standard scrollbar.
7+
Escribe el código que retorna el tamaño de una barra de desplazamiento estándar.
88

9-
For Windows it usually varies between `12px` and `20px`. If the browser doesn't reserve any space for it (the scrollbar is half-translucent over the text, also happens), then it may be `0px`.
9+
Para Windows esto usualmente varía entre `12px` y `20px`. Si el navegador no reserva algún espacio para esto (la barra de desplazamiento es medio traslúcida sobre el texto, también pasa), entonces puede ser `0px`.
1010

11-
P.S. The code should work for any HTML document, do not depend on its content.
11+
P.S. El código debería funcionar con cualquier documento HTML, no depende de su contenido.

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
1-
The ball has `position:absolute`. It means that its `left/top` coordinates are measured from the nearest positioned element, that is `#field` (because it has `position:relative`).
1+
La pelota tiene `position:absolute`. Significa que sus coordenadas `left/top` se miden desde el elemento posicionado más cercano, es decir `#field` (porque tiene `position:relative`).
22

3-
The coordinates start from the inner left-upper corner of the field:
3+
Las coordenadas inician desde el interior de la esquina superior izquierda del campo:
44

55
![](field.svg)
66

7-
The inner field width/height is `clientWidth/clientHeight`. So the field center has coordinates `(clientWidth/2, clientHeight/2)`.
7+
El interior del campo ancho/alto es `clientWidth/clientHeight`. Entonces el centro del campo tiene coordenadas `(clientWidth/2, clientHeight/2)`.
88

9-
...But if we set `ball.style.left/top` to such values, then not the ball as a whole, but the left-upper edge of the ball would be in the center:
9+
...Pero si configuramos `ball.style.left/top` a tales valores, entonces no la pelota en su conjunto, sino el borde superior izquierdo de la pelota estaría en el centro:
1010

1111
```js
1212
ball.style.left = Math.round(field.clientWidth / 2) + 'px';
1313
ball.style.top = Math.round(field.clientHeight / 2) + 'px';
1414
```
1515

16-
Here's how it looks:
16+
Así es como se ve:
1717

1818
[iframe height=180 src="ball-half"]
1919

20-
To align the ball center with the center of the field, we should move the ball to the half of its width to the left and to the half of its height to the top:
20+
Para alinear la pelota al centro con el centro del campo, deberíamos mover la pelota a la mitad de su ancho a la izquierda y a la mitad de su altura hacia arriba:
2121

2222
```js
2323
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-
Now the ball is finally centered.
27+
Ahora la pelota está finalmente centrada.
2828

29-
````warn header="Attention: the pitfall!"
29+
````warn header="Atención: ¡la trampa!"
3030
31-
The code won't work reliably while `<img>` has no width/height:
31+
El código no funcionará seguramente mientras `<img>` no tenga width/height:
3232
3333
```html
3434
<img src="ball.png" id="ball">
3535
```
3636
````
3737

38-
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.
38+
Cuando el navegador no conoce el ancho/alto de una imagen (de un atributo o CSS), entonces este asume que es igual a `0` hasta que la imagen termine de cargarse.
3939

40-
So the value of `ball.offsetWidth` will be `0` until the image loads. That leads to wrong coordinates in the code above.
40+
Entonces el valor de `ball.offsetWidth` deberá ser `0` hasta que la imagen carge. Eso conduce a coordinadas incorrectas en el código anterior.
4141

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`.
42+
Después de la primera carga, el navegador usualmente almacena en caché la imagen, y cuando se vuelva a cargar esta tendrá el tamaño inmediatamente. Pero en la primera carga el valor de `ball.offsetWidth` es `0`.
4343

44-
We should fix that by adding `width/height` to `<img>`:
44+
Deberíamos arreglar eso agregando `width/height` en `<img>`:
4545

4646
```html
4747
<img src="ball.png" *!*width="40" height="40"*/!* id="ball">
4848
```
4949

50-
...Or provide the size in CSS:
50+
...O indicar el tamaño en CSS:
5151

5252
```css
5353
#ball {

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

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

33
---
44

5-
# Place the ball in the field center
5+
# Coloca la pelota en el centro del campo.
66

7-
Here's how the source document looks:
7+
Así es como se ve el documento de origen:
88

99
[iframe src="source" edit link height=180]
1010

11-
What are coordinates of the field center?
11+
¿Cuáles son las coordenadas del centro de campo?
1212

13-
Calculate them and use to place the ball into the center of the green field:
13+
Calcúlalos y úsalos para colocar la pelota en el centro del campo verde:
1414

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

17-
- The element should be moved by JavaScript, not CSS.
18-
- The code should work with any ball size (`10`, `20`, `30` pixels) and any field size, not be bound to the given values.
17+
- El elemento debe ser movido por JavaScript, no por CSS.
18+
- El código debería funcionar con cualquier una pelota de cualquier tamaño (`10`, `20`, `30` pixels) y cualquier tamaño de campo, no debe estar vinculado a los valores dados.
1919

20-
P.S. Sure, centering could be done with CSS, but here we want exactly JavaScript. Further we'll meet other topics and more complex situations when JavaScript must be used. Here we do a "warm-up".
20+
P.S. Claro, el centrado se podría hacer con CSS, pero aquí lo queremos específicamente con JavaScript. Además, conoceremos otros temas y situaciones más complejas en las que se debe utilizar JavaScript. Aquí hacemos un "calentamiento".
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Differences:
1+
Diferencias:
22

3-
1. `clientWidth` is numeric, while `getComputedStyle(elem).width` returns a string with `px` at the end.
4-
2. `getComputedStyle` may return non-numeric width like `"auto"` for an inline element.
5-
3. `clientWidth` is the inner content area of the element plus paddings, while CSS width (with standard `box-sizing`) is the inner content area *without paddings*.
6-
4. If there's a scrollbar and the browser reserves the space for it, some browser substract that space from CSS width (cause it's not available for content any more), and some do not. The `clientWidth` property is always the same: scrollbar size is substracted if reserved.
3+
1. `clientWidth` es numérico, mientras `getComputedStyle(elem).width` retorna una cadena con `px` en el final.
4+
2. `getComputedStyle` puede devolver un ancho no numérico como `"auto"` para un elemento en linea.
5+
3. `clientWidth` es el contenido interior del área del elemento más los rellenos, mientras el ancho de CSS (con el estándar `box-sizing`) es el contenido interior del área *sin rellenos*.
6+
4. Si hay una barra de desplazamiento y el navegador reserva espacio para esta, algunos navegadores restan ese espacio del ancho de CSS (por que no está disponible para el contenido), y otros no. La propiedad `clientWidth` es siempre la misma: el tamaño de la barra de desplazamiento se resta si está reservado.

2-ui/1-document/09-size-and-scroll/6-width-vs-clientwidth/task.md

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

33
---
44

5-
# The difference: CSS width versus clientWidth
5+
# La diferencia: CSS width versus clientWidth
66

7-
What's the difference between `getComputedStyle(elem).width` and `elem.clientWidth`?
7+
¿Cuál es la diferencia entre `getComputedStyle(elem).width` y `elem.clientWidth`?
88

9-
Give at least 3 differences. The more the better.
9+
Dar al menos 3 diferencias. Mientras más, mejor.

0 commit comments

Comments
 (0)