[JavaScript30] ๐ 24. Sticky Nav
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๋์ ๋ ๋์ด๋ฅผ ์กฐ์ ํด์ค๋ค.
Author And Source
์ด ๋ฌธ์ ์ ๊ดํ์ฌ([JavaScript30] ๐ 24. Sticky Nav), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค https://velog.io/@cjh951114/JavaScript30-24.-Sticky-Nav์ ์ ๊ท์: ์์์ ์ ๋ณด๊ฐ ์์์ URL์ ํฌํจ๋์ด ์์ผ๋ฉฐ ์ ์๊ถ์ ์์์ ์์ ์ ๋๋ค.
์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ ์ธ ๋ฐ๊ฒฌ์ ์ ๋ (Collection and Share based on the CC Protocol.)
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค