Dragonball Online Goku Hand
본문 바로가기
Effect(자바스크립트 활용)/Parallax Effect 패럴럭스 이펙트

"패럴랙스 이펙트 06" : 텍스트 효과

by 별의 코비 2022. 9. 29.
728x90

🔮 패럴랙스 이펙트 - 텍스트 효과

📌 이번 이펙트는 스크롤했을 때 글자가 한 글자씩 transition-delay를 통해 도로로로로 나타나는 이펙트입니다.

➰ HTML

스크롤값도 나오게 해줄 것이므로 parallax__info도 만들어줍니다.

코드 보기
    <header id="header">
    <h1><a href="parallaxEffect.html"> Javascript parallax sliderEffect06</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><a href="parallaxEffect05.html">5</a></li>
        <li class="active"><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">
        <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 split">오늘 할 수 있는 일에 전력을 다하라. 그러면 내일에는 한걸음 더 진보한다.</p>
        </section>
        <!-- //section1 -->
        <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 split">게으르지 않음은 영원한 삶의 집이요, 게으름은 죽음의 집이다.</p>
        </section>
        <!-- //section2-->
        <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 split">우리는 행복하기 때문에 웃는게 아니라, 웃기 때문에 행복하다.</p>
        </section>
        <!-- //section3 -->
        <section id="section4" class="content__item">
            <span class="content__item__num">04</span>
            <h2 class="content__item__title split">section4</h2>
            <figure class="content__item__imgWrap">
                <div class="content__item__img"></div>
            </figure>
            <p class="content__item__desc split">인생은 가까이서 보면 비극, 멀리서 보면 희극이다.</p>
        </section>
        <!-- //section4 -->
        <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 split">승자는 시간을 관리하며 살고 패자는 시간에 끌려 산다.</p>
        </section>
        <!-- //section5 -->
        <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 split">아무것도 하지 않으면 아무 일도 일어나지 않는다.</p>
        </section>
        <!-- //section6 -->
        <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 split">일단 시작해라. 나중에 완벽해지면 된다.</p>
        </section>
        <!-- //section7 -->
        <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 split">모든 이의 마음을 얻으려고 계산된 글은 그 누구의 마음도 얻지 못한다.</p>
        </section>
        <!-- //section8 -->
        <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 split">우리 자신에게 실패를 허락하는 것이 성공을 허락하는 것이다.</p>
        </section>
        <!-- //section9 -->
    </div>
</main>
<!-- //main -->
<aside id="parallax__info">
    <div class="scroll">scrollTop : <span>0</span>px</div>
</aside>

➰ CSS

html틀을 구성해 준 다음, 그에 맞는 스타일을 줍니다.

코드 보기
/* 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 li {
    display: inline;
    margin: 0 5px;
}
#parallax__nav li a{
    /* background-color: #ffee00; */
    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;
    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: 7vw;
}
.content__item:nth-child(2n){
    /* 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 {
    /* left초기화 기준점 right로 바뀜*/
    left: auto;
    right: -5vw;
}
.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;
    background-image: url(../assets/img/effect_bg05.jpg);
    background-repeat: no-repeat;
    background-position: center center;
     /* 사진안짤리게 */
    background-size: cover;
    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/img/effect_bg01.jpg);
}
.content__item:nth-child(2) .content__item__img {
    background-image: url(../assets/img/effect_bg02.jpg);
}
.content__item:nth-child(3) .content__item__img {
    background-image: url(../assets/img/effect_bg03.jpg);
}
.content__item:nth-child(4) .content__item__img {
    background-image: url(../assets/img/effect_bg04.jpg);
}
.content__item:nth-child(5) .content__item__img {
    background-image: url(../assets/img/effect_bg05.jpg);
}
.content__item:nth-child(6) .content__item__img {
    background-image: url(../assets/img/effect_bg06.jpg);
}
.content__item:nth-child(7) .content__item__img {
    background-image: url(../assets/img/effect_bg07.jpg);
}
.content__item:nth-child(8) .content__item__img {
    background-image: url(../assets/img/effect_bg08.jpg);
}
.content__item:nth-child(9) .content__item__img {
    background-image: url(../assets/img/effect_bg09.jpg);
}
.content__item__desc {
    font-size: 4vw;
    line-height: 1.4;
    margin-top: -5vw;
    margin-left: -4vw;
    word-break: keep-all;
}

