👉 window
👉document
👉 navigator
👉 window.screen
👉 window.outer
👉 window.inner
👉 documentElement.clientWidth
window 객체가 아닌 document.documentElement를 쓰는 이유?
=>>
window.innerWidth/innerHeight는 스크롤바가 차지하는 영역을 포함해 값을 계산합니다. 스크롤바가 있는 경우 스크롤 바 역시 공간을 차지하는데, 이럴 때 window객체와 document.documentElement의 해당 프로퍼티들은 다른 값을 반환합니다.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
.tag {
background-color: powderblue;
width: 1000px;
height: 500px;
font-size: 40px;
}
</style>
<body>
<div class="tag">window size</div>
<script>
const tag = document.querySelector(".tag");
function updateTag() {
tag.innerHTML = `screen size: ${screen.width}, ${screen.height} <br/>
window outer: ${window.outerHeight}, ${window.outerWidth} <br/>
window inner: ${window.innerHeight}, ${window.innerWidth} <br/>
document : ${document.documentElement.clientHeight}, ${document.documentElement.clientWidth}`;
}
window.addEventListener("resize", () => {
updateTag();
});
updateTag();
</script>
</body>
</html>
참고자료
브라우저 창 사이즈와 스크롤 https://ko.javascript.info/size-and-scroll-window
mdn Document.documentElement https://developer.mozilla.org/ko/docs/Web/API/Document/documentElement
드림코딩 프론트엔드 필수 브라우저 101