ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

Scroll Event


๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹๋“ค์„ ํ†ตํ•ด ์Šคํฌ๋กค ์œ„์น˜์— ๋”ฐ๋ผ ๊ฐ๊ธฐ ๋‹ค๋ฅธ Header ๋””์ž์ธ์ด ๋žœ๋”๋ง๋˜๋„๋ก ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

 

scrollTop

window.addEventListener('scroll', (event) => {
    let {scrollTop} = event.target.scrollingElement; //document.documentElement.scrollTop || document.body.scrollTop
    nav.classList.add('active'); //active ํด๋ž˜์Šค ์—ฌ๋ถ€์— ๋”ฐ๋ผ ํ—ค๋” ๋””์ž์ธ์ด ์ƒ์ดํ•˜๊ฒŒ ์ž‘๋™
    if(scrollTop <= 50) nav.classList.remove('active');
})

 

scrollY / pageYoffset

window.addEventListener('scroll', function() {
    const top = window.scrollY || window.pageYOffset;
    (top >= 50 )
        ? nav.classList.add('active')
        : nav.classList.remove('active');
})
Window ์ธํ„ฐํŽ˜์ด์Šค์˜ scrollY ์ฝ๊ธฐ ์ „์šฉ ์†์„ฑ์€ ๋ฌธ์„œ๊ฐ€ ์ˆ˜์ง์œผ๋กœ ์–ผ๋งˆ๋‚˜ ์Šคํฌ๋กค๋๋Š”์ง€ ํ”ฝ์…€ ๋‹จ์œ„๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
 ์ตœ์‹  ๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” ๊ฐ’์˜ ์ •๋ฐ€๋„๊ฐ€ ํ”ฝ์…€๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ ๋ฐ˜๋“œ์‹œ ์ •์ˆซ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š”๊ฑด ์•„๋‹™๋‹ˆ๋‹ค.
Window ์ธํ„ฐํŽ˜์ด์Šค์˜ pageYOffset ์ฝ๊ธฐ ์ „์šฉ ์†์„ฑ์€ scrollY ์˜ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ, ๋ฌธ์„œ๊ฐ€ ์ˆ˜์ง์œผ๋กœ ์–ผ๋งˆ๋‚˜ ์Šคํฌ๋กค๋๋Š”์ง€
ํ”ฝ์…€ ๋‹จ์œ„๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ถ€ ์˜ค๋ž˜๋œ ๋ธŒ๋ผ์šฐ์ €๋Š” scrollY ๋Œ€์‹  pageYOffset ๋งŒ ์ง€์›ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์ง€๋งŒ, ๋…ธํ›„ ํ™˜๊ฒฝ์„ ์‹ ๊ฒฝ์“ฐ์ง€
์•Š์•„๋„ ๋œ๋‹ค๋ฉด ๋‘˜ ์ค‘ ์•„๋ฌด๊ฑฐ๋‚˜ ์‚ฌ์šฉํ•ด๋„ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค.

 

wheelDelta

const isFireFox = (navigator.userAgent.indexOf('Firefox') !== -1);
const wheelEvt = isFireFox ? 'DOMMouseScroll' : 'wheel';

window.addEventListener(wheelEvt, mouseWheelEvent);

function mouseWheelEvent(e) {
    const delta = e.wheelDelta ? e.wheelDelta : -e.detail;
    (delta < 0)
        ? nav.classList.add('active')
        : nav.classList.remove('active');
}
wheelEvent์˜ wheelDelta ์†์„ฑ ๊ฐ’์€ ํœ ์ด ์–ผ๋งˆ๋‚˜ ํšŒ์ „ํ–ˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ถ”์ƒ ๊ฐ’์ด๋‹ค. ํœ ์ด ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฉ€์–ด์ง€๋ฉด ์–‘์ˆ˜์ด๊ณ  ๊ทธ๋ ‡์ง€
์•Š์œผ๋ฉด ์Œ์ˆ˜์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋Ÿฌํ•œ ๊ฐ’์˜ ์–‘์˜ ์˜๋ฏธ๋Š” ๋ธŒ๋ผ์šฐ์ € ๊ฐ„์— ๋™์ผํ•˜์ง€ ์•Š๋‹ค.

ํ•˜์ง€๋งŒ Element: mouseWheel ๊ธฐ๋Šฅ์€ ๊ด€๋ จ ์›น ํ‘œ์ค€์—์„œ ์ด๋ฏธ ์ œ๊ฑฐ๋˜์—ˆ๊ฑฐ๋‚˜ ์‚ญ์ œ ๊ณผ์ •์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋” ์ด์ƒ ๊ถŒ์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค.

 

https://stackoverflow.com/questions/22912661/what-is-the-meaning-of-navigator-useragent-indexof-in-javascript-i-used-this-in

 

what is the meaning of navigator.userAgent.indexOf in javascript,i used this in my javascript?

I'm using this for my browser width checking.. But i dont know the exact meaning,i'm new to javascript. How can i do this ? Here is my Code : if(navigator.userAgent.indexOf("840") != -1){ /...

stackoverflow.com


์Šคํฌ๋กค ์ƒํƒœ ๊ตฌ๋ถ„ํ•˜๊ธฐ

let oldValue = 0;
window.addEventListener('scroll', function(){
    const newValue = window.scrollY || window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
    
    // ์Œ์ˆ˜ : ์Šคํฌ๋กค ๋‹ค์šด
    if(oldValue - newValue < 0) {
        nav.classList.add('active');
    }

    // ์–‘์ˆ˜ : ์Šคํฌ๋กค ์—…
    if(oldValue - newValue > 0) {
        nav.classList.remove('active');
    }
    
    oldValue = newValue;
    
    console.log(oldValue, newValue);
});

 

 

๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