ํจ๋ด๋ญ์ค ์ดํํธ 03
์ด๋ฒ ์๊ฐ์๋ ๊ทธ ๋์ ๋ฐฐ์ ๋ JavaScript๋ฅผ ์ด์ฉํ์ฌ ํจ๋ด๋ญ์ค(parallax, ํจ๋ด๋์ค) ์ดํํธ๋ฅผ ๋ง๋ค์ด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
๋จผ์ parallax๋, '์์ฐจ(่ฆๅทฎ)'๋ผ๋ ์๋ฏธ๋ฅผ ๊ฐ์ง ์ฒ๋ฌธํ ์ฉ์ด์ด๋ฉฐ, ์ด๋ฒ ํฌ์คํ
์์ ๋ด๋น๊ฒ์ด์
๋ฐ์์ ํน์ ์น์
์ผ๋ก ๊ฐ๋ ๋ฒํผ์
๋๋ฅด๋ฉด ํด๋น ์น์
์ผ๋ก ์์ฐจ๋ฅผ ๋๊ณ ์ค๋ฌด์คํ๊ฒ ์ด๋์์ผ ์ฃผ๋ ํจ๊ณผ๋ฅผ ๋ง๋ค์ด๋ณด๊ณ ์ ํฉ๋๋ค.
์ง๋ ํจ๋ด๋ญ์ค 01 ํฌ์คํ
์์ ๋ง๋ ํ์ ๊ทธ๋๋ก ์ ์งํ ์ฑ ๋ฉ๋ด ๋ฐ๋ฅผ ์จ๊ฒผ๋ค ๋ํ๋๊ฒ ํ๋ ๊ธฐ๋ฅ์ ์ถ๊ฐํ๊ฒ ์ต๋๋ค.
๋ํ ํ์ด์ง ์ตํ๋จ์ผ๋ก ๋ด๋ ค๊ฐ๋ฉด TOP ๋ฒํผ์ด ๋ํ๋๊ณ , TOP ๋ฒํผ์ ๋๋ฅด๋ฉด ํ์ด์ง ์ต์๋จ์ผ๋ก ์ฌ๋ผ๊ฐ๊ฒ ํ๋ ์ดํํธ๋ ์ถ๊ฐํด๋ณด๋๋ก ํฉ์๋ค.
HTML ์์ค
header, parallax__dot(dot ๋ด๋น๊ฒ์ด์ ), main(parallax__cont(๋ช ์ธ ๋ชจ์)), aside(parallax__info(์คํฌ๋กคํ๊ฐ)), footer(๋ชจ๋ฌ ๋ฐ์ค)๋ก ๊ตฌ์ฑ๋๋ค.
<body class="img10">
<header id="header">
<h1>JAVASCRIPT PARALLAX EFFECT 03</h1>
<p>ํจ๋ด๋ญ์ค ์ดํํธ</p>
<ul>
<li><a href="parallaxEffect01.html">01</a></li>
<li><a href="parallaxEffect02.html">02</a></li>
<li class="active"><a href="parallaxEffect03.html">03</a></li>
<li><a href="parallaxEffect04.html">04</a></li>
<li><a href="parallaxEffect05.html">05</a></li>
<li><a href="parallaxEffect06.html">06</a></li>
<li><a href="parallaxEffect07.html">07</a></li>
</ul>
</header>
<!-- //header -->
<nav id="parallax__nav">
<ul>
<li class="active"><a href="#section1">๋ฉ๋ด1</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 -->
<div id="parallax__top">
Top
</div>
<!-- //parallax__top -->
<main id="parallax__cont">
<div id="contents">
<section id="section1" class="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 -->
<footer id="footer">
<div class="modal__wrap">
</div>
</footer>
<!-- //footer -->
</body>
JS ์์ค
๋ถ๋๋ฌ์ด ์ ๋๋ฉ์ด์
์ ์ฃผ๊ธฐ ์ํด e.preventDefault()๋ก ๋ด๋น๊ฒ์ด์
๋ฐ ์ด๋ ๊ธฐ๋ฅ์ ์ ์ ๋นํ์ฑํ ์ํจ ๋ค์
behavior: "smooth"๋ก ์ค๋ฌด์คํ ์ ๋๋ฉ์ด์
ํจ๊ณผ๋ฅผ ์ฃผ๋ ๋์์ ์ด๋ ๊ธฐ๋ฅ์ ์ฌํ์ฑํ์ํจ๋ค.
๊ทธ ๋ค์ ์คํฌ๋กคํ๊ฐ์ let ๋ณ์์ ์ ์ฅํ๊ณ ๊ทธ ๊ฐ์ ๋ฐ์ฌ๋ฆผํ์ฌ ํ์๋๋๋ก ์ค์ ํ๊ณ , ์คํฌ๋กค์ด ํด๋น ์น์
์ ์ง๋๊ฐ๋๊ฐ
์ง๋์น์ง ์์๋๊ฐ ์ฌ๋ถ๋ฅผ if๋ฌธ์ผ๋ก ๊ฐ๋ ค .active ํจ๊ณผ๋ฅผ ์ถ๊ฐํ ์ง ์ ๊ฑฐํ ์ง๋ฅผ ์ค์ ํ๋ค.
window.addEventListener("scroll", () => {
let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop; // ํ์ฌ ๋ธ๋ผ์ฐ์ ํ๋ฉด ์ค ์ต์๋จ ์์น ๊ฐ
// ๋ฌธ์์ ์ฒด๋์ด - ๋ธ๋ผ์ฐ์ ๋์ด(์ฆ, ๋ง์ง๋ง ์น์
์ ๋๋ฌํ์ ๋) ==> TOP ๋ฒํผ ๋ํ๋๊ฒ ํจ
if(scrollTop + window.innerHeight >= document.body.scrollHeight){
document.querySelector("#parallax__top").classList.add("show");
} else {
document.querySelector("#parallax__top").classList.remove("show");
}
// top ๋ฒํผ : ๋๋ ์ ๋ ํ์ด์ง ์ต์๋จ์ผ๋ก ์ค๋ฌด์คํ๊ฒ ์ด๋์์ผ์ค
document.querySelector("#parallax__top").addEventListener("click", () => {
window.scrollTo({left:0, top:0, behavior:"smooth"});
});
document.querySelector("#parallax__info span").innerText = Math.ceil(scrollTop);
});
// ์คํฌ๋กค ์์ง์ผ ๋
// ํธ๋ฆฌ๊ฑฐ ๋ณ์ ์ด์ฉ
let nowScroll = true; // ํธ๋ฃจ์ผ๋ ์คํ
let lastScroll = 0;
function scrollProgress() {
nowScroll = true;
setInterval(() => {
if(nowScroll) {
nowScroll = false;
hasScroll();
}
}, 300) // 0.3์ด๋ง๋ค
}
function hasScroll() {
let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;
// scrollTop๊ฐ์ด lastScroll๋ณด๋ค ์ ์ผ๋ฉด
if(scrollTop < lastScroll) {
document.querySelector("#parallax__nav").classList.add("show"); //๋ด๋น๊ฒ์ด์
๋ฐ ๋ณด์ฌ์ฃผ๊ธฐ
} else {
document.querySelector("#parallax__nav").classList.remove("show"); // (์๋ ๊ฒฝ์ฐ) ์จ๊ธฐ๊ธฐ
}
lastScroll = scrollTop; // scrollTop๊ฐ์ lastScroll์ ์ ์ฅํจ.
}
window.addEventListener("scroll", scrollProgress);
CSS ์์ค
ํจ๋ด๋ญ์ค ํจ๊ณผ์๋ง ์ ์ฉ๋๋ CSS ์์ค์ด๋ค. ๊ณตํต/์ด๊ธฐํ ๋ถ๋ถ CSS๋ ํ๋จ์ CSS ์์ค ๋ณด๊ธฐ ๋ฒํผ์ ๋๋ฌ ํ์ธํ ๊ฒ.
<style>
/* parallax__nav */
#parallax__nav {
position: fixed;
right: 20px;
top: -200px;
z-index: 2000;
background-color: rgba(0,0,0,0.4);
padding: 20px 30px;
border-radius: 50px;
transition: top .4s ease;
}
#parallax__nav.show {
top: 20px;
}
#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.1); */
text-align: left;
margin-right: 0;
position: relative;
padding-top: 7vw;
}
.content__item:nth-child(even) { /* even == ์ง์๋ง ์ ํ */
margin-left: 0;
text-align: right;
}
.content__item__num {
font-size: 35vw;
font-family: 'Lato';
font-weight: 100;
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-image: url(../assets/img/effect_bg10.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: url(../assets/img/effect_bg10.jpg);
}
.content__item:nth-child(2) .content__item__img {
background-image: url(../assets/img/effect_bg09.jpg);
}
.content__item:nth-child(3) .content__item__img {
background-image: url(../assets/img/effect_bg08.jpg);
}
.content__item:nth-child(4) .content__item__img {
background-image: url(../assets/img/effect_bg07.jpg);
}
.content__item:nth-child(5) .content__item__img {
background-image: url(../assets/img/effect_bg06.jpg);
}
.content__item:nth-child(6) .content__item__img {
background-image: url(../assets/img/effect_bg05.jpg);
}
.content__item:nth-child(7) .content__item__img {
background-image: url(../assets/img/effect_bg04.jpg);
}
.content__item:nth-child(8) .content__item__img {
background-image: url(../assets/img/effect_bg03.jpg);
}
.content__item:nth-child(9) .content__item__img {
background-image: url(../assets/img/effect_bg02.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 {
margin-left: auto;
margin-right: -4vw;
}
#parallax__info {
position: fixed;
left: 20px;
bottom: 20px;
z-index: 2000;
background: rgba(0,0,0,0.4);
color: #fff;
padding: 20px 30px;
border-radius: 10px;
}
#parallax__info li, .scrollTop {
line-height: 1.4;
}
#parallax__top {
position: fixed;
left: 50%;
bottom: -220px;
z-index: 10000;
transform: translateX(-50%);
width: 40px;
height: 40px;
background: rgba(0,0,0,0.4);
text-align: center;
line-height: 40px;
color: #fff;
border-radius: 50%;
padding: 10px;
cursor: pointer;
transition: all 0.3s;
}
#parallax__top.show {
bottom: 20px;
}
#parallax__top:hover {
background: #fff;
color: #000;
}
/* ๋ฐ์ํ */
@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;
}
}
</style>
์ด๋ฏธ์ง ์ค๋ช ์ผ๋ก ํ ๋์ ๋ณด๊ธฐ
๊ฒฐ๊ณผ
'Effect > Parallax Effect' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Parallax Effect] ํจ๋ด๋ญ์ค ์ดํํธ 06 (2) | 2022.09.29 |
---|---|
[Parallax Effect] ํจ๋ด๋ญ์ค ์ดํํธ 05 - ์ด์ง๊ฐ ํจ๊ณผ (10) | 2022.09.20 |
[Parallax Effect] ํจ๋ด๋ญ์ค ์ดํํธ 04 - ๋ถ๋๋ฝ๊ฒ ๋ํ๋ด๊ธฐ (5) | 2022.09.18 |
[Parallax Effect] ํจ๋ด๋ญ์ค ์ดํํธ 02 - ์ฌ์ด๋๋ฐ (6) | 2022.09.08 |
[Parallax Effect] ํจ๋ด๋ญ์ค ์ดํํธ 01 (6) | 2022.09.06 |
๋๊ธ