[JS] Scroll Event

Chenยท2024๋…„ 6์›” 12์ผ
0

Javascript

๋ชฉ๋ก ๋ณด๊ธฐ
12/22
post-thumbnail

window.scrollY

px๋กœ ์Šคํฌ๋กค ์–ผ๋งˆ๋‚˜ ํ–ˆ๋Š”์ง€ ๋‚˜ํƒ€๋ƒ„


offsetTop

์š”์†Œ์˜ Y์ถ• ์œ„์น˜ ๊ฐ’(์ฒ˜์Œ์œ„์น˜๋งŒ) ๊ฐ€์ ธ์˜ด


getBoundingClientRect() โค๏ธ

์š”์†Œ์˜ ํฌ๊ธฐ์™€ ์œ„์น˜๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฐ์ฒด

์•„์ฃผ ์œ ์šฉํ•œ ์•„์ด!!

function showValues() {
    outputElm.innerHTML = `
    ์Šคํฌ๋กค ๋œ ์–‘: <strong>${window.scrollY}</strong>
    / offsetTop: <strong>${ilbuniElm.offsetTop}</strong>
    / getBoundingClientRect(): <strong>${ilbuniElm.getBoundingClientRect().top}</strong>
`;

    let posY = ilbuniElm.getBoundingClientRect().top;
    outputElm.innerHTML = posY;

    if (posY < window.innerHeight * 0.2) {
        ilbuniElm.classList.add('zoom');
    } else {
        ilbuniElm.classList.remove('zoom');
    }
}

document.addEventListener('scroll', function () {
    showValues();
});
profile
ํ˜„์‹ค์ ์ธ ๋ชฝ์ƒ๊ฐ€

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