패럴렉스 이펙트 - 메뉴 효과
HTML
<!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>패럴렉스 이펙트01</title>
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/parallax.css">
</head>
<body class="img01 bg01 font01">
<header id="header">
<h1>Javascript Parallax Effect01</h1>
<p>패럴렉스 이펙트 : 메뉴 효과</p>
<ul>
<li class="active"><a href="parallaxEffect01.html">1</a></li>
<li><a href="parallaxEffect02.html">2</a></li>
<li><a href="parallaxEffect03.html">3</a></li>
<li><a href="parallaxEffect04.html">4</a></li>
<li><a href="parallaxEffect05.html">5</a></li>
<li><a href="parallaxEffect06.html">6</a></li>
<li><a href="parallaxEffect07.html">7</a></li>
</ul>
</header>
<!-- //header-->
<nav class="parallax__nav">
<ul>
<li class="active"><a href="#section1">메뉴1</a></li>
<li><a href="#section2">메뉴2</a></li>
<li><a href="#section3">메뉴3</a></li>
<li><a href="#section4">메뉴4</a></li>
<li><a href="#section5">메뉴5</a></li>
<li><a href="#section6">메뉴6</a></li>
<li><a href="#section7">메뉴7</a></li>
<li><a href="#section8">메뉴8</a></li>
<li><a href="#section9">메뉴9</a></li>
</ul>
</nav>
<!-- //parallax__nav -->
<main id="main">
<div class="parallax__wrap">
<section id="section1" class="parallax__item">
<span class="parallax__item__num">01</span>
<h2 class="parallax__item__title">Section01</h2>
<figure class="parallax__item__imgWrap">
<div class="parallax__item__img"></div>
</figure>
<p class="parallax__item__desc">늘 이걸 명심하라. 그 무엇보다 성공하겠다는 결의가 중요하다.</p>
</section>
<!-- //section1 -->
<section id="section2" class="parallax__item">
<span class="parallax__item__num">02</span>
<h2 class="parallax__item__title">Section02</h2>
<figure class="parallax__item__imgWrap">
<div class="parallax__item__img"></div>
</figure>
<p class="parallax__item__desc">모든 진보는 안전 구역 밖에서 이루어진다.</p>
</section>
<!-- //section2 -->
<section id="section3" class="parallax__item">
<span class="parallax__item__num">03</span>
<h2 class="parallax__item__title">Section03</h2>
<figure class="parallax__item__imgWrap">
<div class="parallax__item__img"></div>
</figure>
<p class="parallax__item__desc">성공이란 실패에 실패를 거듭하면서도 열의를 잃지 않는 것이다.</p>
</section>
<!-- //section3-->
<section id="section4" class="parallax__item">
<span class="parallax__item__num">04</span>
<h2 class="parallax__item__title">Section04</h2>
<figure class="parallax__item__imgWrap">
<div class="parallax__item__img"></div>
</figure>
<p class="parallax__item__desc">늘 이걸 명심하라. 그 무엇보다 성공하겠다는 결의가 중요하다.</p>
</section>
<!-- //section4-->
<section id="section5" class="parallax__item">
<span class="parallax__item__num">05</span>
<h2 class="parallax__item__title">Section05</h2>
<figure class="parallax__item__imgWrap">
<div class="parallax__item__img"></div>
</figure>
<p class="parallax__item__desc">더 열심히 일하면 할수록 운이 더 좋아진다는 것을 알게 된다.</p>
</section>
<!-- //section5-->
<section id="section6" class="parallax__item">
<span class="parallax__item__num">06</span>
<h2 class="parallax__item__title">Section06</h2>
<figure class="parallax__item__imgWrap">
<div class="parallax__item__img"></div>
</figure>
<p class="parallax__item__desc">더 나은 것을 위해 좋은 것을 포기하는 걸 두려워 하지 마라.</p>
</section>
<!-- //section6-->
<section id="section7" class="parallax__item">
<span class="parallax__item__num">07</span>
<h2 class="parallax__item__title">Section07</h2>
<figure class="parallax__item__imgWrap">
<div class="parallax__item__img"></div>
</figure>
<p class="parallax__item__desc">나의 성공은 최고의 조언을 듣고 떠나 정반대의 일을 한 덕분이다.</p>
</section>
<!-- //section7-->
<section id="section8" class="parallax__item">
<span class="parallax__item__num">08</span>
<h2 class="parallax__item__title">Section08</h2>
<figure class="parallax__item__imgWrap">
<div class="parallax__item__img"></div>
</figure>
<p class="parallax__item__desc">더 좋은 것을 쫓기 위해 좋은 것을 버리는 것을 두려워하지 마라.</p>
</section>
<!-- //section8-->
<section id="section9" class="parallax__item">
<span class="parallax__item__num">09</span>
<h2 class="parallax__item__title">Section09</h2>
<figure class="parallax__item__imgWrap">
<div class="parallax__item__img"></div>
</figure>
<p class="parallax__item__desc">독특한 사람이 되려 하지 말아라. 좋은 사람이 되도록 해라.</p>
</section>
<!-- //section9-->
</div>
</main>
<!-- //main -->
<aside class="parallax__info">
<div class="scroll">scrollTop : <span>0</span>px</div>
<div class="info">
<ul>
<li>#section1 offset() : <span class="offset1">0</span>px</li>
<li>#section2 offset() : <span class="offset2">0</span>px</li>
<li>#section3 offset() : <span class="offset3">0</span>px</li>
<li>#section4 offset() : <span class="offset4">0</span>px</li>
<li>#section5 offset() : <span class="offset5">0</span>px</li>
<li>#section6 offset() : <span class="offset6">0</span>px</li>
<li>#section7 offset() : <span class="offset7">0</span>px</li>
<li>#section8 offset() : <span class="offset8">0</span>px</li>
<li>#section9 offset() : <span class="offset9">0</span>px</li>
</ul>
</div>
</aside>
<!-- //parallax__info -->
<footer id="footer">
<a href="mailto:hyejeong3283@gmail.com">hyejeong3283@gmail.com</a>
</footer>
<!-- //footer-->
</body>
</html>
a 태그를 사용해 각 메뉴를 클릭하면 해당 section으로 이동하도록 합니다.
1~9까지의 section에는 parallax__itme 클래스명을 부여했습니다.
각 section 영역 안에는 span, h2, figure, p 태그를 사용하여 번호와 제목, 이미지, 명언 영역을 만들었습니다.
CSS
reset.css
@import url('https://webfontworld.github.io/NexonLv1Gothic/NexonLv1Gothic.css');
@import url('https://webfontworld.github.io/score/SCoreDream.css');
@import url('https://webfontworld.github.io/suncheon/Suncheon.css');
@import url('https://webfontworld.github.io/hallym/Hallym.css');
@import url('https://webfontworld.github.io/busan/Busan.css');
@import url('https://webfontworld.github.io/wando/WandoCleanSea.css');
@import url('https://webfontworld.github.io/utoBalsamtint/UtoBalsamTint.css');
@import url('https://webfontworld.github.io/eulyoo/Eulyoo1945.css');
@import url('https://webfontworld.github.io/mapo/MapoDPP.css');
@import url('https://webfontworld.github.io/woori/Wooridaum.css');
@import url('https://fonts.googleapis.com/css2?family=Lato:wght@100&display=swap');
/*reset*/
* {
margin: 0;
padding: 0;
color: #fff;
}
*, *::before, *::after {
box-sizing: border-box;
}
a {
text-decoration: none;
color: #222;
}
h1, h2, h3, h4, h5, h6 {
font-weight: normal;
}
li, ul, ol {
list-style: none;
}
img {
vertical-align: top;
width: 100%;
}
em {
font-style: normal;
}
.font01 {
font-family: 'NexonLv1Gothic';
font-weight: 300;
}
.font02 {
font-family: 'SCoreDream';
font-weight: 300;
}
.font03 {
font-family: 'Suncheon';
font-weight: 300;
}
.font04 {
font-family: 'Hallym';
font-weight: 300;
}
.font05 {
font-family: 'Busan';
font-weight: 300;
}
.font06 {
font-family: 'WandoCleanSea';
font-weight: 300;
}
.font07 {
font-family: 'UtoBalsamTint';
font-weight: 300;
}
.font08 {
font-family: 'Eulyoo1945';
font-weight: 300;
}
.font09 {
font-family: 'MapoDPP';
font-weight: 300;
}
.font10 {
font-family: 'Wooridaum';
font-weight: 300;
}
body {
background-color: #222;
}
parallax.css
/* header */
#header {
position: absolute;
left: 20px;
top: 20px;
}
#header h1 {
margin-bottom: 0.3em;
}
#header ul {
margin-top: 0.6em;
}
#header li {
display: inline-block;
}
#header li a {
color: #fff;
border: 1px solid #fff;
width: 30px;
height: 30px;
line-height: 30px;
display: inline-block;
border-radius: 40%;
text-align: center;
}
#header li.active a {
background: #fff;
color: #000;
}
/* footer */
#footer {
text-align: center;
padding: 100px 0;
}
#footer a {
color: #fff;
font-size: 14px;
}
#footer a:hover {
text-decoration: underline;
}
/* parallax__nav */
.parallax__nav {
position: fixed;
right: 20px;
top: 20px;
z-index: 2000;
background-color: rgba(0,0,0,0.4);
padding: 20px 30px;
border-radius: 50px;
}
.parallax__nav li {
display: inline;
margin: 0 5px;
}
.parallax__nav li a {
display: inline-block;
padding: 5px 20px;
text-align: center;
line-height: 30px;
color: #fff;
}
.parallax__nav li.active a {
background-color: #fff;
color: #000;
border-radius: 20px;
box-sizing: content-box;
}
/* parallax__wrap */
.parallax__wrap {
max-width: 1600px;
width: 98%;
margin: 0 auto;
/* background-color: rgba(255,255,255,0.1); */
}
.parallax__item {
width: 1000px;
max-width: 70vw;
margin: 30vw auto;
/* background-color: rgba(255,255,255,0.3); */
margin-right: 0;
position: relative;
padding-top: 8vw;
}
.parallax__item:nth-child(even) {
margin-left: 0;
text-align: right;
}
.parallax__item__num {
font-size: 35vw;
font-weight: 100;
font-family: Lato;
position: absolute;
left: -5vw;
top: -16vw;
opacity: 0.07;
z-index: -2;
}
.parallax__item:nth-child(even) .parallax__item__num {
left: auto;
right: -5vw;
}
.parallax__item__title {
font-weight: bold;
}
.parallax__item__imgWrap {
width: 100%;
padding-bottom: 56.25%;
background-color: #000;
position: relative;
z-index: -1;
}
.parallax__item__img {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-image: url(../img/parallaxEffect01-min.jpg);
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
filter: saturate(0%);
transition: all 1s;
}
.parallax__item:nth-child(1) .parallax__item__img {
background-image: url(../img/parallaxEffect01-min.jpg);
}
.parallax__item:nth-child(2) .parallax__item__img {
background-image: url(../img/parallaxEffect02-min.jpg);
}
.parallax__item:nth-child(3) .parallax__item__img {
background-image: url(../img/parallaxEffect03-min.jpg);
}
.parallax__item:nth-child(4) .parallax__item__img {
background-image: url(../img/parallaxEffect04-min.jpg);
}
.parallax__item:nth-child(5) .parallax__item__img {
background-image: url(../img/parallaxEffect05-min.jpg);
}
.parallax__item:nth-child(6) .parallax__item__img {
background-image: url(../img/parallaxEffect06-min.jpg);
}
.parallax__item:nth-child(7) .parallax__item__img {
background-image: url(../img/parallaxEffect07-min.jpg);
}
.parallax__item:nth-child(8) .parallax__item__img {
background-image: url(../img/parallaxEffect08-min.jpg);
}
.parallax__item:nth-child(9) .parallax__item__img {
background-image: url(../img/parallaxEffect09-min.jpg);
}
.parallax__item__desc {
font-size: 3vw;
line-height: 1.4;
margin-top: -5vw;
margin-left: -4vw;
word-break: keep-all;
}
.parallax__item:nth-child(even) .parallax__item__desc {
margin-left: auto;
margin-right: -4vw;
}
.parallax__info {
position: fixed;
left: 20px;
bottom: 20px;
background-color: rgba(0, 0, 0 , 0.6);
color: #fff;
padding: 20px;
border-radius: 10px;
font-size: 14px;
line-height: 1.4;
}
@media (max-width: 1200px){
.parallax__nav {
padding: 10px;
background-color: rgba(0, 0, 0, 0.9);
right: 10px;
left: 10px;
top: 10px;
border-radius: 5px;
text-align: center;
}
.parallax__nav li {
margin: 2px;
}
.parallax__nav li a {
font-size: 12px;
padding: 0 14px;
}
}
SCRIPT
<script>
window.addEventListener("scroll", () => {
let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;
document.querySelectorAll(".parallax__item").forEach((item, index) => {
if(scrollTop >= item.offsetTop){
document.querySelectorAll(".parallax__nav li").forEach((li) => {
li.classList.remove("active");
});
document.querySelector(".parallax__nav li:nth-child("+(index+1)+")").classList.add("active");
}
});
document.querySelectorAll(".parallax__nav li a").forEach(li => {
li.addEventListener("click", (e) => {
e.preventDefault();
document.querySelector(li.getAttribute("href")).scrollIntoView({
behavior: "smooth"
});
});
});
// 선택자
let info = document.querySelectorAll(".info ul li span");
// info
document.querySelector(".scroll span").innerText = parseInt(scrollTop);
document.querySelector(".info .offset1").innerText = document.getElementById("section1").offsetTop;
document.querySelector(".info .offset2").innerText = document.getElementById("section2").offsetTop;
document.querySelector(".info .offset3").innerText = document.getElementById("section3").offsetTop;
document.querySelector(".info .offset4").innerText = document.getElementById("section4").offsetTop;
document.querySelector(".info .offset5").innerText = document.getElementById("section5").offsetTop;
document.querySelector(".info .offset6").innerText = document.getElementById("section6").offsetTop;
document.querySelector(".info .offset7").innerText = document.getElementById("section7").offsetTop;
document.querySelector(".info .offset8").innerText = document.getElementById("section8").offsetTop;
document.querySelector(".info .offset9").innerText = document.getElementById("section9").offsetTop;
// for문
// for(i=1; i<=9; i++){
// document.querySelector(".info .offset"+i).innerText = document.getElementById("section"+i).offsetTop;
// }
// forEach문
// info.forEach((el, i) => {
// el.innerText = document.getElementById("section"+(i+1)).offsetTop;
// });
// for of문
// for(const [i,el] of info.entries()){
// el.innerText = document.getElementById(`section${i+1}`).offsetTop;
// }
// for in문
// for(const i in info){
// info[i].innerText = document.getElementById(`section${parseInt(i)+1}`).offsetTop;
// }
});
</script>
스크롤 했을 때 이벤트를 주는 함수를 등록합니다.
let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;
현재 스크롤의 위치를 scrollTop라는 변수명에 저장합니다.
3가지의 방법으로 스크롤의 위치값을 구할 수 있습니다.
구하는 방법은 다를 수 있지만, 3가지 방법 모두 사용 가능한 값을 scrollTop에 할당합니다.
document.querySelectorAll(".parallax__item").forEach((item, index) => {
if(scrollTop >= item.offsetTop){
document.querySelectorAll(".parallax__nav li").forEach((li) => {
li.classList.remove("active");
});
document.querySelector(".parallax__nav li:nth-child("+(index+1)+")").classList.add("active");
}
});
forEach문을 사용해 parallax__item 클래스명을 가진 요소들을 반복실행합니다.
item.offsetTop 값이 현재 스크롤 위치값 보다 작거나 같을 경우, 이전 요소에 active값을 제거하고 현재 요소에 active값을 주어 활성화해줍니다.
document.querySelectorAll(".parallax__nav li a").forEach(li => {
li.addEventListener("click", (e) => {
e.preventDefault();
document.querySelector(li.getAttribute("href")).scrollIntoView({
behavior: "smooth"
});
});
});
forEach문을 사용하여 해당 요소에 반복문을 실행합니다.
해당 클래스명을 가진 요소에 클릭 이벤트를 등록합니다.
클릭시 href 값이 있는 해당 요소의 위치로 behavior: "smooth" 속성값을 주어 부드럽게 스크롤하여 이동시킵니다.
document.querySelector(".scroll span").innerText = parseInt(scrollTop);
document.querySelector(".info .offset1").innerText = document.getElementById("section1").offsetTop;
document.querySelector(".info .offset2").innerText = document.getElementById("section2").offsetTop;
document.querySelector(".info .offset3").innerText = document.getElementById("section3").offsetTop;
document.querySelector(".info .offset4").innerText = document.getElementById("section4").offsetTop;
document.querySelector(".info .offset5").innerText = document.getElementById("section5").offsetTop;
document.querySelector(".info .offset6").innerText = document.getElementById("section6").offsetTop;
document.querySelector(".info .offset7").innerText = document.getElementById("section7").offsetTop;
document.querySelector(".info .offset8").innerText = document.getElementById("section8").offsetTop;
document.querySelector(".info .offset9").innerText = document.getElementById("section9").offsetTop;
info 클래스명을 가진 각 요소(offset1~9)에 스크롤 위치값(offseetTop)을 부여합니다.
아래의 스크립트는 위의 스크립트를 for, forEach, for of, for in문으로 작성했습니다.
// for문
// for(i=1; i<=9; i++){
// document.querySelector(".info .offset"+i).innerText = document.getElementById("section"+i).offsetTop;
// }
// forEach문
// info.forEach((el, i) => {
// el.innerText = document.getElementById("section"+(i+1)).offsetTop;
// });
// for of문
// for(const [i,el] of info.entries()){
// el.innerText = document.getElementById(`section${i+1}`).offsetTop;
// }
// for in문
// for(const i in info){
// info[i].innerText = document.getElementById(`section${parseInt(i)+1}`).offsetTop;
// }