diff --git a/2-ui/1-document/11-coordinates/1-find-point-coordinates/solution.md b/2-ui/1-document/11-coordinates/1-find-point-coordinates/solution.md index 4101d4915..2513b6f04 100644 --- a/2-ui/1-document/11-coordinates/1-find-point-coordinates/solution.md +++ b/2-ui/1-document/11-coordinates/1-find-point-coordinates/solution.md @@ -1,8 +1,8 @@ -# Outer corners +# Cantos externos -Outer corners are basically what we get from [elem.getBoundingClientRect()](https://developer.mozilla.org/en-US/docs/DOM/element.getBoundingClientRect). +Os cantos externos são basicamente o que obtemos de [elem.getBoundingClientRect()](https://developer.mozilla.org/en-US/docs/DOM/element.getBoundingClientRect). -Coordinates of the upper-left corner `answer1` and the bottom-right corner `answer2`: +Coordenadas do canto superior esquerdo `answer1` e do canto inferior direito `answer2`: ```js let coords = elem.getBoundingClientRect(); @@ -11,19 +11,19 @@ let answer1 = [coords.left, coords.top]; let answer2 = [coords.right, coords.bottom]; ``` -# Left-upper inner corner +# Canto interno superior esquerdo -That differs from the outer corner by the border width. A reliable way to get the distance is `clientLeft/clientTop`: +Esse difere do canto externo pela largura da borda. Uma forma confiável de obter a distância é `clientLeft/clientTop`: ```js let answer3 = [coords.left + field.clientLeft, coords.top + field.clientTop]; ``` -# Right-bottom inner corner +# Canto interno inferior direito -In our case we need to substract the border size from the outer coordinates. +No nosso caso precisamos subtrair o tamanho da borda das coordenadas externas. -We could use CSS way: +Poderíamos usar a abordagem via CSS: ```js let answer4 = [ @@ -32,7 +32,7 @@ let answer4 = [ ]; ``` -An alternative way would be to add `clientWidth/clientHeight` to coordinates of the left-upper corner. That's probably even better: +Uma forma alternativa seria adicionar `clientWidth/clientHeight` às coordenadas do canto superior esquerdo. Isso é provavelmente ainda melhor: ```js let answer4 = [ diff --git a/2-ui/1-document/11-coordinates/1-find-point-coordinates/solution.view/index.html b/2-ui/1-document/11-coordinates/1-find-point-coordinates/solution.view/index.html index 229c87186..7e2795413 100755 --- a/2-ui/1-document/11-coordinates/1-find-point-coordinates/solution.view/index.html +++ b/2-ui/1-document/11-coordinates/1-find-point-coordinates/solution.view/index.html @@ -5,7 +5,7 @@ @@ -13,10 +13,10 @@ - Click anywhere to get window coordinates. -
That's for testing, to check the result you get by JavaScript. + Clique em qualquer lugar para obter as coordenadas da janela. +
Isso é para teste, para verificar o resultado que você obtém via JavaScript.
-
(click coordinates show up here)
+
(as coordenadas do clique aparecem aqui)
diff --git a/2-ui/1-document/11-coordinates/1-find-point-coordinates/source.view/index.html b/2-ui/1-document/11-coordinates/1-find-point-coordinates/source.view/index.html index dd168f783..7d4347169 100755 --- a/2-ui/1-document/11-coordinates/1-find-point-coordinates/source.view/index.html +++ b/2-ui/1-document/11-coordinates/1-find-point-coordinates/source.view/index.html @@ -5,7 +5,7 @@ @@ -13,10 +13,10 @@ - Click anywhere to get window coordinates. -
That's for testing, to check the result you get by JavaScript. + Clique em qualquer lugar para obter as coordenadas da janela. +
Isso é para teste, para verificar o resultado que você obtém via JavaScript.
-
(click coordinates show up here)
+
(as coordenadas do clique aparecem aqui)
@@ -32,7 +32,7 @@ diff --git a/2-ui/1-document/11-coordinates/1-find-point-coordinates/task.md b/2-ui/1-document/11-coordinates/1-find-point-coordinates/task.md index 6bbb9fe13..9c47ae88b 100644 --- a/2-ui/1-document/11-coordinates/1-find-point-coordinates/task.md +++ b/2-ui/1-document/11-coordinates/1-find-point-coordinates/task.md @@ -2,23 +2,23 @@ importance: 5 --- -# Find window coordinates of the field +# Encontre as coordenadas da janela do campo -In the iframe below you can see a document with the green "field". +No iframe abaixo você pode ver um documento com o "campo" verde. -Use JavaScript to find window coordinates of corners pointed by with arrows. +Use JavaScript para encontrar as coordenadas da janela dos cantos apontados pelas setas. -There's a small feature implemented in the document for convenience. A click at any place shows coordinates there. +Há um pequeno recurso implementado no documento para conveniência. Um clique em qualquer lugar mostra as coordenadas ali. [iframe border=1 height=360 src="source" link edit] -Your code should use DOM to get window coordinates of: +Seu código deve usar o DOM para obter as coordenadas da janela de: -1. Upper-left, outer corner (that's simple). -2. Bottom-right, outer corner (simple too). -3. Upper-left, inner corner (a bit harder). -4. Bottom-right, inner corner (there are several ways, choose one). +1. Canto externo superior esquerdo (isso é simples). +2. Canto externo inferior direito (também simples). +3. Canto interno superior esquerdo (um pouco mais difícil). +4. Canto interno inferior direito (há várias formas, escolha uma). -The coordinates that you calculate should be the same as those returned by the mouse click. +As coordenadas que você calcular devem ser as mesmas retornadas pelo clique do mouse. -P.S. The code should also work if the element has another size or border, not bound to any fixed values. +P.S. O código também deve funcionar se o elemento tiver outro tamanho ou borda, sem ficar amarrado a nenhum valor fixo. diff --git a/2-ui/1-document/11-coordinates/2-position-at/solution.md b/2-ui/1-document/11-coordinates/2-position-at/solution.md index 353eb65dd..32d422f3e 100644 --- a/2-ui/1-document/11-coordinates/2-position-at/solution.md +++ b/2-ui/1-document/11-coordinates/2-position-at/solution.md @@ -1,4 +1,4 @@ -In this task we only need to accurately calculate the coordinates. See the code for details. +Nesta tarefa só precisamos calcular as coordenadas com precisão. Veja o código para os detalhes. -Please note: the elements must be in the document to read `offsetHeight` and other properties. -A hidden (`display:none`) or out of the document element has no size. +Note: os elementos precisam estar no documento para ler `offsetHeight` e outras propriedades. +Um elemento oculto (`display:none`) ou fora do documento não tem tamanho. diff --git a/2-ui/1-document/11-coordinates/2-position-at/solution.view/index.html b/2-ui/1-document/11-coordinates/2-position-at/solution.view/index.html index f931fbeac..ffdfd6746 100755 --- a/2-ui/1-document/11-coordinates/2-position-at/solution.view/index.html +++ b/2-ui/1-document/11-coordinates/2-position-at/solution.view/index.html @@ -12,10 +12,10 @@ esse sequi officia sapiente.

- Teacher: Why are you late? - Student: There was a man who lost a hundred dollar bill. - Teacher: That's nice. Were you helping him look for it? - Student: No. I was standing on it. + Professor: Por que você está atrasado? + Aluno: Tinha um homem que perdeu uma nota de cem dólares. + Professor: Que legal. Você estava ajudando ele a procurar? + Aluno: Não. Eu estava em cima dela.

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reprehenderit sint atque dolorum fuga ad incidunt voluptatum error fugiat animi amet! Odio temporibus nulla id unde quaerat dignissimos enim nisi rem provident molestias sit tempore omnis recusandae @@ -24,13 +24,13 @@ diff --git a/2-ui/1-document/11-coordinates/2-position-at/source.view/index.html b/2-ui/1-document/11-coordinates/2-position-at/source.view/index.html index 675573450..df0511f39 100755 --- a/2-ui/1-document/11-coordinates/2-position-at/source.view/index.html +++ b/2-ui/1-document/11-coordinates/2-position-at/source.view/index.html @@ -12,10 +12,10 @@ esse sequi officia sapiente.

- Teacher: Why are you late? - Student: There was a man who lost a hundred dollar bill. - Teacher: That's nice. Were you helping him look for it? - Student: No. I was standing on it. + Professor: Por que você está atrasado? + Aluno: Tinha um homem que perdeu uma nota de cem dólares. + Professor: Que legal. Você estava ajudando ele a procurar? + Aluno: Não. Eu estava em cima dela.

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reprehenderit sint atque dolorum fuga ad incidunt voluptatum error fugiat animi amet! Odio temporibus nulla id unde quaerat dignissimos enim nisi rem provident molestias sit tempore omnis recusandae @@ -24,21 +24,21 @@ diff --git a/2-ui/1-document/11-coordinates/2-position-at/task.md b/2-ui/1-document/11-coordinates/2-position-at/task.md index 3aaa47f03..4de3d01d9 100644 --- a/2-ui/1-document/11-coordinates/2-position-at/task.md +++ b/2-ui/1-document/11-coordinates/2-position-at/task.md @@ -2,17 +2,17 @@ importance: 5 --- -# Show a note near the element +# Mostre uma nota perto do elemento -Create a function `positionAt(anchor, position, elem)` that positions `elem`, depending on `position` near `anchor` element. +Crie uma função `positionAt(anchor, position, elem)` que posiciona `elem`, dependendo de `position`, perto do elemento `anchor`. -The `position` must be a string with any one of 3 values: -- `"top"` - position `elem` right above `anchor` -- `"right"` - position `elem` immediately at the right of `anchor` -- `"bottom"` - position `elem` right below `anchor` +O `position` deve ser uma string com um dos 3 valores: +- `"top"` - posiciona `elem` logo acima de `anchor` +- `"right"` - posiciona `elem` imediatamente à direita de `anchor` +- `"bottom"` - posiciona `elem` logo abaixo de `anchor` -It's used inside function `showNote(anchor, position, html)`, provided in the task source code, that creates a "note" element with given `html` and shows it at the given `position` near the `anchor`. +Ela é usada dentro da função `showNote(anchor, position, html)`, fornecida no código fonte da tarefa, que cria um elemento de "nota" com o `html` dado e o mostra na `position` dada, perto do `anchor`. -Here's the demo of notes: +Aqui está a demonstração das notas: [iframe src="solution" height="350" border="1" link] diff --git a/2-ui/1-document/11-coordinates/3-position-at-absolute/solution.md b/2-ui/1-document/11-coordinates/3-position-at-absolute/solution.md index 014e505ed..485615982 100644 --- a/2-ui/1-document/11-coordinates/3-position-at-absolute/solution.md +++ b/2-ui/1-document/11-coordinates/3-position-at-absolute/solution.md @@ -1,4 +1,4 @@ -The solution is actually pretty simple: +A solução, na verdade, é bem simples: -- Use `position:absolute` in CSS instead of `position:fixed` for `.note`. -- Use the function [getCoords()](info:coordinates#getCoords) from the chapter to get document-relative coordinates. +- Use `position:absolute` no CSS em vez de `position:fixed` para `.note`. +- Use a função [getCoords()](info:coordinates#getCoords) do capítulo para obter as coordenadas relativas ao documento. diff --git a/2-ui/1-document/11-coordinates/3-position-at-absolute/solution.view/index.html b/2-ui/1-document/11-coordinates/3-position-at-absolute/solution.view/index.html index 56c95d5ec..5b82a4eb4 100644 --- a/2-ui/1-document/11-coordinates/3-position-at-absolute/solution.view/index.html +++ b/2-ui/1-document/11-coordinates/3-position-at-absolute/solution.view/index.html @@ -12,10 +12,10 @@ esse sequi officia sapiente.

