카테고리 없음

패럴랙스 효과 03/04

Kim do hyun 2023. 5. 15. 19:25
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>패럴랙스 이펙트03</title>

    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/parallax.css">
    <style>
        .parallax__nav {
            top: -100px;
            transition: all 0.6s;
        }
        .parallax__nav.show {
            top: 10px;
        }
    </style>
</head>
<body class="img01 bg01 Eulyoo">
    <header id="header">
        <h1>Javascript Parallax Effect03</h1>
        <p>패럴랙스 이펙트 : 숨김 메뉴 / 탑 버튼</p>
        <ul>
            <li><a href="parallaxEffect01.html">1</a></li>
            <li><a href="parallaxEffect02.html">2</a></li>
            <li class="active"><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">5</a></li>
            <li><a href="parallaxEffect07.html">5</a></li>
        </ul>
    </header>
    <!-- header -->

    <nav class="parallax__nav show">
        <ul>
            <li class="active"><a href="#section1">메뉴1</a></li>  <!-- li*9>a[href="#section$"]{메뉴$} -->
            <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">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">하루에 3시간을 걸으면 7년 후에 지구를 한바퀴 돌 수 있다.</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">1퍼센트의 가능성, 그것이 나의 길이다. </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>
        
    </aside>
    <!--//parallax__info-->

    <div class="parallax__top">
        <a>Top</a>
    </div>
    <!-- parallax__top -->

    <footer id="footer">
        <a href="mailto:kshkmn0929@naver.com">kshkmn0929@naver.com</a>
    </footer>
    <!-- footer -->

 

CSS

@import url('https://fonts.googleapis.com/css2?family=Lato:wght@100&display=swap');
main {
    width: 100%;
    overflow: hidden;
}
/* 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: 50%;
    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: 78vw;
    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: 300;
    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;
    overflow: hidden;
}
.parallax__item__img {
    position: absolute;
    left: -5%;
    top: -5%;
    width: 110%;
    height: 110%;
    background-image: url(../img/parallaxEffect01.jpg);
    background-repeat: no-repeat;
    background-position: center center;
    background-size: cover;
    filter: saturate(0%);
    transition: filter 1s;
}
.parallax__item:nth-child(1) .parallax__item__img {
    background-image: url(../img/parallaxEffect01.jpg);
}
.parallax__item:nth-child(2) .parallax__item__img {
    background-image: url(../img/parallaxEffect02.jpg);
}
.parallax__item:nth-child(3) .parallax__item__img {
    background-image: url(../img/parallaxEffect03.jpg);
}
.parallax__item:nth-child(4) .parallax__item__img {
    background-image: url(../img/parallaxEffect04.jpg);
}
.parallax__item:nth-child(5) .parallax__item__img {
    background-image: url(../img/parallaxEffect05.jpg);
}
.parallax__item:nth-child(6) .parallax__item__img {
    background-image: url(../img/parallaxEffect06.jpg);
}
.parallax__item:nth-child(7) .parallax__item__img {
    background-image: url(../img/parallaxEffect07.jpg);
}
.parallax__item:nth-child(8) .parallax__item__img {
    background-image: url(../img/parallaxEffect08.jpg);
}
.parallax__item:nth-child(9) .parallax__item__img {
    background-image: url(../img/parallaxEffect09.jpg);
}
.parallax__item__desc {
    font-size: 3.5vw;
    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;
}

.parallax__dot {
    position: fixed;
    right: 20px;
    top: 50%;
    transform: translateY(-50%);
    z-index: 1000;
    background-color: 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.1);
    text-indent: -9999px;
    transition: box-shadow .2s ease;
}
.parallax__dot a {
    background: #fff;
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 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__top {
    position: fixed;
    right: -200px;
    bottom: 20px;
    z-index: 1000;
    transition: all 0.3s;
}
.parallax__top.show {
    right: 20px;
}
.parallax__top a {
    display: block;
    width: 40px;
    height: 40px;
    line-height: 40px;
    background-color: rgba(255, 255, 255, 1);
    border-radius: 50%;
    color: #000;
    text-align: center;
    font-size: 14px;
    cursor: pointer;
}
.parallax__top a:hover {
    background-color: #000;
    color: #fff;
}

@media (max-width: 1200px){
    .parallax__nav {
        padding: 10px;
        background-color: rgba(0, 0, 0, 0);
        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;
    }
}

 

JAVASCRIPT

// 01 메뉴
// window.addEventListener("scroll", () => {
//     let scrollTop = window.pageYOffset || window.scrollY;

//     if(scrollTop > 0){
//         document.querySelector(".parallax__nav").classList.add("show");
//     } else {
//         document.querySelector(".parallax__nav").classList.remove("show");
//     }
//     document.querySelector(".scroll span").innerText = Math.round(scrollTop);

// });

    // 문서의 높이
    // document.body.scrollHeight
    // document.body.offsetHeight
    // document.documentElement.scrollHeight
    // document.documentElement.offsetHeight

    // 탑 버튼
window.addEventListener("scroll", () => {

    let scrollTop = window.pageYOffset || window.scrollY;

    if(scrollTop > document.body.scrollHeight - 1000){
            document.querySelector(".parallax__top").classList.add("show");
        } else {
            document.querySelector(".parallax__top").classList.remove("show");
    }
});
document.querySelector(".parallax__top").addEventListener("click", () => {
    window.scrollTo({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;

    if(scrollTop < lastScroll){
        document.querySelector(".parallax__nav").classList.add("show");
    } else {
        document.querySelector(".parallax__nav").classList.remove("show");
    }
    lastScroll = scrollTop;
    document.querySelector(".scroll span").innerText = Math.round(scrollTop);
}

window.addEventListener("scroll", scrollProgress);

코드 설명

스크롤 이벤트 리스너 등록
웹 페이지를 스크롤할 때 발생하는 이벤트를 감지하고, 이벤트가 발생하면 실행할 함수를 등록하는 코드입니다.

스크롤 위치에 따른 상황 처리
웹 페이지를 스크롤할 때 스크롤 위치를 계산하고, 스크롤 위치에 따라 상황을 처리하는 코드입니다. 스크롤 위치가 일정 값 이하로 내려가면 .parallax__top 클래스를 가진 요소에 show 클래스를 추가합니다. 반대로, 스크롤 위치가 일정 값 이상으로 올라가면 show 클래스를 삭제합니다. 또한, 스크롤 위치를 계산하여 .scroll span 요소의 내용을 업데이트합니다.

메뉴 관련 처리
웹 페이지를 스크롤할 때, 스크롤 위치에 따라 .parallax__nav 클래스를 가진 요소에 show 클래스를 추가 또는 삭제하는 코드입니다. scrollProgress 함수는 0.3초 간격으로 hasScroll 함수를 실행합니다. hasScroll 함수는 스크롤 위치를 계산하고, 이전 스크롤 위치와 비교하여 .parallax__nav 요소에 show 클래스를 추가 또는 삭제합니다.