코드랩 레퍼런스

Parallax - Sticky Nav 본문

SAMPLE

Parallax - Sticky Nav

webstoryboy 2018. 2. 7. 16:21

See the Pen Parallax - Sticky Nav by Webstoryboy (@webstoryboy) on CodePen.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
		@import url('https://fonts.googleapis.com/css?family=Orbitron:400,500,700,900');
		/*font-family: 'Orbitron', sans-serif;*/

		/* reset */
		*{margin:0; padding:0}
		li,ul {list-style:none}
		a {text-decoration:none}
		.clearfix{*zoom:1}
		.clearfix:before,.clearfix:after{display:block; content:'';line-height:0;}
		.clearfix:after{clear:both;}

		body {font-family: 'Orbitron', sans-serif}

		/* nav */
		#nav {	
			position: absolute; left: 0; bottom: 0; 
			width: 100%; height: 56px; 
			text-align: center;
		}
		#nav.on {
			position: fixed; top:0;
		}
		#nav li {
			float: left; 
			width: 16.6666%;
		}
		#nav li a {
			display: block;
			background-color:rgba(255,255,255,0.3);
			color: #fff;
			padding:20px;
		}
		#nav li.active a {
			background-color:rgba(0,0,0,0.5)
		} 

		/* contents */
		#contents {text-align: center; font-weight: 400; }
		#contents > div {
			line-height: 100vh; 
			background-size: cover; 
			background-position: center; 
			background-attachment:fixed; 
			color: #fff; font-size: 30px;
		}

		/* section */
		#section1 {background-image:url(https://tistory4.daumcdn.net/tistory/2141493/skin/images/bg01.jpg);}
		#section2 {background-image:url(https://tistory4.daumcdn.net/tistory/2141493/skin/images/bg02.jpg);}
		#section3 {background-image:url(https://tistory4.daumcdn.net/tistory/2141493/skin/images/bg03.jpg);}
		#section4 {background-image:url(https://tistory4.daumcdn.net/tistory/2141493/skin/images/bg04.jpg);}
		#section5 {background-image:url(https://tistory4.daumcdn.net/tistory/2141493/skin/images/bg05.jpg);}
		#section6 {background-image:url(https://tistory4.daumcdn.net/tistory/2141493/skin/images/bg06.jpg);}
	</style>
</head>
<body>

	<nav id="nav">
		<ul>
			<li class="active"><a href="#section1"><span>Intro</span></a></li>
			<li><a href="#section2"><span>Skill</span></a></li>
			<li><a href="#section3"><span>Coding</span></a></li>
			<li><a href="#section4"><span>Design</span></a></li>
			<li><a href="#section5"><span>About</span></a></li>
			<li><a href="#section6"><span>Contact</span></a></li>
		</ul>
	</nav>

	<section id="contents">
		<div id="section1">
			<div class="container">
				<div class="section1">
					<h2>Who dares, Wins</h2>
				</div>
			</div>
		</div>
		<div id="section2">
			<div class="container">
				<div class="section2">
					<h2>Better late than never</h2>
				</div>
			</div>
		</div>
		<div id="section3">
			<div class="container">
				<div class="section3">
					<h2>Naver, never, give up</h2>
				</div>
			</div>
		</div>
		<div id="section4">
			<div class="container">
				<div class="section4">
					<h2>Pardon all but yourself</h2>
				</div>
			</div>
		</div>
		<div id="section5">
			<div class="container">
				<div class="section5">
					<h2>Shor out "once more"</h2>
				</div>
			</div>
		</div>
		<div id="section6">
			<div class="container">
				<div class="section6">
					<h2>Big goals gets big result</h2>
				</div>
			</div>
		</div>
	</section>


	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
	<script>
		$("#nav").each(function(){
			var header = $(this);
			var headerOffset = header.offset().top;
			
			$(window).scroll(function(){
				var wScroll = $(this).scrollTop();
				
				if(wScroll > headerOffset){
					header.addClass("on");
				} else {
					header.removeClass("on");
				}
			});
		});

		var nav = $("#nav ul li");
		var contents = $("#contents > div");

		nav.click(function(e){
			e.preventDefault();
			var target = $(this);
			var index = target.index();
			var section = contents.eq(index);
			var offset = section.offset().top;
			$("html,body").animate({scrollTop:offset},600,"easeInOutExpo");
		});


		$(window).scroll(function(){
			var wScroll = $(this).scrollTop();

			if(wScroll >= contents.eq(0).offset().top){
				nav.removeClass("active");
				nav.eq(0).addClass("active");
			}
			if(wScroll >= contents.eq(1).offset().top){
				nav.removeClass("active");
				nav.eq(1).addClass("active");
			}
			if(wScroll >= contents.eq(2).offset().top){
				nav.removeClass("active");
				nav.eq(2).addClass("active");
			}
			if(wScroll >= contents.eq(3).offset().top){
				nav.removeClass("active");
				nav.eq(3).addClass("active");
			}
			if(wScroll >= contents.eq(4).offset().top){
				nav.removeClass("active");
				nav.eq(4).addClass("active");
			}
			if(wScroll >= contents.eq(5).offset().top){
				nav.removeClass("active");
				nav.eq(5).addClass("active");
			}
		});	
	</script>
</body>
</html>

Share

Ad

Comments