- Teacher: Why are you late? - Student: There was a man who lost a hundred dollar bill. - Teacher: That's nice. Were you helping him look for it? - Student: No. I was standing on it. + Professor: Por que você está atrasado? + Aluno: Tinha um homem que perdeu uma nota de cem dólares. + Professor: Que legal. Você estava ajudando ele a procurar? + Aluno: Não. Eu estava em cima dela.

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reprehenderit sint atque dolorum fuga ad incidunt voluptatum error fugiat animi amet! Odio temporibus nulla id unde quaerat dignissimos enim nisi rem provident molestias sit tempore omnis recusandae @@ -66,12 +66,12 @@ positionAt(anchor, position, note); } - // test it + // teste let blockquote = document.querySelector('blockquote'); - showNote(blockquote, "top", "note above"); - showNote(blockquote, "right", "note at the right"); - showNote(blockquote, "bottom", "note below"); + showNote(blockquote, "top", "nota acima"); + showNote(blockquote, "right", "nota à direita"); + showNote(blockquote, "bottom", "nota abaixo"); diff --git a/2-ui/1-document/11-coordinates/3-position-at-absolute/task.md b/2-ui/1-document/11-coordinates/3-position-at-absolute/task.md index 0554da862..886f33402 100644 --- a/2-ui/1-document/11-coordinates/3-position-at-absolute/task.md +++ b/2-ui/1-document/11-coordinates/3-position-at-absolute/task.md @@ -2,10 +2,10 @@ importance: 5 --- -# Show a note near the element (absolute) +# Mostre uma nota perto do elemento (absolute) -Modify the solution of the [previous task](info:task/position-at) so that the note uses `position:absolute` instead of `position:fixed`. +Modifique a solução da [tarefa anterior](info:task/position-at) para que a nota use `position:absolute` em vez de `position:fixed`. -That will prevent its "runaway" from the element when the page scrolls. +Isso vai impedir que ela "escape" do elemento quando a página rolar. -Take the solution of that task as a starting point. To test the scroll, add the style ``. +Use a solução daquela tarefa como ponto de partida. Para testar a rolagem, adicione o estilo ``. diff --git a/2-ui/1-document/11-coordinates/4-position-inside-absolute/solution.view/index.html b/2-ui/1-document/11-coordinates/4-position-inside-absolute/solution.view/index.html index b89db3790..bd440a6ef 100644 --- a/2-ui/1-document/11-coordinates/4-position-inside-absolute/solution.view/index.html +++ b/2-ui/1-document/11-coordinates/4-position-inside-absolute/solution.view/index.html @@ -12,10 +12,10 @@ esse sequi officia sapiente.

