728x90
๋ฐ์ํ
Javascript parallax Effect
parallax ์ดํํธ ์ค ์จ๊น ๋ฉ๋ด/ํ๋ฒํผ ์ ํ์ ๋๋ค.
HTML
html ์์ค์ ๋๋ค.
<main id="parallax__cont">
<div id="contents">
<section id="section01" 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>
<!-- section01 -->
<section id="section02" class="content__item">
<span class="content__item__num">02</span>
<h2 class="content__item__title">section02</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">
๋น๋ก ๋ด์ผ ์ง๊ตฌ์ ์ข
๋ง์ด ์จ๋ค ํ๋๋ผ๋ ๋๋ ์ค๋ ํ ๊ทธ๋ฃจ์ ์ฌ๊ณผ๋๋ฌด๋ฅผ ์ฌ๊ฒ ๋ค.
</p>
</section>
<!-- section02 -->
<section id="section03" class="content__item">
<span class="content__item__num">03</span>
<h2 class="content__item__title">section03</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">๊ทธ๋ค์ด ์ฃฝ์์ ๋๋ ค์ํ๋ ๊ฒ์ด ์ด์ํฉ๋๋ค. ์ถ์ด ์ฃฝ์๋ณด๋ค ํจ์ฌ ๋ ์ํ๋ค.</p>
</section>
<!-- section03 -->
<section id="section04" class="content__item">
<span class="content__item__num">04</span>
<h2 class="content__item__title">section04</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">
๋ชจ๋ ๊ฒ์ด ๋ณํ๋ค๋ ๊ฒ์ ๊นจ๋ฌ์ผ๋ฉด ๋ถ์ก์ผ๋ ค ํ ๊ฒ์ด ์๋ค.
</p>
</section>
<!-- section04 -->
<section id="section05" class="content__item">
<span class="content__item__num">05</span>
<h2 class="content__item__title">section05</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">
์ด๋ค ์๊ฐ์ ๋ฐ์๋ค์ด์ง ์๊ณ ์ฆ๊ฒ๊ฒ ํ ์ ์๋ ๊ฒ์ ๊ต์ ์๋ ์ ์ ์ ํ์ง์ด๋ค.
</p>
</section>
<!-- section05 -->
<section id="section06" class="content__item">
<span class="content__item__num">06</span>
<h2 class="content__item__title">section06</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">
์ฒ ํ์ ์ธ์ ๋ ์ฐ๋ฆฌ ๋ ์์ ๋์ฌ ์๋ ๊ทธ ์๋ํ ์ฑ
์ ๊ธฐ๋ก๋์ด ์์ต๋๋ค. ์ฆ, ์ฐ์ฃผ๋ฅผ ์๋ฏธํฉ๋๋ค.
</p>
</section>
<!-- section06 -->
<section id="section07" class="content__item">
<span class="content__item__num">07</span>
<h2 class="content__item__title">section07</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">
ํ๋ณต์ ํ์ด ์ฆ๊ฐํ๊ณ ์ ํญ์ด ๊ทน๋ณต๋๊ณ ์๋ค๋ ๋๋์
๋๋ค.
</p>
</section>
<!-- section07 -->
<section id="section08" class="content__item">
<span class="content__item__num">08</span>
<h2 class="content__item__title">section08</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">์ฌ๋์ ๊ด๊ธฐ๋ ํ๋์ ์ถ๋ณต ์ค ๊ฐ์ฅ ํฐ ๊ฒ์
๋๋ค.</p>
</section>
<!-- section08 -->
<section id="section09" class="content__item">
<span class="content__item__num">09</span>
<h2 class="content__item__title">section09</h2>
<figure class="content__item__imgWrap">
<div class="content__item__img"></div>
</figure>
<p class="content__item__desc">๊ฐํ ์๊ณ ! ๋น์ ์ ์ง๋ฅ์ ์ฌ์ฉํ ์ฉ๊ธฐ๋ฅผ ๊ฐ์ง์ญ์์ค.</p>
</section>
<!-- section09 -->
</div>
</main>
CSS
css ์์ค์ ๋๋ค.
/* 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;
/* width: 30px; */
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;
/* background-color: rgba(255,255,255,0.1); */
width: 98%;
margin: 0 auto;
}
.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: 10vw;
}
.content__item:nth-child(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 {
right: -5vw;
left: auto;
}
.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;
width: 110%;
height: 110%;
left: -5px;
top: -5px;
background-image: url(img/effect_bg02.jpg);
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
filter: saturate(0%);
transition: all 1s;
}
.content__item:nth-child(2) .content__item__img {
background-image: url(img/effect_bg09.jpg);
}
.content__item:nth-child(3) .content__item__img {
background-image: url(img/effect_bg09.jpg);
}
.content__item:nth-child(4) .content__item__img {
background-image: url(img/effect_bg09.jpg);
}
.content__item:nth-child(5) .content__item__img {
background-image: url(img/effect_bg09.jpg);
}
.content__item:nth-child(6) .content__item__img {
background-image: url(img/effect_bg09.jpg);
}
.content__item:nth-child(7) .content__item__img {
background-image: url(img/effect_bg09.jpg);
}
.content__item:nth-child(8) .content__item__img {
background-image: url(img/effect_bg09.jpg);
}
.content__item:nth-child(9) .content__item__img {
background-image: url(img/effect_bg09.jpg);
}
.content__item__desc {
font-size: 3vw;
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;
}
#parllax__nav li.active a {
border-radius: 5px;
}
#parallax__info {
position: fixed;
left: 20px;
bottom: 20px;
z-index: 2000;
background-color: rgba(0, 0, 0, 0.6);
color: #fff;
padding: 20px;
border-radius: 10px;
}
#parallax__info li,
.scrollTop {
line-height: 1.4;
}
#parallax__info li.active a {
border-radius: 5px;
}
@media (max-width: 800px) {
#parallax__cont {
margin-top: 70vw;
}
#parllax__nav {
padding: 10px;
right: auto;
left: 10px;
top: 10px;
border-radius: 5px;
background: rgba(0, 0, 0, 0.8);
transform: translateY(-50%);
}
#parllax__nav li {
display: block;
margin: 5px;
}
#parllax__nav li a {
font-size: 14px;
padding: 5px;
border-radius: 5px;
height: auto;
line-height: 1;
}
#parallax__info {
left: 10;
bottom: 10px;
}
}
#parallax__top {
font-size: 14px;
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%;
cursor: pointer;
transition: all 0.3s;
}
#parallax__top.show {
bottom: 20px;
}
#parallax__top:hover {
background: #fff;
color: #000;
}
Javascript
Javascript ์์ค์ ๋๋ค.
window.addEventListener("scroll", ()=>{
let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;
//01
// if(scrollTop > 0){
// document.querySelector("#parallax__nav").classList.add("show");
// } else {
// document.querySelector("#parallax__nav").classList.remove("show");
// } ์คํฌ๋กค๋ฐ 0๋ณด๋ค ์ปค์ง๋ฉด ๋ฉ๋ด๊ฐ ๋์จ๋ค.
02
if(scrollTop > (document.body.scrollHeight - window.outerHeight) ){
document.querySelector("#parallax__top").classList.add("show");
} else {
document.querySelector("#parallax__top").classList.remove("show");
}
console.log();
document.querySelector("#parallax__info span").innerText = Math.floor(scrollTop);
// document.querySelector("#parallax__info span").innerText = Math.ceil(scrollTop);
})
// top ๋ฒํผ
document.querySelector("#parallax__top").addEventListener("click", ()=>{
window.scrollTo({left: 0, top: 0, behavior: "smooth"});
});
//03
let nowScroll = true;
let lastScroll = 0;
function scrollProgress(){
nowScroll = true;
setInterval(() => {
if(nowScroll){
nowScroll = false;
hasScroll();
}
}, 300);
}
function hasScroll(){
let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;
if(scrollTop < lastScroll){
document.querySelector("#parallax__nav").classList.add("show");
} else {
document.querySelector("#parallax__nav").classList.remove("show");
}
lastScroll = scrollTop;
}
window.addEventListener("scroll", scrollProgress);
728x90
๋ฐ์ํ
'์ดํํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ง์ฐ์ค ์ดํํธ 2. ๋ง์ฐ์ค ๋ฐ๋ผ๋ค๋๊ธฐ (GSAP) (3) | 2022.09.19 |
---|---|
ํจ๋ด๋์ค ์ดํํธ 4. ๋ด์ฉ ๋ํ๋๊ธฐ (3) | 2022.09.19 |
ํจ๋ด๋ญ์ค ์ดํํธ - 02. ์ฌ์ด๋ ๋ฉ๋ด (1) | 2022.09.13 |
ํจ๋ด๋ ์ค ์ดํํธ - 1. ๋ฉ๋ด ์ด๋ (6) | 2022.09.06 |
๋ง์ฐ์ค ์ดํํธ - ๋ง์ฐ์ค ๋ฐ๋ผ๋ค๋๊ธฐ (1) | 2022.09.06 |
๋๊ธ