๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Javascript/Effect

๋งˆ์šฐ์Šค ์ดํŽ™ํŠธ 01 : transform & transition ํšจ๊ณผ

by oranssy 2022. 9. 23.
728x90
๋ฐ˜์‘ํ˜•

โญ๏ธ ๋งˆ์šฐ์Šค ์ดํŽ™ํŠธ 01 : transform & transition ํšจ๊ณผ โญ๏ธ


01. ๋งˆ์šฐ์Šค ํšจ๊ณผ ๋งŒ๋“ค์–ด๋ณด๊ธฐ : ๋ชฉํ‘œ

๐ŸงŠ ์ •ํ•ด์ง„ ๊ธ€์ž ์œ„์—์„œ ๋งˆ์šฐ์Šค ์ปค์„œ์˜ ํ˜•ํƒœ๊ฐ€ ๋ณ€ํ˜•๋˜๋Š” ํšจ๊ณผ๋ฅผ ๋‚˜ํƒ€๋‚ด๋ด…๋‹ˆ๋‹ค.
๐ŸงŠ ์ปค์„œ ๋ชจ์–‘ ๋ณ€ํ˜• ํšจ๊ณผ์˜ ์†๋„๋ฅผ ์กฐ์ ˆํ•ด ๋ด…๋‹ˆ๋‹ค.

[1] ์ปค์„œ ๋ชจ์–‘ div ๋งŒ๋“ค๊ณ  ๋ฌธ์žฅ ์ž‘์„ฑํ•˜๊ธฐ (html)