- Teacher: Why are you late? - Student: There was a man who lost a hundred dollar bill. - Teacher: That's nice. Were you helping him look for it? - Student: No. I was standing on it. + Professor: Por que você está atrasado? + Aluno: Tinha um homem que perdeu uma nota de cem dólares. + Professor: Que legal. Você estava ajudando ele a procurar? + Aluno: Não. Eu estava em cima dela.

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reprehenderit sint atque dolorum fuga ad incidunt voluptatum error fugiat animi amet! Odio temporibus nulla id unde quaerat dignissimos enim nisi rem provident molestias sit tempore omnis recusandae @@ -83,10 +83,10 @@ let blockquote = document.querySelector('blockquote'); - showNote(blockquote, "top-in", "note top-in"); - showNote(blockquote, "top-out", "note top-out"); - showNote(blockquote, "right-out", "note right-out"); - showNote(blockquote, "bottom-in", "note bottom-in"); + showNote(blockquote, "top-in", "nota top-in"); + showNote(blockquote, "top-out", "nota top-out"); + showNote(blockquote, "right-out", "nota right-out"); + showNote(blockquote, "bottom-in", "nota bottom-in"); diff --git a/2-ui/1-document/11-coordinates/4-position-inside-absolute/task.md b/2-ui/1-document/11-coordinates/4-position-inside-absolute/task.md index de45b5498..60cfb3ea2 100644 --- a/2-ui/1-document/11-coordinates/4-position-inside-absolute/task.md +++ b/2-ui/1-document/11-coordinates/4-position-inside-absolute/task.md @@ -2,27 +2,27 @@ importance: 5 --- -# Position the note inside (absolute) +# Posicione a nota dentro (absolute) -Extend the previous task : teach the function `positionAt(anchor, position, elem)` to insert `elem` inside the `anchor`. +Estenda a tarefa anterior : ensine a função `positionAt(anchor, position, elem)` a inserir `elem` dentro do `anchor`. -New values for `position`: +Novos valores para `position`: -- `top-out`, `right-out`, `bottom-out` -- work the same as before, they insert the `elem` over/right/under `anchor`. -- `top-in`, `right-in`, `bottom-in` -- insert `elem` inside the `anchor`: stick it to the upper/right/bottom edge. +- `top-out`, `right-out`, `bottom-out` -- funcionam como antes, eles inserem o `elem` acima/à direita/abaixo de `anchor`. +- `top-in`, `right-in`, `bottom-in` -- inserem `elem` dentro do `anchor`: colam-no na borda superior/direita/inferior. -For instance: +Por exemplo: ```js -// shows the note above blockquote +// mostra a nota acima de blockquote positionAt(blockquote, "top-out", note); -// shows the note inside blockquote, at the top +// mostra a nota dentro de blockquote, no topo positionAt(blockquote, "top-in", note); ``` -The result: +O resultado: [iframe src="solution" height="310" border="1" link] -As the source code, take the solution of the task . +Como código fonte, use a solução da tarefa . diff --git a/2-ui/1-document/11-coordinates/article.md b/2-ui/1-document/11-coordinates/article.md index fc605c414..2cc0581c5 100644 --- a/2-ui/1-document/11-coordinates/article.md +++ b/2-ui/1-document/11-coordinates/article.md @@ -1,42 +1,42 @@ -# Coordinates +# Coordenadas -To move elements around we should be familiar with coordinates. +Para mover elementos de um lugar para outro, devemos estar familiarizados com coordenadas. -Most JavaScript methods deal with one of two coordinate systems: +A maioria dos métodos JavaScript trabalha com um de dois sistemas de coordenadas: -1. **Relative to the window** - similar to `position:fixed`, calculated from the window top/left edge. - - we'll denote these coordinates as `clientX/clientY`, the reasoning for such name will become clear later, when we study event properties. -2. **Relative to the document** - similar to `position:absolute` in the document root, calculated from the document top/left edge. - - we'll denote them `pageX/pageY`. +1. **Relativo à janela** - parecido com `position:fixed`, calculado a partir da borda superior/esquerda da janela. + - vamos chamar essas coordenadas de `clientX/clientY`, o motivo desse nome vai ficar claro mais adiante, quando estudarmos as propriedades de eventos. +2. **Relativo ao documento** - parecido com `position:absolute` na raiz do documento, calculado a partir da borda superior/esquerda do documento. + - vamos chamá-las de `pageX/pageY`. -When the page is scrolled to the very beginning, so that the top/left corner of the window is exactly the document top/left corner, these coordinates equal each other. But after the document shifts, window-relative coordinates of elements change, as elements move across the window, while document-relative coordinates remain the same. +Quando a página está rolada até o início, de forma que o canto superior/esquerdo da janela é exatamente o canto superior/esquerdo do documento, essas coordenadas são iguais entre si. Mas depois que o documento é deslocado, as coordenadas relativas à janela dos elementos mudam, pois os elementos se movem pela janela, enquanto as coordenadas relativas ao documento permanecem as mesmas. -On this picture we take a point in the document and demonstrate its coordinates before the scroll (left) and after it (right): +Nesta imagem, pegamos um ponto no documento e demonstramos suas coordenadas antes da rolagem (esquerda) e depois dela (direita): ![](document-and-window-coordinates-scrolled.svg) -When the document scrolled: -- `pageY` - document-relative coordinate stayed the same, it's counted from the document top (now scrolled out). -- `clientY` - window-relative coordinate did change (the arrow became shorter), as the same point became closer to window top. +Quando o documento rolou: +- `pageY` - a coordenada relativa ao documento permaneceu a mesma, ela é contada a partir do topo do documento (agora fora da área visível). +- `clientY` - a coordenada relativa à janela mudou (a flecha ficou mais curta), já que o mesmo ponto ficou mais perto do topo da janela. -## Element coordinates: getBoundingClientRect +## Coordenadas do elemento: getBoundingClientRect -The method `elem.getBoundingClientRect()` returns window coordinates for a minimal rectangle that encloses `elem` as an object of built-in [DOMRect](https://www.w3.org/TR/geometry-1/#domrect) class. +O método `elem.getBoundingClientRect()` retorna as coordenadas na janela de um retângulo mínimo que envolve `elem`, como um objeto da classe interna [DOMRect](https://www.w3.org/TR/geometry-1/#domrect). -Main `DOMRect` properties: +Principais propriedades do `DOMRect`: -- `x/y` -- X/Y-coordinates of the rectangle origin relative to window, -- `width/height` -- width/height of the rectangle (can be negative). +- `x/y` -- coordenadas X/Y da origem do retângulo, relativas à janela, +- `width/height` -- largura/altura do retângulo (podem ser negativas). -Additionally, there are derived properties: +Além disso, há propriedades derivadas: -- `top/bottom` -- Y-coordinate for the top/bottom rectangle edge, -- `left/right` -- X-coordinate for the left/right rectangle edge. +- `top/bottom` -- coordenada Y da borda superior/inferior do retângulo, +- `left/right` -- coordenada X da borda esquerda/direita do retângulo. ```online -For instance click this button to see its window coordinates: +Por exemplo, clique neste botão para ver suas coordenadas na janela: -

