Skip to content

Commit 5c2ce31

Browse files
[브라우저 창 사이즈와 스크롤] 번역
1 parent 1fc53d0 commit 5c2ce31

File tree

1 file changed

+65
-65
lines changed
  • 2-ui/1-document/10-size-and-scroll-window

1 file changed

+65
-65
lines changed
Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
1-
# Window sizes and scrolling
1+
# 브라우저 창 사이즈와 스크롤
22

3-
How do we find the width and height of the browser window? How do we get the full width and height of the document, including the scrolled out part? How do we scroll the page using JavaScript?
3+
브라우저 창이 차지하는 너비와 높이를 어떻게 구할 수 있을까요? 스크롤 때문에 보이지 않는 영역을 포함하여 문서 전체가 차지하는 너비와 높이는 어떻게 구할 수 있을까요? 자바스크립트를 사용해서 페이지를 스크롤 할 수 있을까요?
44

5-
For this type of information, we can use the root document element `document.documentElement`, that corresponds to the `<html>` tag. But there are additional methods and peculiarities to consider.
5+
이번 챕터에선 위와 같은 물음에 답을 주는 루트 문서 요소인 `document.documentElement`를 살펴볼 예정입니다. `document.documentElement``<html>` 태그와 상응하는 요소로 다양한 메서드를 지원합니다. 유용한 메서드이긴 하지만 몇 가지 주의할 점이 있어 같이 살펴봅시다.
66

7-
## Width/height of the window
7+
## 브라우저 창의 너비와 높이
88

9-
To get window width and height, we can use the `clientWidth/clientHeight` of `document.documentElement`:
9+
창이 차지하는 너비와 높이를 알려면 `document.documentElement``clientWidth``clientHeight`를 사용하면 됩니다.
1010

1111
![](document-client-width-height.svg)
1212

1313
```online
14-
For instance, this button shows the height of your window:
14+
아래 버튼을 눌러 직접 창 높이를 출력해보세요.
1515
1616
<button onclick="alert(document.documentElement.clientHeight)">alert(document.documentElement.clientHeight)</button>
1717
```
1818

19-
````warn header="Not `window.innerWidth/innerHeight`"
20-
Browsers also support properties like `window.innerWidth/innerHeight`. They look like what we want, so why not to use them instead?
19+
````warn header="`window` 객체가 아닌 `document.documentElement`를 쓰는 이유"
20+
브라우저의 `window` 객체 역시 `innerWidth``innerHeight` 프로퍼티를 지원합니다. 이 프로퍼티를 써도 원하는 대로 창 크기를 구할 수 있을 것 같은데 왜 `document.documentElement``clientWidth``clientHeight`를 쓰는 걸까요?
2121

22-
If there exists a scrollbar, and it occupies some space, `clientWidth/clientHeight` provide the width/height without it (subtract it). In other words, they return the width/height of the visible part of the document, available for the content.
22+
스크롤바가 생기면 스크롤바 역시 공간을 차지하는데, `clientWidth``clientHeight`는 스크롤바가 차지하는 공간을 제외해서 너비나 높이 값을 계산합니다. 눈에 보이는 문서에서 콘텐츠가 실제로 들어가게 될 영역의 너비와 높이 값을 반환하는 것이죠.
2323

24-
`window.innerWidth/innerHeight` includes the scrollbar.
24+
그런데 `window.innerWidth/innerHeight`는 스크롤바가 차지하는 영역을 포함해 값을 계산합니다.
2525

26-
If there's a scrollbar, and it occupies some space, then these two lines show different values:
26+
스크롤바가 있는 경우 스크롤 바 역시 공간을 차지하는데, 이럴 때 `window`객체와 `document.documentElement`의 해당 프로퍼티들은 다른 값을 반환합니다.
2727
```js run
28-
alert( window.innerWidth ); // full window width
29-
alert( document.documentElement.clientWidth ); // window width minus the scrollbar
28+
alert( window.innerWidth ); // 전체 창 너비
29+
alert( document.documentElement.clientWidth ); // 스크롤바가 차지하는 영역을 제외한 창 너비
3030
```
3131