section ํƒœ๊ทธ๋ฅผ ๋งŒ๋“ค๊ณ , css ์™€ script ์˜ ๊ฒฝ๋กœ ์„ค์ •์„ ์œ„ํ•ด id ๋ฐ class๋กœ ์ด๋ฆ„์„ ์ •ํ•ฉ๋‹ˆ๋‹ค.
๋ฌธ์žฅ์—์„œ ์ •ํ•ด์ง„ ๊ธ€์ž ์œ„์— ์ปค์„œ๋ฅผ ์˜ฌ๋ ธ์„ ๋•Œ, ์ปค์„œ์˜ ํ˜•ํƒœ๊ฐ€ ๋ณ€ํ˜•๋˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด span ํƒœ๊ทธ๋กœ ๊ธ€์ž๋ฅผ ๊ฐ์‹ธ์ค๋‹ˆ๋‹ค.
span ํƒœ๊ทธ์˜ css์™€ script ์˜ ๊ฒฝ๋กœ ์„ค์ •์„ ์œ„ํ•ด class = style($num) ๋กœ ์ด๋ฆ„์„ ์ง€์–ด์ค๋‹ˆ๋‹ค.

   ___ ์ฐธ๊ณ ํ•˜๊ธฐ ___
  `1   a href ์— class ="ํด๋ž˜์Šค๊ฐ’" ๋˜๋Š” id ="์•„์ด๋””๊ฐ’" ์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋งํฌ ์ด๋™์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

<section id="mouseType">
    <div class="mouse__cursor"></div>
    <div class="mouse__wrap">
        <p>The <span class="style1">greatest glory</span> in living lies not in <span
                class="style2">never</span> falling, but in
            <span class="style3">rising</span> every time we fall. - Nelson Mandela
        </p>
        <p>์ธ์ƒ์—์„œ ๊ฐ€์žฅ <span class="style4">ํฐ ์˜๊ด‘</span>์€ ๋„˜์–ด์ง€์ง€ <span class="style5">์•Š๋Š” ๊ฒƒ</span>์— ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋งค๋ฒˆ <span
                class="style6">์ผ์–ด์„ ๋‹ค</span>๋Š” ๋ฐ ์žˆ๋‹ค. - ๋„ฌ์Šจ ๋งŒ๋ธ๋ผ</p>
    </div>
</section>

[2] ์ปค์„œ ๋ชจ์–‘ ๋ฐ ๋ฌธ์žฅ div ๋””์ž์ธํ•˜๊ธฐ (css)

๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด์— ์–ด๋–ป๊ฒŒ ๋ณด์—ฌ์ง€๋Š”์ง€ ๊ณ„์† ํ™•์ธํ•˜๋ฉด์„œ ๋””์ž์ธํ•ฉ๋‹ˆ๋‹ค.

/* mouseType 01 */
.mouse__cursor {
    position: absolute;         /* ๊ณ ์ •ํ•ด๋‘๊ธฐ */
    left: 0;
    top: 0;
    width: 50px;                /* ์ปค์„œ ์˜์—ญ ํฌ๊ธฐ ์„ค์ • */
    height: 50px;
    border-radius: 50%;
    border: 3px solid #fff;   /* ํ…Œ๋‘๋ฆฌ ์„ค์ • */
    background-color: rgba(225, 225, 225, 0.6);
    /* user-select: ํ…์ŠคํŠธ๋ฅผ ์„ ํƒํ–ˆ์„ ๋•Œ์˜ ๋™์ž‘์„ ์„ค์ •ํ•˜๋Š” ์†์„ฑ -> auto ๋”๋ธ”ํด๋ฆญ ๋˜๋Š” ๋“œ๋ž˜๊ทธ๋กœ ์„ ํƒ๋จ, none ํด๋ฆญ ๋ฐ ๋“œ๋ž˜๊ทธ๋กœ ์„ ํƒ ์•ˆ๋จ, all ๋‹จ์ˆœ ํด๋ฆญ๋งŒ์œผ๋กœ ์„ ํƒ๋จ */
    user-select: none;
    /* pointer-events: ๊ทธ๋ž˜ํ”ฝ ์š”์†Œ๊ฐ€ ์–ด๋–ค ์ƒํ™ฉ์—์„œ ํฌ์ธํ„ฐ ์ด๋ฒคํŠธ์˜ ๋Œ€์ƒ์ด ๋  ์ˆ˜ ์žˆ๋Š”์ง€ ์ง€์ •ํ•จ */
    pointer-events: none;
    transition:
        background-color 0.3s,
        border-color 0.3s,
        transform 0.6s,
        border-radius 0.3s ;
    /* all: ๋ชจ๋“  ์†์„ฑ์— ์ ์šฉ, ์ด ์‹œ๊ฐ„: 1์ดˆ, ์‹œ์ž‘ ์‹œ๊ฐ„ ์—ฐ๊ธฐ: 0.1์ดˆ, ์ง„ํ–‰์†๋„: ease-in-out = cubic-bezier  */
}

.mouse__wrap {
    width: 100%;        /* 100vw๋ผ๊ณ  ์ ์œผ๋ฉด ์Šคํฌ๋กค๋ฐ”๊ฐ€ ์ƒ๊ธฐ๋ฏ€๋กœ, %๋กœ ์ž…๋ ฅํ•ด์คŒ */
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;     /* ์•„์ดํ…œ์ด ์„ธ๋กœ ๋ฐฉํ–ฅ์œผ๋กœ ํ๋ฆ„ */
    color: #fff;
    overflow: hidden;           /* ๋„˜์น˜๋Š” ๊ฒƒ ์•ˆ ๋ณด์ด๊ฒŒ ๊ฐ€๋ฆฌ๊ธฐ */
    cursor: none;
}

.mouse__wrap p {
    font-size: 1.5vw;
    line-height: 2;
    font-weight: 300;
}

/* ๋‘๋ฒˆ์งธ(๋งˆ์ง€๋ง‰) ์ค„์—๋งŒ ์ ์šฉ์‹œํ‚ค๊ธฐ ์œ„ํ•ด last-child ์‚ฌ์šฉ */
.mouse__wrap p:last-child {
    font-size: 2vw;
    font-weight: 400;
}

.mouse__wrap p span {
    border-bottom: 0.15vw dashed #fdf0ba;
    color: #fdf0ba;
}

[3] ์ •ํ•ด์ง„ ๋‹จ์–ด ์œ„์—์„œ ๋ณ€ํ˜•๋˜๋Š” ์ปค์„œ ํ˜•ํƒœ ๋””์ž์ธํ•˜๊ธฐ (css)

๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด์— ์–ด๋–ป๊ฒŒ ๋ณด์—ฌ์ง€๋Š”์ง€ ๊ณ„์† ํ™•์ธํ•˜๋ฉด์„œ ๋””์ž์ธํ•ฉ๋‹ˆ๋‹ค.

.mouse__cursor.style1 {
    background-color: #ff645088;
    border-color: #ff6450;
    transform: scale(3) rotateY(720deg);
    /* transform: rotate ํšŒ์ „, scale ํ™•๋Œ€ ์ถ•์†Œ, skew ๋น„ํ‹€๊ธฐ, translate ์ด๋™ ๋“ฑ์˜ ํ˜•ํƒœ ๋ณ€ํ˜•์„ ํ•˜๋Š” ์†์„ฑ */
}

.mouse__cursor.style2 {
    background-color: #b3ff5d81;
    border-color: #b3ff5d;
    transform: scale(0.5)
}

.mouse__cursor.style3 {
    background-color: #6250ff8f;
    border-color: #6250ff;
    transform: scale(4.5) rotateX(720deg);
}

.mouse__cursor.style4 {
    background-color: #7c24cf7e;
    border-color: #7c24cf;
    transform: scale(3) rotateX(720deg) rotateY(720deg);
}

.mouse__cursor.style5 {
    background-color: #fc2d7294;
    border-color: #fc2d72;
    border-radius: 20%;
    transform: scale(7);
}

.mouse__cursor.style6 {
    background-color: #ffe55079;
    border-color: #ffe550;
    border-radius: 5%;
    transform: scale(2) rotateX(1080deg) rotateY(75deg);
}

[4] mouse__info ํ™•์ธ์นธ ๋งŒ๋“ค๊ธฐ (html)

1) div ํƒœ๊ทธ ์•ˆ์— ๋ชฉ๋ก ul > li ํƒœ๊ทธ๋ฅผ ๋„ฃ๊ณ  ๋‚ด์šฉ์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
2) ๋‚˜์ค‘์— script๋ฅผ ํ†ตํ•ด ๋ณ€ํ™”ํ•  ๊ฐ’์ด ๋“ค์–ด๊ฐˆ ์ˆซ์ž๋Š” span ํƒœ๊ทธ๋กœ ๊ฐ์‹ธ์ค๋‹ˆ๋‹ค.
2) css ์™€ script ์˜ ๊ฒฝ๋กœ ์„ค์ •์„ ์œ„ํ•ด, id ์™€ class ๋กœ ์ด๋ฆ„์„ ์ •ํ•ฉ๋‹ˆ๋‹ค.

<aside id="parallax__info">
    <div class="scroll">scrollTop: <span>0</span>px</div>
    <div class="info">
        <ul>
            <li>( scrollTop - section$ ๋†’์ด๊ฐ’ ) * 0.07</li>
            <li>#(sT - s1) * 0.07 : <span class="offset1">0</span>px</li>
            <li>#(sT - s2) * 0.07 : <span class="offset2">0</span>px</li>
            <li>#(sT - s3) * 0.07 : <span class="offset3">0</span>px</li>
            <li>#(sT - s4) * 0.07 : <span class="offset4">0</span>px</li>
            <li>#(sT - s5) * 0.07 : <span class="offset5">0</span>px</li>
            <li>#(sT - s6) * 0.07 : <span class="offset6">0</span>px</li>
            <li>#(sT - s7) * 0.07 : <span class="offset7">0</span>px</li>
            <li>#(sT - s8) * 0.07 : <span class="offset8">0</span>px</li>
            <li>#(sT - s9) * 0.07 : <span class="offset9">0</span>px</li>
        </ul>
    </div>
</aside>
<!-- //parallax__info -->

[5] mouse__info ํ™•์ธ์นธ ๋””์ž์ธํ•˜๊ธฐ (css)

๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด์— ์–ด๋–ป๊ฒŒ ๋ณด์—ฌ์ง€๋Š”์ง€ ๊ณ„์† ํ™•์ธํ•˜๋ฉด์„œ ๋””์ž์ธํ•ฉ๋‹ˆ๋‹ค.

.mouse__info {
    position: absolute;
    left: 20px;
    bottom: 10px;
    font-size: 14px;
    line-height: 1.6;
    color: #fff;
}

[6] ๋ณ€ํ•˜๋Š” ๊ฐ’ ๊ณ„์‚ฐํ•˜์—ฌ ์ถœ๋ ฅํ•˜๊ธฐ (script)

1) ๊ฐ’์„ ์–ด๋–ป๊ฒŒ ๊ฐ€์ ธ์™€์•ผ ํ• ์ง€ ์ƒ๊ฐํ•ด๋ณด๊ณ , ๊ณ„์‚ฐ ๋ฐฉ๋ฒ•์„ ๊ตฌ์ƒํ•ด๋ด…๋‹ˆ๋‹ค.
2) ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•, ๊ณ„์‚ฐ ๋ฐฉ๋ฒ•์„ ๋‹ค์–‘ํ•˜๊ฒŒ ์ƒ๊ฐํ•ด๋ณด๊ณ  ์—ฌ๋Ÿฌ ๊ฐ€์ง€๋กœ ์ถœ๋ ฅํ•ด๋ด…์‹œ๋‹ค.

// mouse__info ๊ฐ’ ์ถœ๋ ฅํ•˜๊ธฐ

    window.addEventListener("mousemove", (event) => {                       // ๋งˆ์šฐ์Šค ์›€์ง์ž„์ด ์žˆ์„ ๋•Œ, (event ; ์›€์ง์ด๋Š” ๋งˆ์šฐ์Šค)๋Š”,
        document.querySelector(".clientX").innerText = event.clientX;       // ์š”์†Œ clientX ์•ˆ์— ์ถœ๋ ฅํ•˜์‹œ์˜ค = ์›€์ง์ด๋Š” ๋งˆ์šฐ์Šค์˜ X์ขŒํ‘œ๊ฐ’ (๋ธŒ๋ผ์šฐ์ € ๊ธฐ์ค€)
        document.querySelector(".clientY").innerText = event.clientY;       // ์š”์†Œ clientY ์•ˆ์— ์ถœ๋ ฅํ•˜์‹œ์˜ค = ์›€์ง์ด๋Š” ๋งˆ์šฐ์Šค์˜ Y์ขŒํ‘œ๊ฐ’ (๋ธŒ๋ผ์šฐ์ € ๊ธฐ์ค€)
        document.querySelector(".offsetX").innerText = event.offsetX;       // ์š”์†Œ offsetX ์•ˆ์— ์ถœ๋ ฅํ•˜์‹œ์˜ค = ์›€์ง์ด๋Š” ๋งˆ์šฐ์Šค์˜ X์ขŒํ‘œ๊ฐ’ (์š”์†Œ ๊ธฐ์ค€)
        document.querySelector(".offsetY").innerText = event.offsetY;       // ์š”์†Œ offsetY ์•ˆ์— ์ถœ๋ ฅํ•˜์‹œ์˜ค = ์›€์ง์ด๋Š” ๋งˆ์šฐ์Šค์˜ Y์ขŒํ‘œ๊ฐ’ (์š”์†Œ ๊ธฐ์ค€)
        document.querySelector(".pageX").innerText = event.pageX;           // ์š”์†Œ pageX ์•ˆ์— ์ถœ๋ ฅํ•˜์‹œ์˜ค = ์›€์ง์ด๋Š” ๋งˆ์šฐ์Šค์˜ X์ขŒํ‘œ๊ฐ’ (ํŽ˜์ด์ง€ ๊ธฐ์ค€)
        document.querySelector(".pageY").innerText = event.pageY;           // ์š”์†Œ pageY ์•ˆ์— ์ถœ๋ ฅํ•˜์‹œ์˜ค = ์›€์ง์ด๋Š” ๋งˆ์šฐ์Šค์˜ Y์ขŒํ‘œ๊ฐ’ (ํŽ˜์ด์ง€ ๊ธฐ์ค€)
        document.querySelector(".screenX").innerText = event.screenX;       // ์š”์†Œ screenX ์•ˆ์— ์ถœ๋ ฅํ•˜์‹œ์˜ค = ์›€์ง์ด๋Š” ๋งˆ์šฐ์Šค์˜ X์ขŒํ‘œ๊ฐ’ (๋””๋ฐ”์ด์Šค ๊ธฐ์ค€)
        document.querySelector(".screenY").innerText = event.screenY;       // ์š”์†Œ screenY ์•ˆ์— ์ถœ๋ ฅํ•˜์‹œ์˜ค = ์›€์ง์ด๋Š” ๋งˆ์šฐ์Šค์˜ Y์ขŒํ‘œ๊ฐ’ (๋””๋ฐ”์ด์Šค ๊ธฐ์ค€)
    });

    const cursor = document.querySelector(".mouse__cursor");        // ์ƒ์ˆ˜ cursot = mouse__cursor ๋ฅผ ์ €์žฅ

    // ๋™๊ทธ๋ผ๋ฏธ ๋ชจ์–‘์˜ ์ •๊ฐ€์šด๋ฐ์— ์ปค์„œ ํฌ์ธํŠธ ๋งž์ถฐ์ฃผ๊ธฐ
    window.addEventListener("mousemove", (e) => {       // event ๋ฅผ e ๋ผ๊ณ  ๋งŽ์ด ์”€
        cursor.style.left = e.clientX - 25 + "px";      // ๋‹จ์œ„๋ฅผ ์จ์ค˜์•ผ ํ•˜๋Š”๋ฐ, ์•ˆ ์จ์ฃผ๋ฉด ์›€์ง์ด์ง€๋„ ์•Š๊ณ  ๊ฒ€์‚ฌ์—์„œ๋„ ์—๋Ÿฌ๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š์Œ
        cursor.style.top = e.clientY - 25 + "px";
    });



    // ๋งˆ์šฐ์Šค ์ปค์„œ ์ƒ‰๊น” ๋ฐ”๊พธ๊ธฐ (css ์ ์šฉ์‹œํ‚ค๊ธฐ)  01. ์ผ์ผ์ด ํ•˜๋‚˜์”ฉ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฐฉ๋ฒ•

    // style$์— ๋Œ€ํ•˜์—ฌ ๋งˆ์šฐ์Šค์˜ ์›€์ง์ž„์ด ์žˆ์„ ๋•Œ, cursor์˜ ํด๋ž˜์Šค์— style$๋ฅผ ์ถ”๊ฐ€ํ•˜์‹œ์˜ค.
    // document.querySelector(".style1").addEventListener("mouseover", () => {
    //     cursor.classList.add("style1");
    // });
    // document.querySelector(".style1").addEventListener("mouseout", () => {
    //     cursor.classList.remove("style1");
    // });

    // document.querySelector(".style2").addEventListener("mouseover", () => {
    //     cursor.classList.add("style2");
    // });
    // document.querySelector(".style2").addEventListener("mouseout", () => {
    //     cursor.classList.remove("style2");
    // });

    // document.querySelector(".style3").addEventListener("mouseover", () => {
    //     cursor.classList.add("style3");
    // });
    // document.querySelector(".style3").addEventListener("mouseout", () => {
    //     cursor.classList.remove("style3");
    // });

    // document.querySelector(".style4").addEventListener("mouseover", () => {
    //     cursor.classList.add("style4");
    // });
    // document.querySelector(".style4").addEventListener("mouseout", () => {
    //     cursor.classList.remove("style4");
    // });

    // document.querySelector(".style5").addEventListener("mouseover", () => {
    //     cursor.classList.add("style5");
    // });
    // document.querySelector(".style5").addEventListener("mouseout", () => {
    //     cursor.classList.remove("style5");
    // });

    // document.querySelector(".style6").addEventListener("mouseover", () => {
    //     cursor.classList.add("style6");
    // });
    // document.querySelector(".style6").addEventListener("mouseout", () => {
    //     cursor.classList.remove("style6");
    // });


    // ๋งˆ์šฐ์Šค ์ปค์„œ ์ƒ‰๊น” ๋ฐ”๊พธ๊ธฐ (css ์ ์šฉ์‹œํ‚ค๊ธฐ)  02. for๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
    // for (let i = 1; i <= 6; i++) {
    //     document.querySelector(".style" + i).addEventListener("mouseover", () => {
    //         cursor.classList.add("style" + i);
    //     });
    //     document.querySelector(".style" + i).addEventListener("mouseout", () => {
    //         cursor.classList.remove("style" + i);
    //     });
    // };


    // ๋งˆ์šฐ์Šค ์ปค์„œ ์ƒ‰๊น” ๋ฐ”๊พธ๊ธฐ (css ์ ์šฉ์‹œํ‚ค๊ธฐ)  03. forEach๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• : mouse__wrap ์˜ span ๊ธธ์ด๊ฐ’ ํ™œ์šฉ
    // console.log(document.querySelectorAll(".mouse__wrap span").length);
    // document.querySelectorAll(".mouse__wrap span").forEach((span, num) => {
    //     span.addEventListener("mouseover", () => {
    //         cursor.classList.add("style" + (num +1));           // ๊ธธ์ด๊ฐ’์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— +1 ํ•ด์คŒ
    //     });
    //     span.addEventListener("mouseout", () => {
    //         cursor.classList.remove("style" + (num +1));
    //     });
    // });


    // ๋งˆ์šฐ์Šค ์ปค์„œ ์ƒ‰๊น” ๋ฐ”๊พธ๊ธฐ (css ์ ์šฉ์‹œํ‚ค๊ธฐ)  04. getAttribute() : ํ•ด๋‹น ์š”์†Œ์— ์ง€์ •๋œ ๊ฐ’์„ ๋ฐ˜ํ™˜
    document.querySelectorAll(".mouse__wrap span").forEach(span => {
        let attr = span.getAttribute("class");          // ๋ณ€์ˆ˜ attr ์— span์˜ ์†์„ฑ class ๋ฅผ ์ €์žฅ

        span.addEventListener("mouseover", () => {
            cursor.classList.add(attr);
        });
        span.addEventListener("mouseout", () => {
            cursor.classList.remove(attr);
        });
    });


    // num.forEach(el => {
    //     document.querySelector(".style" + el).addEventListener("mouseover", () => {
    //         cursor.classList.add("style" + el);
    //     });
    //     document.querySelector(".style" + el).addEventListener("mouseout", () => {
    //         cursor.classList.remove("style" + el);
    //     });
    // });

03. ๊ฒฐ๊ณผ๋ณด๊ธฐ

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€