/* 홀수는 odd랍니다. */
.content__item:nth-child(even) .content__item__desc {
    margin-left: auto;
    margin-right: -4vw;
}
@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: rgb(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 {
    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;
}

/* option */
.split span{
    opacity: 0;
    transform: translateY(50px);
    transition: all 0.3s cubic-bezier(0, 0.71, 0.11,);
    display: inline-block;
    min-width: 1vw;
}
.split span.show{
    opacity: 1;
    transform: translateY(0);
}
/* 노가다 -> 나중에 스크립트로 처리 */
/* .split span:nth-child(1) {transition-delay: 100ms;}
.split span:nth-child(2) {transition-delay: 150ms;}
.split span:nth-child(3) {transition-delay: 200ms;}
.split span:nth-child(4) {transition-delay: 250ms;}
.split span:nth-child(5) {transition-delay: 300ms;}
.split span:nth-child(6) {transition-delay: 350ms;}
.split span:nth-child(7) {transition-delay: 400ms;}
.split span:nth-child(8) {transition-delay: 450ms;}
.split span:nth-child(9) {transition-delay: 500ms;}
.split span:nth-child(10) {transition-delay: 550ms;}
.split span:nth-child(11) {transition-delay: 600ms;}
.split span:nth-child(12) {transition-delay: 650ms;}
.split span:nth-child(13) {transition-delay: 700ms;}
.split span:nth-child(14) {transition-delay: 750ms;}
.split span:nth-child(15) {transition-delay: 800ms;}
.split span:nth-child(16) {transition-delay: 850ms;}
.split span:nth-child(17) {transition-delay: 900ms;}
.split span:nth-child(18) {transition-delay: 950ms;}
.split span:nth-child(19) {transition-delay: 1000ms;}
.split span:nth-child(20) {transition-delay: 1050ms;}
.split span:nth-child(21) {transition-delay: 1100ms;}
.split span:nth-child(22) {transition-delay: 1150ms;}
.split span:nth-child(23) {transition-delay: 1200ms;}
.split span:nth-child(24) {transition-delay: 1250ms;}
.split span:nth-child(25) {transition-delay: 1300ms;}
.split span:nth-child(26) {transition-delay: 1350ms;}
.split span:nth-child(27) {transition-delay: 1400ms;}
.split span:nth-child(28) {transition-delay: 1450ms;}
.split span:nth-child(29) {transition-delay: 1500ms;}
.split span:nth-child(30) {transition-delay: 1550ms;}
.split span:nth-child(31) {transition-delay: 1600ms;}
.split span:nth-child(32) {transition-delay: 1650ms;}
.split span:nth-child(33) {transition-delay: 1700ms;}
.split span:nth-child(34) {transition-delay: 1750ms;}
.split span:nth-child(35) {transition-delay: 1800ms;} */

➰ 자바스크립트 활용

주석을 참고해주세요!

코드 보기
//글씨 쪼개기
let text = document.querySelectorAll("#contents .content__item__desc");
// let splitText = text.innerText;
// let splitWrap = splitText.split('').join('');
// 앞뒤 span 완전하게 안나와서 splitWrap = "" + splitWrap + "" 써주고 innerHTML로 저장 후 뿌려주기
// text.innerHTML = splitWrap = "" + splitWrap + "" ;

//글씨 쪼개기(다중이)
document.querySelectorAll(".split").forEach(text => {
    //글씨 가져오기
    let splitText = text.innerText;
    //쪼개기
    let splitWrap = splitText.split('').join(("

댓글