๐ŸŒˆ์‚ฌ์ดํŠธ ๋งŒ๋“ค๊ธฐ_JS

choยท2023๋…„ 11์›” 27์ผ

โœ๏ธgetBoundingClientRect()

The

Element.getBoundingClientRect()

method returns a DOMRect object providing information about the size of an element and its position relative to the viewport.

Element.getBoundingClientRect() ๋ฉ”์„œ๋“œ๋Š” ์—˜๋ฆฌ๋จผํŠธ์˜ ํฌ๊ธฐ์™€ ๋ทฐํฌํŠธ์— ์ƒ๋Œ€์ ์ธ ์œ„์น˜ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” DOMRect ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

The returned value is a DOMRect object which is the smallest rectangle which contains the entire element, including its padding and border-width. The left, top, right, bottom, x, y, width, and height properties describe the position and size of the overall rectangle in pixels. Properties other than width and height are relative to the top-left of the viewport.

๋ฐ˜ํ™˜ ๊ฐ’์€ padding๊ณผ border-width๋ฅผ ํฌํ•จํ•ด ์ „์ฒด ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ๋“ค์–ด์žˆ๋Š” ๊ฐ€์žฅ ์ž‘์€ ์‚ฌ๊ฐํ˜•์ธ DOMRect ๊ฐ์ฒด์ด๋‹ค. left, top, right, bottom, x, y, width, height ํ”„๋กœํผํ‹ฐ๋Š” ์ „๋ฐ˜์ ์ธ ์‚ฌ๊ฐํ˜•์˜ ์œ„์น˜์™€ ํฌ๊ธฐ๋ฅผ ํ”ฝ์…€ ๋‹จ์œ„๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค. width์™€ height๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ํ”„๋กœํผํ‹ฐ๋Š” ๋ทฐํฌํŠธ์˜ top-left์— ์ƒ๋Œ€์ ์ด๋‹ค.

โœ๏ธaddEventListener()

๊ตฌ๋ฌธ

addEventListener(type, listener);
addEventListener(type, listener, options);
addEventListener(type, listener, useCapture);

type

์ˆ˜์‹ ํ•  ์ด๋ฒคํŠธ ์œ ํ˜• (en-US)์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ๋ฌธ์ž์—ด

listener

์ง€์ •ํ•œ ์ด๋ฒคํŠธ(Event ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ๊ฐ์ฒด)๋ฅผ ์ˆ˜์‹ ํ•  ๊ฐ์ฒด์ด๋‹ค. handleEvent() ๋ฉ”์„œ๋“œ๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฐ์ฒด ๋˜๋Š” JavaScript ํ•จ์ˆ˜์—ฌ์•ผ ํ•œ๋‹ค.

options (Optional)

์ด๋ฒคํŠธ ์ˆ˜์‹ ๊ธฐ์˜ ํŠน์ง•์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด์ด๋‹ค.

useCapture (Optional)

์ด๋ฒคํŠธ ๋Œ€์ƒ์˜ DOM ํŠธ๋ฆฌ ํ•˜์œ„์— ์œ„์น˜ํ•œ ์ž์† EventTarget์œผ๋กœ ์ด๋ฒคํŠธ๊ฐ€ ์ „๋‹ฌ๋˜๊ธฐ ์ „์—, ์ด ์ˆ˜์‹ ๊ธฐ๊ฐ€ ๋จผ์ € ๋ฐœ๋™๋ผ์•ผ ํ•จ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์ด๋‹ค. ์บก์ฒ˜ ๋ชจ๋“œ์ธ ์ˆ˜์‹ ๊ธฐ๋Š” DOM ํŠธ๋ฆฌ์˜ ์œ„์ชฝ์œผ๋กœ ๋ฒ„๋ธ”๋ง ์ค‘์ธ ์ด๋ฒคํŠธ์— ์˜ํ•ด์„  ๋ฐœ๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด๋ฒคํŠธ ๋ฒ„๋ธ”๋ง๊ณผ ์บก์ฒ˜๋ง์€ ์กฐ์ƒ-์ž์† ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง„ ๋‘ ๊ฐœ์˜ ์š”์†Œ๊ฐ€ ๋™์ผํ•œ ์ด๋ฒคํŠธ ์œ ํ˜•์— ๋Œ€ํ•œ ์ˆ˜์‹ ๊ธฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๋•Œ, ๋‘ ์š”์†Œ์— ์ด๋ฒคํŠธ๊ฐ€ ์ „ํŒŒ๋˜๋Š” ๋ฐฉ๋ฒ•์„ ๋งํ•œ๋‹ค. ์ด๋ฒคํŠธ ์ „ํŒŒ ๋ชจ๋“œ์— ๋”ฐ๋ผ ๋‘ ์š”์†Œ ์ค‘ ์ด๋ฒคํŠธ๋ฅผ ๋จผ์ € ์ˆ˜์‹ ํ•˜๋Š” ์ชฝ์ด ๋‹ฌ๋ผ์ง„๋‹ค. ๊ธฐ๋ณธ ๊ฐ’์€ false์ด๋‹ค.

The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target. Common targets are Element, or its children, Document, and Window, but the target may be any object that supports events (such as IDBRequest).

EventTarget ์ธํ„ฐํŽ˜์ด์Šค์˜ addEventListener() ๋ฉ”์„œ๋“œ๋Š” ์ง€์ •ํ•œ ์œ ํ˜•์˜ ์ด๋ฒคํŠธ๋ฅผ ๋Œ€์ƒ์ด ์ˆ˜์‹ ํ•  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœํ•  ํ•จ์ˆ˜๋ฅผ ์„ค์ •ํ•œ๋‹ค. ์ผ๋ฐ˜์ ์ธ ๋Œ€์ƒ์€ Element, Document, Window์ง€๋งŒ XMLHttpRequest์™€ ๊ฐ™์ด ์ด๋ฒคํŠธ๋ฅผ ์ง€์›ํ•˜๋Š” ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ๋Œ€์ƒ์ด ๋  ์ˆ˜ ์žˆ๋‹ค.

โœ๏ธclassList

The Element.classList is a read-only property that returns a live DOMTokenList collection of the class attributes of the element. This can then be used to manipulate the class list.

Element.classList ๋Š” ์—˜๋ฆฌ๋จผํŠธ์˜ ํด๋ž˜์Šค ์†์„ฑ์˜ ์ปฌ๋ ‰์…˜์ธ ํ™œ์„ฑ DOMTokenList (en-US)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ฝ๊ธฐ ์ „์šฉ ํ”„๋กœํผํ‹ฐ์ด๋‹ค.

Using classList is a convenient alternative to accessing an element's list of classes as a space-delimited string via element.className.

classList ์‚ฌ์šฉ์€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋œ ๋ฌธ์ž์—ด์ธ element.className์„ ํ†ตํ•ด ์—˜๋ฆฌ๋จผํŠธ์˜ ํด๋ž˜์Šค ๋ชฉ๋ก์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ์‹์„ ๋Œ€์ฒดํ•˜๋Š” ๊ฐ„ํŽธํ•œ ๋ฐฉ๋ฒ•์ด๋‹ค.

const elementClasses = elementNodeReference.classList;

A DOMTokenList representing the contents of the element's class attribute. If the class attribute is not set or empty, it returns an empty DOMTokenList, i.e. a DOMTokenList with the length property equal to 0.

Element.classList ๋Š” ์—˜๋ฆฌ๋จผํŠธ์˜ ํด๋ž˜์Šค ์†์„ฑ์˜ ์ปฌ๋ ‰์…˜์ธ ํ™œ์„ฑ DOMTokenList (en-US)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ฝ๊ธฐ ์ „์šฉ ํ”„๋กœํผํ‹ฐ์ด๋‹ค.

Although the classList property itself is read-only, you can modify its associated DOMTokenList using the add(), remove(), replace(), and toggle() methods.

