728x90
반응형
패럴렉스 이펙트 - 사이드 메뉴
메뉴를 사이드로 배치하여 봅시다.
html
이전의 방식에서 parallax__nav이었던 것을 parallax__dot으로 변경해줍니다.
클릭하면 각 섹션으로 이동할 수 있도록 링크를 부여해 줍니다.
코드보기
<header id="header">
<h1>Javascript Parallax Effect02</h1>
<p>패럴렉스 이펙트 - 사이드 메뉴</p>
<ul>
<li><a href="parallaxEffect01.html">1</a></li>
<li class="active"><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 id="parallax__dot">
<ul>
<li class="active"><a href="#section1"><span>메뉴1</span></a></li>
<li><a href="#section2"><span>메뉴2</span></a></li>
<li><a href="#section3"><span>메뉴3</span></a></li>
<li><a href="#section4"><span>메뉴4</span></a></li>
<li><a href="#section5"><span>메뉴5</span></a></li>
<li><a href="#section6"><span>메뉴6</span></a></li>
<li><a href="#section7"><span>메뉴7</span></a></li>
<li><a href="#section8"><span>메뉴8</span></a></li>
<li><a href="#section9"><span>메뉴9</span></a></li>
</ul>
</nav>
<!-- //parallax__nav -->
<main id="parallax__cont">
<div class="contents">
<section id="section1" class="content__item">
<!-- //각 섹션에 content__item으로 클래스 부여 -->
<span class="content__item__num">01</span>
<h2 class="content__item__title">section1</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">얕은 사람은 행운을 믿으며, 강한 사람은 원인과 결과를 믿는다.</p>
</section>
<!-- //section1 -->
<section id="section2" class="content__item">
<span class="content__item__num">02</span>
<h2 class="content__item__title">section2</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">지난 일은 어쩔 수 없는 바 슬퍼한들 이미 엎질러진 물이다.</p>
</section>
<!-- //section2 -->
<section id="section3" class="content__item">
<span class="content__item__num">03</span>
<h2 class="content__item__title">section3</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">행동하는 사람처럼 생각하고, 생각하는 사람처럼 행동하라.</p>
</section>
<!-- //section3 -->
<section id="section4" class="content__item">
<span class="content__item__num">04</span>
<h2 class="content__item__title">section4</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">실수할 자유가 없는 자유란 가치가 없다.</p>
</section>
<!-- //section4 -->
<section id="section5" class="content__item">
<span class="content__item__num">05</span>
<h2 class="content__item__title">section5</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">끝까지 해보기 전까지는 늘 불가능해 보입니다.</p>
</section>
<!-- //section5 -->
<section id="section6" class="content__item">
<span class="content__item__num">06</span>
<h2 class="content__item__title">section6</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">고통은 잠깐이다. 포기는 영원히 남는다.</p>
</section>
<!-- //section6 -->
<section id="section7" class="content__item">
<span class="content__item__num">07</span>
<h2 class="content__item__title">section7</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">희망을 품지 않은 자는 절망도 할 수 없다.</p>
</section>
<!-- //section7 -->
<section id="section8" class="content__item">
<span class="content__item__num">08</span>
<h2 class="content__item__title">section8</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">뭔가를 위해 시도조차하려는 용기가 없다면 삶이 무슨 의미가 있을까?</p>
</section>
<!-- //section8 -->
<section id="section9" class="content__item">
<span class="content__item__num">09</span>
<h2 class="content__item__title">section9</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">꿈을 꾸세요. 그러면 그 꿈이 당신을 만들 것입니다.</p>
</section>
<!-- //section9 -->
</div>
</main>
<!-- //main -->
<aside id="parallax__info">
<div class="scroll">scrollTop : <span>0</span>px</div>
</aside>
<!-- //parallax__info -->
css
parallax__dot이 추가되었기에 그에 따른 css를 추가해줍니다. active되는 경우의 transform을 부여합니다.
코드보기
#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;
height: 30px;
padding: 5px 20px;
text-align: center;
line-height: 30px;
}
#parallax__nav li.active a {
background: #fff;
color: #000;
border-radius: 20px;
box-sizing: content-box;
}
#parallax__cont {
max-width: 1600px;
width: 98%;
margin: 0 auto;
/* background-color: rgba(255, 255, 255, 0.1); */
}
.content__item {
width: 1000px;
max-width: 70vw;
margin: 30vw auto;
/* background-color: rgba(255, 255, 255, 0.3); */
text-align: left;
margin-right: 0;
position: relative;
padding-top: 8vw;
}
.content__item:nth-child(even) {
margin-left: 0;
text-align: right;
}
.content__item__num {
font-size: 35vw;
font-weight: 100;
font-family: 'Lato';
position: absolute;
left: -5vw;
top: -16vw;
opacity: 0.07;
z-index: -2;
}
.content__item:nth-child(even) .content__item__num {
left: auto;
right: -5vw;
}
.content__item__title {
font-weight: 400;
text-transform: capitalize;
}
.content__item__imgWrap {
width: 100%;
padding-bottom: 56.25%;
background: #000;
position: relative;
overflow: hidden;
z-index: -1;
}
.content__item__img {
position: absolute;
background: url(../assets/img/effect_bg19-min.jpg);
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
width: 110%;
height: 110%;
left: -5%;
top: -5%;
filter: saturate(0%);
transition: all 1s;
}
.content__item:nth-child(1) .content__item__img {
background-image: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)), url(../assets/img/effect_bg20-min.jpg);
}
.content__item:nth-child(2) .content__item__img {
background-image: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)), url(../assets/img/effect_bg19-min.jpg);
}
.content__item:nth-child(3) .content__item__img {
background-image: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)), url(../assets/img/effect_bg18-min.jpg);
}
.content__item:nth-child(4) .content__item__img {
background-image: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)), url(../assets/img/effect_bg17-min.jpg);
}
.content__item:nth-child(5) .content__item__img {
background-image: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)), url(../assets/img/effect_bg16-min.jpg);
}
.content__item:nth-child(6) .content__item__img {
background-image: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)), url(../assets/img/effect_bg15-min.jpg);
}
.content__item:nth-child(7) .content__item__img {
background-image: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)), url(../assets/img/effect_bg14-min.jpg);
}
.content__item:nth-child(8) .content__item__img {
background-image: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)), url(../assets/img/effect_bg13-min.jpg);
}
.content__item:nth-child(9) .content__item__img {
background-image: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)), url(../assets/img/effect_bg12-min.jpg);
}
.content__item__desc {
font-size: 4vw;
line-height: 1.4;
margin-top: -5vw;
margin-left: -4vw;
word-break: keep-all;
}
.content__item:nth-child(even) .content__item__desc {
/* 2n도 가능 */
margin-left: auto;
margin-right: -4vw;
}
#parallax__info {
position: fixed;
left: 20px;
bottom: 20px;
z-index: 2000;
background: rgba(0, 0, 0, 0.6);
color: #fff;
padding: 20px;
border-radius: 10px;
}
#parallax__info li,
.scroll {
line-height: 1.4;
}
@media (max-width: 800px) {
#parallax__cont {
margin-top: 70vw;
}
#parallax__nav {
padding: 10px;
right: auto;
left: 10px;
top: 50%;
transform: translateY(-50%);
border-radius: 5px;
background-color: rgba(0, 0, 0, 0.8);
}
#parallax__nav li {
display: block;
margin: 5px;
}
#parallax__nav li a {
font-size: 14px;
padding: 5px;
border-radius: 5px;
height: auto;
line-height: 1;
}
#parallax__nav li.active a {
border-radius: 5px;
}
#parallax__info {
left: 10px;
bottom: 10px;
}
/* .content__item {
height: 100vh;
} */
}
#parallax__dot {
position: fixed;
right: 20px;
top: 50%;
transform: translateY(-50%);
z-index: 10000;
background: rgba(0, 0, 0, 0.4);
padding: 20px 10px;
border-radius: 30px;
}
#parallax__dot li {
position: relative;
width: 20px;
height: 20px;
border-radius: 50%;
margin: 12px 8px;
box-shadow: 0 0 0 2px rgba(255, 255, 255, 0);
transition: box-shadow .2s ease;
}
#parallax__dot li a {
background: #fff;
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
border-radius: 50%;
transition: transform .2s ease;
}
#parallax__dot li.active {
box-shadow: 0 0 0 2px rgba(255, 255, 255, 1);
}
#parallax__dot li.active a {
transform: scale(0.4);
}
#parallax__dot li span {
display: none;
}
js
preventDefault() : 요소를 클릭했을 때, 링크로 이동되지 않게 합니다.
=> scrollIntoView() : 특정 위치로 이동시켜주는 함수로 behavior: "smooth"을 부여하여 움직임을 부드럽게 하기위해.
getAttribute() : 특정 요소 내에서 특정 한 속성값을 가져오는 메서드
(scrollTop >= e.offsetTop - 10) = e.offsetTopdl이 scrollTop보다 수치가 커지는 오차가 발생. 10미만의 오차이지만, 10으로 부여. 탑과 section의 오차를 줄여줍니다.
document.querySelectorAll("#parallax__dot a").forEach(el => {
el.addEventListener("click", e => {
e.preventDefault(); //링크를 차단
document.querySelector(el.getAttribute("href")).scrollIntoView({ behavior: "smooth" })
});
});
window.addEventListener("scroll", () => {
let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop; //브라우저마다 지원여부가 다르기에 "||"을 적어 셋 중 한가지를 선택해 스크롤 값을 불어와서 작동하도록.
document.querySelector("#parallax__info span").innerText = Math.floor(scrollTop);
document.querySelectorAll(".content__item").forEach((e, i) => {
if (scrollTop >= e.offsetTop - 10) { //나중에 가면 e.offsetTopdl이 scrollTop보다 수치가 커지는 오차가 발생. 10미만의 오차이지만, 10으로 부여. 탑과 section의 오차를 줄이기.
document.querySelectorAll("#parallax__dot li").forEach(li => {
li.classList.remove("active");
});
document.querySelector("#parallax__dot li:nth-child(" + (i + 1) + ")").classList.add("active")
};
});
});
728x90
반응형
'Effect' 카테고리의 다른 글
mouseEffect02 (3) | 2022.09.16 |
---|---|
parallaxEffect03 (1) | 2022.09.09 |
ParallaxEffect01 (11) | 2022.09.06 |
mouseEffect01 (7) | 2022.09.05 |
sliderEffect03 (4) | 2022.09.01 |