32-
In most cases, we need the *available* window width in order to draw or position something within scrollbars (if there are any), so we should use `documentElement.clientHeight/clientWidth`.
32+
창 사이즈가 필요한 경우는 스크롤 바 안쪽에 무언가를 그리거나 위치시킬 때가 대다수입니다. 따라서 `documentElement``clientHeight/clientWidth`를 써야 합니다.
3333
````
3434
35-
```warn header="`DOCTYPE` is important"
36-
Please note: top-level geometry properties may work a little bit differently when there's no `<!DOCTYPE HTML>` in HTML. Odd things are possible.
35+
```warn header="`DOCTYPE`을 꼭 써주세요."
36+
기하 관련 프로퍼티는 HTML에 문서에 `<!DOCTYPE HTML>`이 명시되어있지 않으면 이상하게 동작할 때가 있습니다. 정확하지 않거나 근거를 알 수 없는 값이 툭 튀어나올 수 있죠.
3737
38-
In modern HTML we should always write `DOCTYPE`.
38+
그러니 앞으로는 항상 HTML에 `DOCTYPE`을 명시하도록 합시다.
3939
```
4040
41-
## Width/height of the document
41+
## 문서의 너비와 높이
4242
43-
Theoretically, as the root document element is `document.documentElement`, and it encloses all the content, we could measure the document's full size as `document.documentElement.scrollWidth/scrollHeight`.
43+
이론상 `document.documentElement`는 문서의 루트 요소에 상응하고, 루트 요소엔 콘텐츠 전부가 들어가기 때문에 우리는 문서의 전체 크기를 `document.documentElement`의 `scrollWidth`와 `scrollHeight`를 사용해 재면 되지 않냐고 생각합니다.
4444
45-
But on that element, for the whole page, these properties do not work as intended. In Chrome/Safari/Opera, if there's no scroll, then `documentElement.scrollHeight` may be even less than `documentElement.clientHeight`! Weird, right?
45+
그런데 전체 페이지를 대상으로 했을 때, `document.documentElement`의 프로피터들은 우리가 예상한 대로 동작하지 않습니다. Chrome이나 Safari, Opera에서 스크롤이 없는 경우 `documentElement.scrollHeight``documentElement.clientHeight`보다 작을 때가 있죠. 예상하기엔 같은 값이어야 하는데도 말입니다.
4646
47-
To reliably obtain the full document height, we should take the maximum of these properties:
47+
정확한 문서 전체 높이 값을 얻으려면 아래 여섯 프로퍼티가 반환하는 값 중 최댓값을 골라야 합니다.
4848
4949
```js run
5050
let scrollHeight = Math.max(
@@ -53,104 +53,104 @@ let scrollHeight = Math.max(
5353
document.body.clientHeight, document.documentElement.clientHeight
5454
);
5555
56-
alert('Full document height, with scrolled out part: ' + scrollHeight);
56+
alert('스크롤에 의해 가려진 분을 포함한 전체 문서 높이: ' + scrollHeight);
5757
```
5858
59-
Why so? Better don't ask. These inconsistencies come from ancient times, not a "smart" logic.
59+
그렇다면 왜 이런 방식으로 문서 전체 높이를 구해야 하는 걸까요? 이유는 알아보지 않는 게 낫습니다. 이런 이상한 계산법은 아주 오래 전부터 있었고 그다지 논리적이지 않은 이유로 만들어졌기 때문입니다.
6060
61-
## Get the current scroll [#page-scroll]
61+
## 스크롤 정보 얻기 [#page-scroll]
6262
63-
DOM elements have their current scroll state in their `scrollLeft/scrollTop` properties.
63+
DOM 요소의 현재 스크롤 상태(스크롤에 의해 가려진 영역에 대한 정보)는 `scrollLeft`와 `scrollTop` 프로퍼티를 통해 구할 수 있습니다.
6464
65-
For document scroll, `document.documentElement.scrollLeft/scrollTop` works in most browsers, except older WebKit-based ones, like Safari (bug [5991](https://bugs.webkit.org/show_bug.cgi?id=5991)), where we should use `document.body` instead of `document.documentElement`.
65+
대부분의 브라우저에서 문서의 스크롤 상태는 `document.documentElement`의 `scrollLeft`나 `scrollTop`을 이용해 구할 수 있습니다. 다만 구버전 WebKit을 기반으로 하는 브라우저에선 버그([5991](https://bugs.webkit.org/show_bug.cgi?id=5991)) 때문에 `document.documentElement`가 아닌 `document.body`를 사용해야 원하는 값을 구할 수 있습니다.
6666
67-
Luckily, we don't have to remember these peculiarities at all, because the scroll is available in the special properties, `window.pageXOffset/pageYOffset`:
67+
이쯤 되면 스크롤 포지션 정보를 구하기 위해 브라우저별 예외처리까지 다 해야 하나 라는 생각이 들 수 있을 겁니다. 다행히도 `window`객체의 `pageXOffset`과 `pageYOffset`을 사용하면 브라우저 상관없이 스크롤 정보를 구할 수 있어서 이런 예외 상황을 외워두지 않아도 됩니다.
6868
6969
```js run
70-
alert('Current scroll from the top: ' + window.pageYOffset);
71-
alert('Current scroll from the left: ' + window.pageXOffset);
70+
alert('세로 스크롤에 의해 가려진 위쪽 영역 높이: ' + window.pageYOffset);
71+
alert('가로 스크롤에 의해 가려진 왼쪽 영역 너비: ' + window.pageXOffset);
7272
```
7373
74-
These properties are read-only.
74+
참고로 이 두 프로퍼티는 읽기만 가능합니다.
7575
76-
## Scrolling: scrollTo, scrollBy, scrollIntoView [#window-scroll]
76+
## scrollTo, scrollBy로 스크롤 상태 변경하기 [#window-scroll]
7777
7878
```warn
79-
To scroll the page with JavaScript, its DOM must be fully built.
79+
자바스크립트를 사용해 스크롤을 움직이려면 DOM이 완전히 만들어진 상태이어야 합니다.
8080
81-
For instance, if we try to scroll the page with a script in `<head>`, it won't work.
81+
`<head>`에 있는 스크립트에서 페이지 전체의 스크롤을 움직이려 하면 잘 동작하지 않을 수 있습니다.
8282
```
8383
84-
Regular elements can be scrolled by changing `scrollTop/scrollLeft`.
84+
일반 요소의 스크롤 상태는 `scrollTop`이나 `scrollLeft`로 쉽게 변경할 수 있습니다.
8585
86-
We can do the same for the page using `document.documentElement.scrollTop/scrollLeft` (except Safari, where `document.body.scrollTop/Left` should be used instead).
86+
페이지 전체의 스크롤 상태 역시 `document.documentElement`의 `scrollTop/scrollLeft`를 사용해 변경 가능하죠(다만, Safari는 `document.body`의 `scrollTop/scrollLeft`를 써야 합니다).
8787
88-
Alternatively, there's a simpler, universal solution: special methods [window.scrollBy(x,y)](mdn:api/Window/scrollBy) and [window.scrollTo(pageX,pageY)](mdn:api/Window/scrollTo).
88+
그런데 이보다 더 편하고 브라우저 상관없이 쓸 수 있는 대안이 있긴합니다. 바로 [window.scrollBy(x,y)](mdn:api/Window/scrollBy)[window.scrollTo(pageX,pageY)](mdn:api/Window/scrollTo)입니다.
8989
90-
- The method `scrollBy(x,y)` scrolls the page *relative to its current position*. For instance, `scrollBy(0,10)` scrolls the page `10px` down.
90+
- `scrollBy(x,y)`메서드를 사용하면 페이지의 스크롤 상태를 현재 포지션을 기준으로 상대적으로 조정합니다. `scrollBy(0,10)`는 문서의 스크롤 상태를 현재를 기준으로 스크롤을 `10px`아래로 내린것 처럼 움직여주죠.
9191
9292
```online
93-
The button below demonstrates this:
93+
아래 버튼을 눌러 직접 실습해봅시다.
9494
9595
<button onclick="window.scrollBy(0,10)">window.scrollBy(0,10)</button>
9696
```
97-
- The method `scrollTo(pageX,pageY)` scrolls the page *to absolute coordinates*, so that the top-left corner of the visible part has coordinates `(pageX, pageY)` relative to the document's top-left corner. It's like setting `scrollLeft/scrollTop`.
97+
- 반면 `scrollTo(pageX,pageY)`메서드는 *절대 좌표*를 기준으로 페이지 스크롤 상태를 변경합니다. 따라서 눈에 보이는 영역의 왼쪽 위 모서리의 좌표가 문서 전체의 왼쪽 위 모서리를 기준으로 `(pageX, pageY)`가 됩니다. 마치 `scrollLeft`와 `scrollTop` 값을 변경한 것처럼 움직이는 거죠.
9898
99-
To scroll to the very beginning, we can use `scrollTo(0,0)`.
99+
그래서 `scrollTo(0,0)`을 호출하면 문서 스크롤 상태를 처음 상태로 되돌릴 수 있습니다.
100100
101101
```online
102102
<button onclick="window.scrollTo(0,0)">window.scrollTo(0,0)</button>
103103
```
104104
105-
These methods work for all browsers the same way.
105+
그리고 이 두 메서드는 브라우저 종류에 상관없이 동일한 동작을 보장합니다.
106106
107107
## scrollIntoView
108108
109-
For completeness, let's cover one more method: [elem.scrollIntoView(top)](mdn:api/Element/scrollIntoView).
109+
추가 메서드 [elem.scrollIntoView(top)](mdn:api/Element/scrollIntoView)를 머릿속에 추가해 스크롤 상태를 완벽히 마스터 해봅시다.
110110
111-
The call to `elem.scrollIntoView(top)` scrolls the page to make `elem` visible. It has one argument:
111+
`elem.scrollIntoView(top)`를 호출하면 전체 페이지 스크롤이 움직여 `elem`이 눈에 보이는 상태로 변경됩니다. `elem.scrollIntoView`는 인수를 하나 받는데, 인수에 따라 다음과 같이 동작합니다.
112112
113-
- If `top=true` (that's the default), then the page will be scrolled to make `elem` appear on the top of the window. The upper edge of the element will be aligned with the window top.
114-
- If `top=false`, then the page scrolls to make `elem` appear at the bottom. The bottom edge of the element will be aligned with the window bottom.
113+
- `top`이 `true`(디폴트)인 경우, `elem`이 창 제일 위에 보이도록 스크롤 상태가 변경됩니다. `elem`의 위쪽 모서리가 창의 위쪽 모서리와 일치하게 되죠.
114+
- `top`이 `false`인 경우, `elem`이 창 가장 아래에 보이도록 스크롤 상태가 변경됩니다. `elem`의 아래쪽 모서리가 창의 아래쪽 모서리와 일치하게 변합니다.
115115
116116
```online
117-
The button below scrolls the page to position itself at the window top:
117+
버튼을 눌러 직접 실습해봅시다. 첫 번째 버튼을 누르면 버튼 상단이 창 제일 꼭대기로 붙는 것을 확인할 수 있습니다.
118118
119119
<button onclick="this.scrollIntoView()">this.scrollIntoView()</button>
120120
121-
And this button scrolls the page to position itself at the bottom:
121+
두 번째 버튼을 누르면 버튼의 아래 모서리가 창 밑으로 붙는 것을 확인할 수 있습니다.
122122
123123
<button onclick="this.scrollIntoView(false)">this.scrollIntoView(false)</button>
124124
```
125125
126-
## Forbid the scrolling
126+
## 스크롤 막기
127127
128-
Sometimes we need to make the document "unscrollable". For instance, when we need to cover the page with a large message requiring immediate attention, and we want the visitor to interact with that message, not with the document.
128+
때에 따라 문서 스크롤바를 '고정' 해야 하는 경우가 생기곤 합니다. 사용자에게 반드시 전달해야 하는 중요한 메시지가 있어서 이 메시지를 화면에 크게 띄우고, 사용자가 스크롤을 움직여 다른 콘텐츠를 보지 못하게 한 상태에서 메시지를 읽게 하려는 경우가 대표적인 예가 될 수 있습니다.
129129
130-
To make the document unscrollable, it's enough to set `document.body.style.overflow = "hidden"`. The page will "freeze" at its current scroll position.
130+
이럴 때 `document.body.style.overflow = "hidden"`를 사용할 수 있습니다. 해당 스크립트가 동작하면 페이지의 스크롤바 위치가 '고정' 됩니다.
131131
132132
```online
133-
Try it:
133+
직접 실습해봅시다.
134134
135135
<button onclick="document.body.style.overflow = 'hidden'">document.body.style.overflow = 'hidden'</button>
136136
137137
<button onclick="document.body.style.overflow = ''">document.body.style.overflow = ''</button>
138138
139-
The first button freezes the scroll, while the second one releases it.
139+
위쪽 버튼을 누르면 스크롤바가 고정되었다가, 아래 버튼을 누르면 고정이 해제되는 것을 확인할 수 있습니다.
140140
```
141141
142-
We can use the same technique to freeze the scroll for other elements, not just for `document.body`.
142+
이 방법은 `document.body`요소 뿐만 아니라 다른 요소의 스크롤을 고정시킬 때도 사용할 수 있습니다.
143143
144-
The drawback of the method is that the scrollbar disappears. If it occupied some space, then that space is now free and the content "jumps" to fill it.
144+
그런데 이 방법은 스크롤바가 사라진다는 단점이 있습니다. 스크롤바는 일정 공간을 차지하는데, 스크롤바가 사라지면 해당 공간을 채우기 위해 콘텐츠가 갑자기 '움직이는' 현상이 발생합니다.
145145
146-
That looks a bit odd, but can be worked around if we compare `clientWidth` before and after the freeze. If it increased (the scrollbar disappeared), then add `padding` to `document.body` in place of the scrollbar to keep the content width the same.
146+
이렇게 페이지 전체의 스크롤 상태가 갑자기 변경되면 사용자 입장에선 이상해 보일 수 있기 때문에 개발자는 스크롤바를 고정시키기 전과 후의 `clientWidth`값을 비교해서 해당 증상을 보정해야 합니다. 스크롤바가 사라질 땐 `clientWidth`값이 커지는데 이때 스크롤바가 차지했던 영역만큼 `document.body`에 `padding`을 줘서 콘텐츠 전체의 너비를 스크롤바가 사라지기 전과 같은 값으로 유지할 수 있습니다.
147147
148-
## Summary
148+
## 요약
149149
150-
Geometry:
150+
기하 프로퍼티:
151151
152-
- Width/height of the visible part of the document (content area width/height): `document.documentElement.clientWidth/clientHeight`
153-
- Width/height of the whole document, with the scrolled out part:
152+
- 사용자 눈에 보이는 문서(콘텐츠가 실제 보여지는 영역)의 너비와 높이: `document.documentElement.clientWidth/clientHeight`
153+
- 스크롤에 의해 가려진 영역을 포함한 문서 전체의 너비와 높이:
154154
155155
```js
156156
let scrollHeight = Math.max(
@@ -160,11 +160,11 @@ Geometry:
160160
);
161161
```
162162
163-
Scrolling:
163+
스크롤 관련 프로퍼티:
164164
165-
- Read the current scroll: `window.pageYOffset/pageXOffset`.
166-
- Change the current scroll:
165+
- 현재 스크롤 정보 읽기: `window.pageYOffset/pageXOffset`.
166+
- 스크롤 상태 변경시키기:
167167
168-
- `window.scrollTo(pageX,pageY)` -- absolute coordinates,
169-
- `window.scrollBy(x,y)` -- scroll relative the current place,
170-
- `elem.scrollIntoView(top)` -- scroll to make `elem` visible (align with the top/bottom of the window).
168+
- `window.scrollTo(pageX,pageY)` -- 절대 좌표
169+
- `window.scrollBy(x,y)` -- 현재 스크롤 상태를 기준으로 상대적으로 스크롤 정보 변경
170+
- `elem.scrollIntoView(top)` -- `elem`이 눈에 보이도록 스크롤 상태 변경(인수에 따라 창 최상단, 최하단에 해당 요소가 노출되도록 처리)

0 commit comments

Comments
 (0)