본문 바로가기

아래로 스크롤 해주세요!

My Reference Book

-

제가 배웠던것을 한곳에 정리해보았어요!

HTML

HTML 태그 톺아보기

HTML

자세히보기

CSS

CSS 속성 톺아보기

CSS

자세히보기

JAVASCRIPT

JS 실행문 톺아보기

JAVASCRIPT

자세히보기

최신댓글

JS 응용하기

패럴렉스 이펙트_05 : 이질감이 느껴지는 패럴렉스 이펙트 만들어보기!

by C0Di 2022. 9. 20.
728x90

패럴렉스 이펙트 05 : 스크롤 할때 각 요소에 이질감 효과 주기

사이트를 돌아다니다보면 스크롤 할때마다 각 요소가 일정한 차이를 두고 높이가 달라지는 효과가 있는데 이번에는 그 효과를 구현해보도록 하겠습니다.


HTML 코드 : 간단한 예제용

이번 예제는 이전 예제의 뼈대만 필요하니 하단의 HTML코드를 그대로 작성해줍니다!

<body class="img07">
    <header id="header">
        <h1><a href="parallaxEffect.html">Javascript parallax Effect05</a></h1>
        <p>패럴렉스 이펙트 - 나타나기</p>
        <ul>
            <li><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 class="active"><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 -->
    
    <main id="parallax__cont">
        <div id="contents">

            <!-- section1 시작 -->
            <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 끝 -->

            <!-- section2 시작 -->
            <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 끝 -->

            <!-- section3 시작 -->
            <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 끝 -->

            <!-- section4 시작 -->
            <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 끝 -->

            <!-- section5 시작 -->
            <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 끝 -->

            <!-- section6 시작 -->
            <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 끝 -->

            <!-- section7 시작 -->
            <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 끝 -->

            <!-- section8 시작 -->
            <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 끝 -->

            <!-- section9 시작 -->
            <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>

CSS 코드 : 예제에 살 붙이기

이전 예제에서 큰 차이는 없습니다
그저 살을 붙여주기만 하면 되기 때문에 하단의 코드를 작성해줍시다!

/* 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;
        transition: top 0.4s ease;
    }

    #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;
        /* background-color: rgba(255, 255, 255, 0.1); */
        margin: 0 auto;
        width: 98%;
    }

    .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) {
        /* 2n도 가능하고 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;
        /* left 값 초기화 */
    }

    .content__item__title {
        font-weight: 400;
        text-transform: capitalize;
    }

    .content__item__imgWrap {
        width: 100%;
        padding-bottom: 56.25%;
        background-color: #000;
        position: relative;
        overflow: hidden;
        z-index: -1;
    }

    .content__item__img {
        background: url(../../assets/slider/effect_bg_01.jpg);
        background-position: center center;
        background-repeat: no-repeat;
        background-size: cover;
        /* 내가 작아서 넣음 */
        position: absolute;
        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/slider/effect_bg_01.jpg);
    }

    .content__item:nth-child(2) .content__item__img {
        background-image: url(../../assets/slider/effect_bg_02.jpg);
    }

    .content__item:nth-child(3) .content__item__img {
        background-image: url(../../assets/slider/effect_bg_03.jpg);
    }

    .content__item:nth-child(4) .content__item__img {
        background-image: url(../../assets/slider/effect_bg_04.jpg);
    }

    .content__item:nth-child(5) .content__item__img {
        background-image: url(../../assets/slider/effect_bg_05.jpg);
    }

    .content__item:nth-child(6) .content__item__img {
        background-image: url(../../assets/slider/effect_bg_06.jpg);
    }

    .content__item:nth-child(7) .content__item__img {
        background-image: url(../../assets/slider/effect_bg_07.jpg);
    }

    .content__item:nth-child(8) .content__item__img {
        background-image: url(../../assets/slider/effect_bg_08.jpg);
    }

    .content__item:nth-child(9) .content__item__img {
        background-image: url(../../assets/slider/effect_bg_09.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.6);
        color: #fff;
        padding: 20px;
        border-radius: 10px;
    }

    #parallax__info li,
    .scrollTop {
        line-height: 1.4;
    }

    @media (max-width: 800px) {
        #parallax__cont {
            margin-top: 70vw;
        }

        #parallax__nav {
            padding: 10px;
            right: auto;
            left: 10px;
            top: 10px;
            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;
        }
    }
    

JS 코드 : 예제에 살 붙이기

1. 각 요소의 이질감 효과를 주기위해 offsetTop 을 이용하여 각 요소의 브라우저 최상단에서의 거리를 구합니다
2. 그 값을 scrollTop(내가 스크롤한 값)을 빼주어 각 요소가 브라우저 내부에서 스크롤한 값에 맞춰 실시간으로 어디에 위치하는지 알아낸뒤
3. 각 요소의 translateY,X 값을 offset 값으로 설정해주어 이동시키도록 설정해줍니다.

function scroll(){
    let scrollTop = window.pageYOffset || document.documentElement.scrollTop;

    document.querySelector("#parallax__info span").innerHTML=Math.ceil(scrollTop);

    document.querySelectorAll(".content__item").forEach(item => {
        const target1 = item.querySelector(".content__item__img");
        const target2 = item.querySelector(".content__item__desc");
        const target3 = item.querySelector(".content__item__num");

        let offset = (scrollTop - item.offsetTop) * 0.1; //스크롤한 값 - 각 아이템의 브라우저 TOp값
        let offset2 = (scrollTop - item.offsetTop) * 0.15; //스크롤한 값 - 각 아이템의 브라우저 TOp값
        let offset3 = (scrollTop - item.offsetTop) * 0.2; //스크롤한 값 - 각 아이템의 브라우저 TOp값

        // target1.style.transform = `translateY(${offset}px)`;
        // target2.style.transform = `translateX(${offset2}px)`;
        gsap.to(target1, {duration: 0.3, y: offset, ease: "power4.out"});
        gsap.to(target2, {duration: 0.3, y: offset2});
        gsap.to(target3, {duration: 0.3, y: offset3});
        
    });

    requestAnimationFrame(scroll);
}
scroll();


728x90
반응형

댓글

#HASH_TAGS

-

1

오징어 한마리 수확 완료! 코딩 선택해주세요 ImageSlideEffect 공부 내일은 즐거운 월요일 멈추지 않는 ' j ' 시리-즈 HTML 오징어 1Kg 당 3000원 숙제가 다양해서 너무 좋아요 코드 오늘 조업 마감했습니다. 필터선택자 울적하니 꽃을 달아봤습니다 제이쿼리 scroll-snap-align 메서드 오늘도 웹표준은.. 오늘은 내가바로 오징어! 다크모드 화사한가요? 슬라이드 결과 : 월요일 오징어 두마리 포획 완료 scroll-snap-type Method JQuery 이건 또 뭐람