[JavaScript30] ๐Ÿ“‹ 24. Sticky Nav

20439 ๋‹จ์–ด JavaScriptjavascript30JavaScript

24. ๐Ÿ“‹ Sticky Nav

์Šคํฌ๋กค ๋˜๋ฉด ๊ณ ์ •๋˜๋Š” ๋ฉ”๋‰ด.

์ดˆ๊ธฐ ์ฝ”๋“œ

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sticky Nav</title>
    <link rel="stylesheet" href="style_JuneHyung.css">
</head>
<body>
    <header>
        <h1>A story about getting lost.</h1>
    </header>

    <nav id="main">
        <ul>
            <li class="logo"><a href="#">LOST</a></li>
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Images</a></li>
            <li><a href="#">Locations</a></li>
            <li><a href="#">Maps</a></li>
        </ul>
    </nav>

    <div class="site-wrap">
        
   <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore tempora rerum, est autem cupiditate~~</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore tempora rerum, est autem cupiditate~~~</p>
   <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore tempora rerum, est autem cupiditate~~~</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore tempora rerum, est autem cupiditate~~~</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore tempora rerum, est autem cupiditate~~~</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore tempora rerum, est autem cupiditate~~~</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore tempora rerum, est autem cupiditate~~~</p>
   <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore tempora rerum, est autem cupiditate~~~</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore tempora rerum, est autem cupiditate~~~</p>
   <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore tempora rerum, est autem cupiditate~~~</p>

    <img src="http://unsplash.it/400/400" class="align-left slide-in">

   <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore tempora rerum, est autem cupiditate~~~</p>

    <img src="http://unsplash.it/400/400" class="align-right slide-in">

   <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore tempora rerum, est autem cupiditate~~~</p>



   <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore tempora rerum, est autem cupiditate~~~</p>

    <img src="http://unsplash.it/400/400" class="align-right slide-in">

   <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore tempora rerum, est autem cupiditate~~~</p>
   <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore tempora rerum, est autem cupiditate~~~</p>
    </div>
    <script>
        
    </script>
</body>
</html>

์ดˆ๊ธฐ ํ™”๋ฉด

๐ŸŒ ๊ณผ์ •

๐Ÿ‘‰ 1. ์ƒ์ˆ˜, ๋ณ€์ˆ˜ ์ƒ์„ฑ

const nav = document.querySelector('#main');
const topOfNav = nav.offsetTop;

๋ฉ”๋‰ด ์˜์—ญ๊ณผ ๋ฉ”๋‰ด์˜ ์ƒ๋‹จ ์ขŒํ‘œ๊ฐ’์„ ์ €์žฅํ•ด๋‘ .

๐Ÿ‘‰ 2. ์Šคํฌ๋กค์‹œ ๋ฉ”๋‰ด๋ฅผ fixed์‹œํ‚จ๋‹ค.

function fixNav(){
    // console.log(topOfNav, window.scrollY);
    if(window.scrollY >= topOfNav){
        document.body.style.paddingTop = nav.offsetHeight + 'px';
        document.body.classList.add('fixed-nav');
    }else{
        document.body.style.paddingTop = 0;
        document.body.classList.remove('fixed-nav');
    }
}

css ์ถ”๊ฐ€๋‚ด์šฉ

fixed-nav์— ๋Œ€ํ•œ css๋ฅผ ์ถ”๊ฐ€ ์ž‘์„ฑ.

.fixed-nav nav{
    position: fixed;
    box-shadow: 0 5px rgba(0,0,0,0.1);
}

.fixed-nav li.logo{
    max-width:500px;
}

๋ฉ”๋‰ด์˜ ์ตœ์ƒ๋‹จ์ด ์Šคํฌ๋กค์˜ ์ƒ๋‹จ๊ฐ’๋ณด๋‹ค ์ž‘์„ ๋•Œ : ์ฆ‰, ์Šคํฌ๋กค์ด ๋‚ด๋ ค๊ฐ”์„ ๋•Œ

fixed-nav๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

์•„๋‹Œ๊ฒฝ์šฐ์— fixed-nav ์‚ญ์ œ

๋กœ๊ณ ์˜ ๊ฒฝ์šฐ ๊ธฐ์กด์˜ logo๋„“์ด๊ฐ€ 0์ด๋ผ fixed๋์„ ๋•Œ ๋„“์ด๋ฅผ ์กฐ์ ˆํ•ด์ค€๋‹ค.

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