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
Чтобы изменить это, нам нужно использовать другую систему координат, где сообщение позиционировалось бы относительно документа, и свойство `position:absolute`.
153
153
154
-
## Координаты в контексте документа
154
+
## Координаты относительно документа
155
155
156
-
Document-relative coordinates start from the upper-left corner of the document, not the window.
156
+
В такой системе координат отсчёт ведётся от левого верхнего угла документа, не окна.
157
157
158
-
In CSS, window coordinates correspond to `position:fixed`, while document coordinates are similar to `position:absolute` on top.
158
+
В CSS координаты относительно окна браузера соответствуют свойству `position:fixed`, а координаты относительно документа -- свойству `position:absolute` на самом верхнем уровне вложенности.
159
159
160
-
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.
160
+
Мы можем воспользоваться свойствами `position:absolute` и `top/left`, чтобы привязать что-нибудь к конкретному месту в документе. При этом прокрутка страницы не имеет значения. Но сначала нужно получить верные координаты.
161
161
162
-
For clarity we'll call window coordinates `(clientX,clientY)` and document coordinates `(pageX,pageY)`.
162
+
Для ясности обозначим координаты в контексте окна как `(clientX,clientY)` и в контексте документа как `(pageX,pageY)`.
163
163
164
-
When the page is not scrolled, then window coordinate and document coordinates are actually the same. Their zero points match too:
164
+
Если страница не прокручена, то координаты в обеих системах совпадают, равно как и точка отсчёта:
165
165
166
166

167
167
168
-
And if we scroll it, then `(clientX,clientY)` change, because they are relative to the window, but `(pageX,pageY)` remain the same.
168
+
Но при прокрутке значения `(clientX,clientY)` меняются, потому что они привязаны к окну браузера, а `(pageX,pageY)` остаются такими же.
169
169
170
-
Here's the same page after the vertical scroll:
170
+
Вот та же самая страница после вертикальной прокрутки:
171
171
172
172

173
173
174
-
- `clientY` of the header `"From today's featured article"` became `0`, because the element is now on window top.
175
-
- `clientX` didn't change, as we didn't scroll horizontally.
176
-
- `pageX` and `pageY` coordinates of the element are still the same, because they are relative to the document.
174
+
- значение `clientY` заголовка `"From today's featured article"` стало равным `0`, потому что верхний край элемента достиг верхней границы окна.
175
+
- значение `clientX` не поменялось, так как не было горизонтальной прокрутки.
176
+
- значения координат `pageX` и `pageY` тоже не изменились, потому что они берутся относительно документа.
177
177
178
-
## Getting document coordinates [#getCoords]
178
+
## Получаем координаты в контексте документа [#getCoords]
179
179
180
-
There's no standard method to get the document coordinates of an element. But it's easy to write it.
180
+
Не существует стандартного метода, который возвращал бы координаты элемента относительно документа, но мы можем написать его сами.
181
181
182
-
The two coordinate systems are connected by the formula:
183
-
- `pageY` = `clientY` + height of the scrolled-out vertical part of the document.
184
-
- `pageX` = `clientX` + width of the scrolled-out horizontal part of the document.
182
+
Две системы координат связаны следующими формулами:
183
+
- `pageY` = `clientY` + высота вертикально прокрученной части документа.
184
+
- `pageX` = `clientX` + ширина горизонтально прокрученной части документа.
185
185
186
-
The function `getCoords(elem)` will take window coordinates from `elem.getBoundingClientRect()` and add the current scroll to them:
186
+
Функция `getCoords(elem)` берёт коордианты в контексте окна с помощью `elem.getBoundingClientRect()` и добавляет к ним значение соответствующей прокрутки:
187
187
188
188
```js
189
-
// get document coordinates of the element
189
+
// получаем координаты элемента в контексте документа
0 commit comments