๐ŸŸจ JavaScript/๊ฐœ๋…

[์ฝ”๋”ฉ์• ํ”Œ] ์š”์†Œ์˜ ๋†’์ด์™€ ์œ„์น˜๊ฐ’ ๊ตฌํ•˜๊ธฐ (clientHeight, offsetHeight, scrollHeight, getBoundingClientRect())

Zoeeey 2023. 1. 11. 12:55

clientHeight

console.log(element.clientHeight);

clientHeight๋Š” ํŒจ๋”ฉ์„ ํฌํ•จํ•œ ์š”์†Œ์˜ ๋†’์ด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

offsetHeight

console.log(element.offsetHeight);

clientHeight๋Š” ํŒจ๋”ฉ, ํ…Œ๋‘๋ฆฌ, ๊ฐ€๋กœ์Šคํฌ๋กค๋ฐ”(์žˆ๋Š” ๊ฒฝ์šฐ)์„ ํฌํ•จํ•œ ์š”์†Œ์˜ ๋†’์ด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

scrollHeight

console.log(element.scrollHeight);

scrollHeight๋Š” ํŒจ๋”ฉ, overflow๋กœ ์ธํ•ด ํ™”๋ฉด์— ํ‘œ์‹œ๋˜์ง€ ์•Š์€ ์ปจํ…์ธ ์„ ํฌํ•จํ•œ ์š”์†Œ์˜ ๋†’์ด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.


getBoundingClientRect()

console.log(element.getBoundingClientRect());

// ์ถœ๋ ฅ๊ฒฐ๊ณผ
{
  top: 134
  y: 134 //top๊ณผ ๋™์ผ
  bottom: 178
  left: 212.5
  x: 212.5 //left์™€ ๋™์ผ
  right: 1092.5
  width: 880
  height: 44
}

getBoundingClientRect() ๋ฉ”์†Œ๋“œ๋Š” ์œˆ๋„์šฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•œ ์š”์†Œ์˜ ์œ„์น˜/๋„ˆ๋น„/๋†’์ด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.


๋ฌธ์„œ ์ „์ฒด์˜ ๋†’์ด ๊ตฌํ•˜๊ธฐ

let scrollHeight = Math.max(
    document.body.scrollHeight, document.documentElement.scrollHeight,
    document.body.offsetHeight, document.documentElement.offsetHeight,
    document.body.clientHeight, document.documentElement.clientHeight
  );

์ถœ์ฒ˜ : ์ฝ”๋”ฉ์• ํ”Œ https://codingapple.com/ / MDN https://developer.mozilla.org/ko/