+

-If you scroll the page and repeat, you'll notice that as window-relative button position changes, its window coordinates (`y/top/bottom` if you scroll vertically) change as well. +Se você rolar a página e repetir, vai notar que, conforme a posição do botão relativa à janela muda, suas coordenadas na janela (`y/top/bottom` se você rolar verticalmente) também mudam. ``` -Here's the picture of `elem.getBoundingClientRect()` output: +Aqui está a imagem da saída de `elem.getBoundingClientRect()`: ![](coordinates.svg) -As you can see, `x/y` and `width/height` fully describe the rectangle. Derived properties can be easily calculated from them: +Como você pode ver, `x/y` e `width/height` descrevem completamente o retângulo. As propriedades derivadas podem ser facilmente calculadas a partir delas: - `left = x` - `top = y` - `right = x + width` - `bottom = y + height` -Please note: +Note: -- Coordinates may be decimal fractions, such as `10.5`. That's normal, internally browser uses fractions in calculations. We don't have to round them when setting to `style.left/top`. -- Coordinates may be negative. For instance, if the page is scrolled so that `elem` is now above the window, then `elem.getBoundingClientRect().top` is negative. +- As coordenadas podem ser frações decimais, como `10.5`. Isso é normal, internamente o navegador usa frações nos cálculos. Não precisamos arredondá-las ao definir `style.left/top`. +- As coordenadas podem ser negativas. Por exemplo, se a página for rolada de forma que `elem` fique agora acima da janela, então `elem.getBoundingClientRect().top` é negativo. -```smart header="Why derived properties are needed? Why does `top/left` exist if there's `x/y`?" -Mathematically, a rectangle is uniquely defined with its starting point `(x,y)` and the direction vector `(width,height)`. So the additional derived properties are for convenience. +```smart header="Por que as propriedades derivadas são necessárias? Por que `top/left` existem se já há `x/y`?" +Matematicamente, um retângulo é definido de forma única pelo seu ponto inicial `(x,y)` e pelo vetor de direção `(width,height)`. Então as propriedades derivadas adicionais existem por conveniência. -Technically it's possible for `width/height` to be negative, that allows for "directed" rectangle, e.g. to represent mouse selection with properly marked start and end. +Tecnicamente é possível que `width/height` sejam negativos, o que permite um retângulo "direcionado", por exemplo, para representar uma seleção de mouse com início e fim marcados corretamente. -Negative `width/height` values mean that the rectangle starts at its bottom-right corner and then "grows" left-upwards. +Valores negativos de `width/height` significam que o retângulo começa no seu canto inferior direito e então "cresce" para a esquerda e para cima. -Here's a rectangle with negative `width` and `height` (e.g. `width=-200`, `height=-100`): +Aqui está um retângulo com `width` e `height` negativos (por exemplo, `width=-200`, `height=-100`): ![](coordinates-negative.svg) -As you can see, `left/top` do not equal `x/y` in such case. +Como você pode ver, `left/top` não são iguais a `x/y` nesse caso. -In practice though, `elem.getBoundingClientRect()` always returns positive width/height, here we mention negative `width/height` only for you to understand why these seemingly duplicate properties are not actually duplicates. +Na prática, porém, `elem.getBoundingClientRect()` sempre retorna width/height positivos, aqui mencionamos `width/height` negativos apenas para você entender por que essas propriedades aparentemente duplicadas não são, na verdade, duplicadas. ``` -```warn header="Internet Explorer: no support for `x/y`" -Internet Explorer doesn't support `x/y` properties for historical reasons. +```warn header="Internet Explorer: sem suporte para `x/y`" +O Internet Explorer não suporta as propriedades `x/y` por razões históricas. -So we can either make a polyfill (add getters in `DomRect.prototype`) or just use `top/left`, as they are always the same as `x/y` for positive `width/height`, in particular in the result of `elem.getBoundingClientRect()`. +Então podemos fazer um polyfill (adicionar getters em `DomRect.prototype`) ou simplesmente usar `top/left`, já que eles são sempre iguais a `x/y` para `width/height` positivos, em particular no resultado de `elem.getBoundingClientRect()`. ``` -```warn header="Coordinates right/bottom are different from CSS position properties" -There are obvious similarities between window-relative coordinates and CSS `position:fixed`. +```warn header="As coordenadas right/bottom são diferentes das propriedades de posição do CSS" +Há semelhanças óbvias entre as coordenadas relativas à janela e o `position:fixed` do CSS. -But in CSS positioning, `right` property means the distance from the right edge, and `bottom` property means the distance from the bottom edge. +Mas no posicionamento CSS, a propriedade `right` significa a distância a partir da borda direita, e a propriedade `bottom` significa a distância a partir da borda inferior. -If we just look at the picture above, we can see that in JavaScript it is not so. All window coordinates are counted from the top-left corner, including these ones. +Se olharmos para a imagem acima, podemos ver que em JavaScript não é assim. Todas as coordenadas na janela são contadas a partir do canto superior esquerdo, incluindo essas. ``` ## elementFromPoint(x, y) [#elementFromPoint] -The call to `document.elementFromPoint(x, y)` returns the most nested element at window coordinates `(x, y)`. +A chamada a `document.elementFromPoint(x, y)` retorna o elemento mais aninhado nas coordenadas da janela `(x, y)`. -The syntax is: +A sintaxe é: ```js let elem = document.elementFromPoint(x, y); ``` -For instance, the code below highlights and outputs the tag of the element that is now in the middle of the window: +Por exemplo, o código abaixo destaca e exibe a tag do elemento que está agora no meio da janela: ```js run let centerX = document.documentElement.clientWidth / 2; @@ -124,43 +124,43 @@ elem.style.background = "red"; alert(elem.tagName); ``` -As it uses window coordinates, the element may be different depending on the current scroll position. +Como ele usa coordenadas da janela, o elemento pode ser diferente dependendo da posição atual de rolagem. -````warn header="For out-of-window coordinates the `elementFromPoint` returns `null`" -The method `document.elementFromPoint(x,y)` only works if `(x,y)` are inside the visible area. +````warn header="Para coordenadas fora da janela, `elementFromPoint` retorna `null`" +O método `document.elementFromPoint(x,y)` só funciona se `(x,y)` estiverem dentro da área visível. -If any of the coordinates is negative or exceeds the window width/height, then it returns `null`. +Se qualquer uma das coordenadas for negativa ou exceder a largura/altura da janela, então ele retorna `null`. -Here's a typical error that may occur if we don't check for it: +Aqui está um erro típico que pode ocorrer se não verificarmos isso: ```js let elem = document.elementFromPoint(x, y); -// if the coordinates happen to be out of the window, then elem = null +// se as coordenadas estiverem fora da janela, então elem = null *!* -elem.style.background = ''; // Error! +elem.style.background = ''; // Erro! */!* ``` ```` -## Using for "fixed" positioning +## Usando para posicionamento "fixed" -Most of time we need coordinates in order to position something. +Na maioria das vezes precisamos de coordenadas para posicionar algo. -To show something near an element, we can use `getBoundingClientRect` to get its coordinates, and then CSS `position` together with `left/top` (or `right/bottom`). +Para mostrar algo próximo a um elemento, podemos usar `getBoundingClientRect` para obter suas coordenadas, e então usar o `position` do CSS junto com `left/top` (ou `right/bottom`). -For instance, the function `createMessageUnder(elem, html)` below shows the message under `elem`: +Por exemplo, a função `createMessageUnder(elem, html)` abaixo mostra a mensagem abaixo de `elem`: ```js let elem = document.getElementById("coords-show-mark"); function createMessageUnder(elem, html) { - // create message element + // cria o elemento da mensagem let message = document.createElement('div'); - // better to use a css class for the style here + // é melhor usar uma classe css para o estilo aqui message.style.cssText = "position:fixed; color: red"; *!* - // assign coordinates, don't forget "px"! + // atribui as coordenadas, não esqueça o "px"! let coords = elem.getBoundingClientRect(); message.style.left = coords.left + "px"; @@ -172,45 +172,45 @@ function createMessageUnder(elem, html) { return message; } -// Usage: -// add it for 5 seconds in the document -let message = createMessageUnder(elem, 'Hello, world!'); +// Uso: +// adiciona-a ao documento por 5 segundos +let message = createMessageUnder(elem, 'Olá, mundo!'); document.body.append(message); setTimeout(() => message.remove(), 5000); ``` ```online -Click the button to run it: +Clique no botão para executar: - + ``` -The code can be modified to show the message at the left, right, below, apply CSS animations to "fade it in" and so on. That's easy, as we have all the coordinates and sizes of the element. +O código pode ser modificado para mostrar a mensagem à esquerda, à direita, abaixo, aplicar animações CSS para "aparecer suavemente" e assim por diante. Isso é fácil, já que temos todas as coordenadas e tamanhos do elemento. -But note the important detail: when the page is scrolled, the message flows away from the button. +Mas note o detalhe importante: quando a página é rolada, a mensagem se afasta do botão. -The reason is obvious: the message element relies on `position:fixed`, so it remains at the same place of the window while the page scrolls away. +O motivo é óbvio: o elemento da mensagem depende de `position:fixed`, então ele permanece no mesmo lugar da janela enquanto a página rola. -To change that, we need to use document-based coordinates and `position:absolute`. +Para mudar isso, precisamos usar coordenadas baseadas no documento e `position:absolute`. -## Document coordinates [#getCoords] +## Coordenadas do documento [#getCoords] -Document-relative coordinates start from the upper-left corner of the document, not the window. +As coordenadas relativas ao documento começam a partir do canto superior esquerdo do documento, não da janela. -In CSS, window coordinates correspond to `position:fixed`, while document coordinates are similar to `position:absolute` on top. +No CSS, as coordenadas da janela correspondem a `position:fixed`, enquanto as coordenadas do documento são parecidas com `position:absolute` aplicado no nível superior do documento. -We can use `position:absolute` and `top/left` to put something at a certain place of the document, so that it remains there during a page scroll. But we need the right coordinates first. +Podemos usar `position:absolute` e `top/left` para colocar algo em um certo lugar do documento, de forma que permaneça lá durante a rolagem da página. Mas primeiro precisamos das coordenadas corretas. -There's no standard method to get the document coordinates of an element. But it's easy to write it. +Não há um método padrão para obter as coordenadas de um elemento relativas ao documento. Mas é fácil escrevê-lo. -The two coordinate systems are connected by the formula: -- `pageY` = `clientY` + height of the scrolled-out vertical part of the document. -- `pageX` = `clientX` + width of the scrolled-out horizontal part of the document. +Os dois sistemas de coordenadas são conectados pela fórmula: +- `pageY` = `clientY` + altura da parte vertical do documento que foi rolada para fora da área visível. +- `pageX` = `clientX` + largura da parte horizontal do documento que foi rolada para fora da área visível. -The function `getCoords(elem)` will take window coordinates from `elem.getBoundingClientRect()` and add the current scroll to them: +A função `getCoords(elem)` vai pegar as coordenadas da janela a partir de `elem.getBoundingClientRect()` e adicionar a rolagem atual a elas: ```js -// get document coordinates of the element +// obtém as coordenadas do elemento relativas ao documento function getCoords(elem) { let box = elem.getBoundingClientRect(); @@ -223,9 +223,9 @@ function getCoords(elem) { } ``` -If in the example above we used it with `position:absolute`, then the message would stay near the element on scroll. +Se no exemplo acima a usássemos com `position:absolute`, então a mensagem permaneceria perto do elemento durante a rolagem. -The modified `createMessageUnder` function: +A função `createMessageUnder` modificada: ```js function createMessageUnder(elem, html) { @@ -243,13 +243,13 @@ function createMessageUnder(elem, html) { } ``` -## Summary +## Resumo -Any point on the page has coordinates: +Qualquer ponto na página tem coordenadas: -1. Relative to the window -- `elem.getBoundingClientRect()`. -2. Relative to the document -- `elem.getBoundingClientRect()` plus the current page scroll. +1. Relativas à janela -- `elem.getBoundingClientRect()`. +2. Relativas ao documento -- `elem.getBoundingClientRect()` mais a rolagem atual da página. -Window coordinates are great to use with `position:fixed`, and document coordinates do well with `position:absolute`. +As coordenadas da janela são ótimas para usar com `position:fixed`, e as coordenadas do documento funcionam bem com `position:absolute`. -Both coordinate systems have their pros and cons; there are times we need one or the other one, just like CSS `position` `absolute` and `fixed`. +Ambos os sistemas de coordenadas têm seus prós e contras; há momentos em que precisamos de um ou de outro, assim como `position` `absolute` e `fixed` do CSS. diff --git a/2-ui/1-document/11-coordinates/head.html b/2-ui/1-document/11-coordinates/head.html index c00340039..d0d7802e0 100644 --- a/2-ui/1-document/11-coordinates/head.html +++ b/2-ui/1-document/11-coordinates/head.html @@ -3,7 +3,7 @@ let elem = document.getElementById('coords-show-mark'); - // no elem in ebook (pdf/epub) mode + // sem elem no modo ebook (pdf/epub) if (elem) { elem.onclick = function() { @@ -20,7 +20,7 @@ return message; } - let message = createMessageUnder(elem, 'Hello, world!'); + let message = createMessageUnder(elem, 'Olá, mundo!'); document.body.append(message); setTimeout(() => message.remove(), 5000); }