classList ์‚ฌ์šฉ์€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋œ ๋ฌธ์ž์—ด์ธ element.className์„ ํ†ตํ•ด ์—˜๋ฆฌ๋จผํŠธ์˜ ํด๋ž˜์Šค ๋ชฉ๋ก์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ์‹์„ ๋Œ€์ฒดํ•˜๋Š” ๊ฐ„ํŽธํ•œ ๋ฐฉ๋ฒ•์ด๋‹ค.

method

add( String [, String [, ...]] )

์ง€์ •ํ•œ ํด๋ž˜์Šค ๊ฐ’์„ ์ถ”๊ฐ€ํ•œ๋‹ค. ๋งŒ์•ฝ ์ถ”๊ฐ€ํ•˜๋ ค๋Š” ํด๋ž˜์Šค๊ฐ€ ์—˜๋ฆฌ๋จผํŠธ์˜ class ์†์„ฑ์— ์ด๋ฏธ ์กด์žฌํ•œ๋‹ค๋ฉด ๋ฌด์‹œํ•œ๋‹ค.

remove( String [, String [, ...]] )

์ง€์ •ํ•œ ํด๋ž˜์Šค ๊ฐ’์„ ์ œ๊ฑฐํ•œ๋‹ค.

item( Number )

์ฝœ๋ ‰์…˜์˜ ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ํด๋ž˜์Šค ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

toggle( String [, force] )

ํ•˜๋‚˜์˜ ์ธ์ˆ˜๋งŒ ์žˆ์„ ๋•Œ: ํด๋ž˜์Šค ๊ฐ’์„ ํ† ๊ธ€๋งํ•œ๋‹ค. ์ฆ‰, ํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด ์ œ๊ฑฐํ•˜๊ณ  false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
๋‘๋ฒˆ์งธ ์ธ์ˆ˜๊ฐ€ ์žˆ์„ ๋•Œ: ๋‘๋ฒˆ์งธ ์ธ์ˆ˜๊ฐ€ true๋กœ ํ‰๊ฐ€๋˜๋ฉด ์ง€์ •ํ•œ ํด๋ž˜์Šค ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๊ณ  false๋กœ ํ‰๊ฐ€๋˜๋ฉด ์ œ๊ฑฐํ•œ๋‹ค.

contains( String )

์ง€์ •ํ•œ ํด๋ž˜์Šค ๊ฐ’์ด ์—˜๋ฆฌ๋จผํŠธ์˜ class ์†์„ฑ์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

replace( oldClass, newClass )

์กด์žฌํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ์ƒˆ๋กœ์šด ํด๋ž˜์Šค๋กœ ๊ต์ฒดํ•œ๋‹ค.

โญHeader์— ํŽ˜์ด์ง€ ์•„๋ž˜๋กœ ์Šคํฌ๋กค์‹œ ๋‹คํฌ ์Šคํƒ€์ผ๋ง ์ ์šฉ

const header = document.querySelector('.header');
const headerHeight = header.getBoundingClientRect().height;
document.addEventListener('scroll', () => {
    //์Šคํฌ๋กค ๋˜๋Š” Y์ขŒํ‘œ๊ฐ€ headerHeight๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๋‹ค๋ฅธ ์Šคํƒ€์ผ๋ง!
    if(window.scrollY > headerHeight) {
        header.classList.add('header--dark');
    } else {
        header.classList.remove('header--dark');
    }
})

ํ’€์–ด์„œ ์ž‘์„ฑ

const headerHeight = header.getBoundingClientRect().height;

์ด ๊ตฌ๋ฌธ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ํ’€์–ด์„œ ์ž‘์„ฑ ๊ฐ€๋Šฅํ•˜๋‹ค.

const header = document.querySelector('.header');
const headerRect = header.getBoundingClientRect();
console.log(headerRect);
const headerHeight = headerRect.height;
console.log(headerHeight);

0๊ฐœ์˜ ๋Œ“๊ธ€