본문 바로가기
Effect

parallax Effect - 01

by 코딩 냠냠 2022. 9. 6.
728x90
반응형

스크립트를 이용한 패럴랙스효과 만들기 01

패럴랙스는 시차라는 뜻을 가지고 있으며, 스크롤링을 했을 때 발생하는 시간차에 따라 변화함으로써 보다 입체감이 있는 구현을 할 수 있습니다.
offsetTop()을 이용해서 Y의 값을 구해보도록 합시다✏️

Script

//scrollTop
// let scrollTop = window.pageYOffset;
// let scrollTop = window.scrollY;
// let scrollTop = document.documentElement.scrollTop;
let scrollTop = window.pageYOffset || window.scrollY || document.documentElement.scrollTop;

스크롤 값을 구하기 위하여 스크립트를 작성해 줍니다. pageYOffset, scrollY, scrollTop 각각 선택해서 사용할 수 있지만, 브라우저마다 각각의 지원여부가 다르니 ||(or)를 이용해서 모두 작성해줍니다.

scrollTop 값 입니다

//for문 영역의 값
for(let i = 1 ; i < 10 ; i++) {
    document.querySelector(".offset"+i).innerText = document.getElementById("section"+ i).offsetTop;
};

for()하여 각각의 영역의 값을 구할 수 있습니다.

//fotEach문
document.querySelectorAll(".content__item").forEach ((element, index) => {
    if(scrollTop >= element.offsetTop - 2) {
        document.querySelectorAll("#parallax__nav li").forEach(li => {
            li.classList.remove("active")
        });
        document.querySelector("#parallax__nav li:nth-child(" + (index+1) + ")").classList.add("active")
    };
    
})
document.querySelector(".scroll span").innerText = Math.round(scrollTop);

forEach()와 if문을 이용하여 scrollTop값이 브라우저의 각 content__item의 값보다 크거나 같다면 li에 활성화된 .active를 지워주고, 각 영역에 해당하는 li애는 .active를 활성화 시켜 줍니다🪄

Math.round();

정수를 반올림 시켜주는 메서드 입니다✏️

for문
//for문
for(let i = 1; i < 10; i++) {
    if(scrollTop >= document.getElementById("section"+i).offsetTop){
        document.querySelectorAll("#parallax__nav li").forEach(li => {
            li.classList.remove("active");
        })
        document.querySelector("#parallax__nav li:nth-child("+ i + ")").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"
        });
    });
});

스크롤 이동이 부드럽게 이어지도록 'smooth'를 추가 해줍니다.

HTML

<nav id="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>

<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>


<aside id="parallax__info">
    <div class="scroll">scrollTop : <span>0</span>px</div>
    <div class="scroll">
        <ul>
            <li>section1 offset() : <span class="offset1">0</span>px</li>
            <li>section2 offset() : <span class="offset2">0</span>px</li>
            <li>section3 offset() : <span class="offset3">0</span>px</li>
            <li>section4 offset() : <span class="offset4">0</span>px</li>
            <li>section5 offset() : <span class="offset5">0</span>px</li>
            <li>section6 offset() : <span class="offset6">0</span>px</li>
            <li>section7 offset() : <span class="offset7">0</span>px</li>
            <li>section8 offset() : <span class="offset8">0</span>px</li>
            <li>section9 offset() : <span class="offset9">0</span>px</li>
        </ul>
    </div>
</aside>

CSS

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;
}
#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-color: #fff;
    color: #222;
    border-radius: 20px;
    box-sizing: content-box;
}

/* parallax__cont */
#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.3); */
    text-align: left;
    margin-right: 0;
    position: relative;
    padding-top: 9vw;
}
.content__item:nth-child(even) {      /* 2n: n은 변수, even: 짝수만 선택 */
    margin-left: 0;
    text-align: right;
}
.content__item:nth-child(even) .content__item__num {
    left: auto;  /*left 값 초기화 */
    right: -5vw;
}
.content__item__num {
    font-size: 35vw;
    font-family: 'Lato';
    font-weight: 100;
    position: absolute;
    left: -5vw;
    top: -13vw;
    opacity: 0.07;
    z-index: -2;
}
.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 {
    position: absolute;
    background-image: url(../assets/img/effect_bg19.jpg);
    background-repeat: no-repeat;
    background-position: center center;
    width: 110%;
    background-size: cover;
    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_bg13.jpg);
}
.content__item:nth-child(2) .content__item__img{
    background-image: url(../assets/img/effect_bg19.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_bg09.jpg);
}
.content__item:nth-child(5) .content__item__img{
    background-image: url(../assets/img/effect_bg18.jpg);
}
.content__item:nth-child(6) .content__item__img{
    background-image: url(../assets/img/effect_bg17.jpg);
}
.content__item:nth-child(7) .content__item__img{
    background-image: url(../assets/img/effect_bg06.jpg);
}
.content__item:nth-child(8) .content__item__img{
    background-image: url(../assets/img/effect_bg01.jpg);
}
.content__item:nth-child(9) .content__item__img{
    background-image: url(../assets/img/effect_bg15.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-color: 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: 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;
    }
}

'Effect' 카테고리의 다른 글

mouseEffect 01  (2) 2022.09.19
parallax Effect - 02  (1) 2022.09.13
SliderEffect - 03  (1) 2022.09.02
SliderEffect - 02  (3) 2022.08.29
SliderEffect - 01  (3) 2022.08.29

댓글


자바스크립트

Javascript

자세히 보기
html
css
광고 준비중입니다.
<