Animated Turtle

Javascript

패럴랙스 이펙트

훙구 2023. 4. 18. 22:41

...

728x90
반응형

각 섹션의 좌표와 스크롤 좌표를 이용한 패럴랙스 이펙트 만들기

스크롤의 좌표와 각 컨텐츠들의 좌표를 이용하여 패럴랙스 효과를 만들어 보도록 하겠습니다.

 

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 gmarketsans">
    <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>
    <!-- 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">Section1</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">Section2</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">Section3</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">Section4</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">Section5</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">Section6</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">Section7</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">Section8</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">Section9</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 offsetTop() : <span class="offset1">0</span>px</li>
                <li>#section2 offsetTop() : <span class="offset2">0</span>px</li>
                <li>#section3 offsetTop() : <span class="offset3">0</span>px</li>
                <li>#section4 offsetTop() : <span class="offset4">0</span>px</li>
                <li>#section5 offsetTop() : <span class="offset5">0</span>px</li>
                <li>#section6 offsetTop() : <span class="offset6">0</span>px</li>
                <li>#section7 offsetTop() : <span class="offset7">0</span>px</li>
                <li>#section8 offsetTop() : <span class="offset8">0</span>px</li>
                <li>#section9 offsetTop() : <span class="offset9">0</span>px</li>
            </ul>
        </div>
    </aside>
    <!-- parallax__info -->

    <footer id="footer">
        <a href="mailto:gnsrbdi@naver.com">gnsrdbi@naver.com</a>
    </footer>
</body>
</html>

HTML 구조 참고

구조는 위와 같이 구성했습니다.

CSS 작성하기

/* header */
#header {
    position: absolute;
    left: 20px;
    top: 20px;
    color: #fff;
}

#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: 30%;
    transition: all 0.2s;
    text-align: center;
}

#header li a:hover {
    background-color: #fff;
    color: #000;
}

#header li.active a {
    background-color: #fff;
    color: #000;
}

/* footer */
#footer {
    text-align: center;
    padding: 200px 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);
    color: #fff;
}
.parallax__item {
    width: 1000px;
    max-width: 70vw;
    margin: 30vw auto;
    /* background-color: rgba(255, 255, 255, 0.1); */
    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%; /* 가상 height */
    position: relative;
    z-index: -1;
}
.parallax__item__img {
    position: absolute;
    left: 0;
    top: 0;
    background-image: url(../img/parallaxEffect01-min.jpg);
    width: 100%;
    height: 100%;
    background-repeat: no-repeat;
    background-position: center center;
    background-size: cover;
    filter: saturate(10%);
    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: 4vw;
    line-height: 1.4;
    margin-top: -5vw;
    margin-left: -4vw;
    word-break: keep-all;
    font-weight: 100;
}
.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: 0 1px;
    }
    .parallax__nav li a {
        font-size: 12px;
        padding: 0px 14px;
    }
}

CSS 참고

  • parallax__item__imgWrap 부분에 padding값을 56.25%를 주었는데, height값을 고정 시켜놓지 않고 가상의 height값 처럼 쓰기 위함입니다. (반응형 고려)
  • css에서 filter 속성을 이용하여 이미지 간단한 편집이 가능합니다.
  • 본문의 메뉴9번이 작동하기 위해 section9의 y좌표 값이 위로 올라올 수 있도록 footer의 패딩값을 많이 주었습니다.

JAVASCRIPT 작성하기

// 선택자
const parallaxInfo = document.querySelector(".parallax__info");
const scroll = parallaxInfo.querySelector(".scroll span");
const offSet = parallaxInfo.querySelectorAll(".info span");

window.addEventListener("scroll", () => {
    let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;

    document.querySelectorAll(".parallax__item").forEach((item, index) => {
        if(scrollTop >= item.offsetTop - 5){
            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"
            })
        });
    });

    scroll.innerHTML = scrollTop;

    // 기본형
    document.querySelector(".info .offset1").innerHTML = document.getElementById("section1").offsetTop;
    document.querySelector(".info .offset2").innerHTML = document.getElementById("section2").offsetTop;
    document.querySelector(".info .offset3").innerHTML = document.getElementById("section3").offsetTop;
    document.querySelector(".info .offset4").innerHTML = document.getElementById("section4").offsetTop;
    document.querySelector(".info .offset5").innerHTML = document.getElementById("section5").offsetTop;
    document.querySelector(".info .offset6").innerHTML = document.getElementById("section6").offsetTop;
    document.querySelector(".info .offset7").innerHTML = document.getElementById("section7").offsetTop;
    document.querySelector(".info .offset8").innerHTML = document.getElementById("section8").offsetTop;
    document.querySelector(".info .offset9").innerHTML = document.getElementById("section9").offsetTop;
});

Javscript 정리해보기

  • window 자체에 scroll에 대한 Event를 추가하였습니다.
  • scrollTop변수에 스크롤 된 Y좌표의 값을 구해 저장하였습니다. (3가지 방법)
  • 각각의 parallax__item의 Y좌표 값이 scrollTop의 Y값보다 작거나 같다면 parallax__nav의 모든 li의 classList에서 active를 제거하고, (index + 1) 번째 자식요소 classList에 active를 추가합니다.
  • parallax__nav li a의 모든 요소에게 preventDefault메서드를 사용해 기본동작을 중단시킵니다. (클릭 막음)
  • 왼쪽 하단 section 목록의 offsetTop 을 구하는 박스가 있습니다. (".info .offset$")
  • 각각의 span에 section.offsetTop의 값을 출력했습니다.
  • 출력문을 아래에 여러가지 방법으로 사용해보겠습니다.

 

for문 사용

for (let i=0; i<=8; i++){
    offSet[i].innerHTML = document.getElementById(`section${i+1}`).offsetTop;
}
  • i가 0부터 8까지 반복하는 기본적인 반복문을 사용한 방법입니다.

forEach 사용

offSet.forEach((el, i) => {
    el.innerHTML = document.getElementById(`section${i+1}`).offsetTop;
});
  • offSet ( .info span을 선택자로 지정 )에 forEach를 사용하여 el(요소)과, i(인덱스)를 사용합니다.
  • 각각의 el에 section(i+1)을 사용했습니다.

for in 사용

for(let index in offSet){
    if(index.match(/[a-z]/) == null){
        offSet[parseInt(index)].innerHTML = document.getElementById(`section${parseInt(index)+1}`).offsetTop;
    }
}
  • index를 사용할 수 있는 for in 문입니다.
  • 배열이 아닌 Nodelist에 사용한 것이라 기본적인 속성까지도 가져오기 때문에 if문에 match를 사용하여 index에 length, item 등등 문자로 적힌 속성이 아닐 때 실행되도록 했습니다.
  • index에 parseInt를 사용하여 확실하게 숫자로 받아와 사용했습니다.

for of 사용

let index = 1;
for(let span of offSet){
    span.innerHTML = document.getElementById(`section${index}`).offsetTop;
    index++;
}
  • 배열의 요소를 사용할 수 있는 for of 문입니다.
  • 각각의 요소를 span이라 지정하고 각각의 span에 따로 지정해 놓은 index의 값을 사용해 section(index+1)의 좌표를 받아와 사용했습니다.

 

 

 

 

이상으로 패럴랙스 이펙트의 기본적인 페이지를 만들어 보았습니다 !

728x90
반응형