ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JavaScript/VanillaJS] ๋งˆ์šฐ์Šค ์˜ค๋ฒ„ ๋ฐ ํœ  ์ด๋ฒคํŠธ (onmouseover, onmousewheel)
    javascript 2019. 4. 10. 14:29

    ์ €๋ฒˆ ํฌ์ŠคํŒ…(https://bejerry.tistory.com/8)์— ์ด์–ด์„œ ๊ณ„์† Sellery.me ํด๋ก ์„ ํ•ด๋ณด๋ ค๊ณ  ๋…ธ๋ ฅ์ค‘์ž…๋‹ˆ๋‹ค.

     

    (ํ•˜๋ฃจ๊ฐ€ ์ง€๋‚˜๊ณ  ์ดํ‹€์ด ์ง€๋‚ฌ๋Š”๋ฐ๋„ ์„ฑ๊ณผ๋ฌผ์ด ์—†๋‹ค.

    ์—ฌ์ „ํžˆ ํœ  ์ด๋ฒคํŠธ๋กœ div์ด๋™์„ ๋ชปํ•˜๊ณ  ์žˆ๋‹คใ… ใ… )

     

    ๊ทธ๋ž˜๋„ ๋‚˜๋ฆ„ ์ง„์ „์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.


    HTML

    index.html๊ณผ test.html์€ ๊ฐ™๋‹ค.

     


    JS

    scroll.js์™€ test.js๋Š” ๊ฐ™๋‹ค.

     

    ์ผ๋‹จ ๋‚ด ๋งˆ์šฐ์Šค์˜ ์œ„์น˜๊ฐ€ ์–ด๋–ค div์— ์žˆ๋Š”์ง€๋ฅผ ์•Œ์•„๋ƒˆ์Šต๋‹ˆ๋‹ค.

    ํ˜„์žฌ ๋งˆ์šฐ์Šค๊ฐ€ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” div๋Š” ๊ฐ€์žฅ ํ•˜์œ„์˜ div(class="section-content")์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ถ€๋ชจ๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋‚˜๋ˆ ์•ผํ•˜๊ณ ,

    classList.js์—์„  ๊ฐ div์—๊ฒŒ ๋‹ค๋ฅธ class name์„ ์ง€์ •ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— div๋ฅผ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    ์†Œ์Šค๋ฅผ ์ปดํŒŒ์ผํ•˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ๊ฐ€๋ณผ๊นŒ์š”?

     

    1. html์„ ์ „๋ถ€ ์ฝ์€ ํ›„ body๊ฐ€ ๋๋‚˜๊ธฐ ์ „, test.js(scroll.js)๋ฅผ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.

     

    2. init() <-- init์œผ๋กœ ์ฒ˜์Œ ์ ‘๊ทผํ•˜๊ณ  init ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

     

    3. getCurrentDivClassName() <-- init ํ•จ์ˆ˜์—์„œ๋Š” getCurrentDivClassName() ์  ์žฅ ๊ธธ๊ฒŒ๋„ ์ง€์—ˆ๋„ค, ๋ฌดํŠผ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

     

    4. onMouseOver()์—์„œ ์ด๋ฒคํŠธ ๊ฐ์ฒด์˜ target ์†์„ฑ์„ ํ†ตํ•ด ํ˜„์žฌ ์œ„์น˜๋ฅผ ์•Œ๊ณ  ๊ทธ ๋ถ€๋ชจ Element๋ฅผ(?) ๋ณ€์ˆ˜์— ๋‹ด๋Š”๋‹ค.

     

    5. ๋‹ด์€ ๋ถ€๋ชจ์˜ class name ์ค‘์— ์ง€์ •ํ•œ ๋ฐฐ์—ด์˜ ๊ฐ’์ด ํฌํ•จ๋œ class name์ด ์žˆ์œผ๋ฉด onMouseWheel ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

     

    6. onMouseWheel ํ•จ์ˆ˜์—์„œ ์ด์ „ div์™€ ๋‹ค์Œ div์˜ ๊ฐ’์„ ๊ฐ๊ฐ ๋ณ€์ˆ˜์— ๋‹ด๊ณ , event ๊ฐ์ฒด์˜ wheelDelta ๊ฐ’์œผ๋กœ ์œ„๋กœ ์Šคํฌ๋กค์ธ์ง€, ์•„๋ž˜๋กœ ์Šคํฌ๋กค์ธ์ง€ ๊ตฌ๋ถ„ ์ง€์„ ์ˆ˜ ์žˆ๋‹ค.

     


    ๊ฒฐ๊ณผ๋ฌผ

    ์Šคํฌ๋กค์„ ์œ„๋กœ ํ•˜๋ฉด ์ด์ „ div๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— undefined๋ฅผ ๋‚ด๋ฑ‰๊ณ , "wheel up" string์ด ์ถœ๋ ฅ๋˜๊ณ ,

    ์Šคํฌ๋กค์„ ์•„๋ž˜๋กœ ํ•˜๋ฉด ๋‹ค์Œ div์ธ sec1 DOM(?)์ด ์ถœ๋ ฅ๋˜๋ฉด์„œ ๋™์‹œ์— "wheel down"์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

     

     


     

    ์•„์ง ํ•ด๊ฒฐ์ด ์•ˆ๋œ ๋ถ€๋ถ„์€,

     

    JSํŒŒ์ผ์„ ๋ณด๋ฉด offset์ด ์•ˆ๋จน๋Š”๋‹ค? ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค? ๋‚ด๊ฐ€ ๋ญ˜ ์ž˜๋ชปํ–ˆ๋‹ค?

    section๋ณด๋‹ค ๋” ์ƒ์œ„์˜ body๋‚˜ html์—์„œ animate ํ•ด์•ผํ•˜๋‚˜?

     

    jQuery์—์„  ์ด๋ ‡๊ฒŒ ๋Œ€๋ถ€๋ถ„ ์“ฐ๋˜๋ฐ, Vanilla๋กœ๋งŒ ํ•˜๋ ค๋‹ˆ ์ž๋ฃŒ ์ฐพ๊ธฐ๊ฐ€ ํž˜๋“œ๋„ค์š”ใ… ใ…  (ํ•‘๊ณ„ ์˜ค์ง€๊ณ ์š”-)

    
    <script>
        function fnMove(seq){
            var offset = $("#div" + seq).offset();
            $('html, body').animate({scrollTop : offset.top}, 400);
        }
    </script>
    ์ถœ์ฒ˜: https://cofs.tistory.com/191 [CofS] 

    ํ˜น์‹œ ์ด ๊ธ€์„ ๋ณด์‹œ๊ณ  solution์ด ์žˆ์œผ์‹œ๋ฉด ๋Œ“๊ธ€ ์ข€ ๋ถ€ํƒ๋“œ๋ฆด๊ฒŒ์š”....ใ…œใ…œ

    ๋„์™€์ฃผ์„ธ์š”;;;;ใ…œใ…œใ… ใ…ํ“จใ… ใ… 

     

    ๋Œ“๊ธ€

Posted by Bejerry.