๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
EFFECT

[EFFECT] ๋งˆ์šฐ์Šค ํšจ๊ณผ 01 : ์ปค์„œ ๋”ฐ๋ผ๋‹ค๋‹ˆ๊ธฐ

by _ํ† ๋งคํ†  2022. 9. 6.
728x90

๐Ÿ–ฑ ๋งˆ์šฐ์Šค ํšจ๊ณผ : ๋งˆ์šฐ์Šค ํฌ์ธํ„ฐ ๋”ฐ๋ผ๋‹ค๋‹ˆ๊ธฐ ๐Ÿ–ฑ

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๋งˆ์šฐ์Šค ์ปค์„œ๋ฅผ ์›€์ง์ž„์— ๋”ฐ๋ผ ํ•ด๋‹น ์˜์—ญ์— ์ปค์„œ๊ฐ€ ๋‹ฟ์•˜์„ ๋•Œ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ํšจ๊ณผ๋ฅผ ์‹ค์Šตํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค ๐Ÿ˜†


#1. HTML / CSS ์„ค์ •ํ•˜๊ธฐ

! HTML TIP !
ํŽ˜์ด์ง€ ์„น์…˜๊ณผ ๋งˆ์šฐ์Šค ์ธํฌ(์ปค์„œ ์œ„์น˜๊ฐ’ ๋ฐ•์Šค)๋ฅผ ๊ฐ๊ฐ ๋‚˜๋ˆ„์–ด ๊ตฌ์กฐ์— ๋งž๊ฒŒ ๋ ˆ์ด์•„์›ƒ์„ ์ง  ๋’ค ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์œ„ํ•œ ์„ ํƒ์ž๋ฅผ ๊ฐ๊ฐ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค.
๋˜ํ•œ ํ…์ŠคํŠธ์—์„œ ์Šคํƒ€์ผ ๋ณ€๊ฒฝ ๋ฐ ์ด๋ฒคํŠธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ  ์‹ถ์€ ๊ณณ์— ๊ฐ๊ฐ ์„ ํƒ์ž๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.

โœ๏ธ HTML ์Šคํฌ๋ฆฝํŠธ ๋ณด๊ธฐ
<section id="mouseType01">
    <div class="mouse__cursor"></div>
    <div class="mouse__wrap">
        <p>The <span class="style1">future</span> depends on <span class="style2">what we do</span> in the <span class="style3">present</span></p>
        <p><span class="style4">๋ฏธ๋ž˜</span>๋Š” <span class="style5">ํ˜„์žฌ</span> ์šฐ๋ฆฌ๊ฐ€ <span class="style6">๋ฌด์—‡์„ ํ•˜๋Š”๊ฐ€</span>์— ๋‹ฌ๋ ค ์žˆ๋‹ค.</p>
    </div>
</section>

<div class="mouse__info">
    <ul>
        <li>clientX : <span class="clientX">0</span>px</li>
        <li>clientY : <span class="clientY">0</span>px</li>
        <li>offsetX: <span class="offsetX">0</span>px</li>
        <li>offsetY: <span class="offsetY">0</span>px</li>
        <li>pageX: <span class="pageX">0</span>px</li>
        <li>pageY: <span class="pageY">0</span>px</li>
        <li>screenX: <span class="screenX">0</span>px</li>
        <li>screenY: <span class="screenY">0</span>px</li>
    </ul>
</div>

! CSS TIP !
1. ์ปค์„œ ์˜์—ญ ์„ค์ •๊ณผ ์ขŒํ‘œ๊ฐ’ ๋ฐ•์Šค๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค
2. ๊ทธ ๋’ค ํ…์ŠคํŠธ์—์„œ ์ปค์„œ๊ฐ€ ๋‹ฟ์•˜์„ ๋•Œ ์ด๋ฒคํŠธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ  ์‹ถ์€ ๊ณณ์— ์• ๋‹ˆ๋ฉ”์ด์…˜ ์†์„ฑ์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค.

โœ๏ธ CSS ์Šคํฌ๋ฆฝํŠธ ๋ณด๊ธฐ : ๋งˆ์šฐ์Šค ํฌ์ธํ„ฐ / ์œ„์น˜(์ขŒํ‘œ)๋ฐ•์Šค ์˜์—ญ
/* mouseType */
.mouse__wrap {
    width: 100%;
    height: 100vh;  /* vw๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์Šคํฌ๋กค์ด ์ƒ๊ธฐ๋ฏ€๋กœvh๋กœ ์ž‘์—… */
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    color: #fff;
    overflow: hidden;
    cursor: none; /* ์ปค์„œ๋ฅผ ํ•ด๋‹น ์˜์—ญ์—์„œ ์•ˆ๋ณด์ด๊ฒŒ ํ•œ๋‹ค. */
}
.mouse__wrap p {
    font-size: 2vw;
    line-height: 2;
    font-weight: 200;
}
.mouse__wrap p:last-child {
    font-size: 3vw;
    font-weight: 300;
}
.mouse__wrap p span {
    padding-bottom: 3px;
    border-bottom: 0.1vw solid #e7ad00;
    color: #e7ad00;
}

@media (max-width: 800px) {
    .mouse__wrap p {
        padding: 0 20px;
        font-size: 24px;
        line-height: 1.5;
        text-align: center;
        margin-bottom: 10px;
    }
    .mouse__wrap p:last-child {
        font-size: 40px;
        line-height: 1.5;
        text-align: center;
        word-break: keep-all;
    }
}

        /* mouse__cursor */
.mouse__cursor {
    position: absolute;
    left: 0;
    top: 0;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    border: 1px solid #fff;
    background-color: rgba(255,255,255,0.1);
    user-select: none; /* ์ปค์„œ ์ด๋ฒคํŠธ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ฒŒ ํ•จ. (์„ ํƒ๋˜์ง€ ์•Š๊ฒŒ ํ•จ.) */
    pointer-events: none;
    /* perspective: 400px;
    transform-style: preserve-3d; */
    transition: background-color 0.3s, border-color 0.3s, transform 0.6s, border-radius 0.3s;
}
.mouse__cursor.style1 {
    background-color: rgba(77, 49, 18, 0.4);
    border-color: #e7ad00;
    border-radius: 0;
}
.mouse__cursor.style2 {
    background-color: rgba(24, 58, 78, 0.4);
    border-color: #008be7;
    transform: scale(2) rotateY(720deg);
}
.mouse__cursor.style3 {
    background-color: rgba(77, 49, 18, 0.4);
    border-color: #e7ad00;
    transform: scale(5) skew(10deg);
}
.mouse__cursor.style4 {
    background-color: rgba(53, 23, 65, 0.4);
    border-color: #ad66ba;
    transform: scale(10);
}
.mouse__cursor.style5 {
    background-color: rgba(24, 58, 78, 0.4);
    border-color: #008be7;
    transform: scale(1);
}
.mouse__cursor.style6 {
    background-color: rgba(145, 46, 86, 0.4);
    border-color: #f586af;
    border-radius: 0;
    transform: scale(3) rotateX(545deg) skew(10deg);
}

/*  .mouse__info */
.mouse__info {
    position: absolute;
    left: 20px;
    bottom: 10px;
    font-size: 14px;
    line-height: 1.6;
    color: #fff;
}
โœ๏ธ CSS ์Šคํฌ๋ฆฝํŠธ ๋ณด๊ธฐ : ํŽ˜์ด์ง€ ์„น์…˜
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
  }
  
  body {
    width: 100%; /* 100%ํ•˜๋ฉด ์Šคํฌ๋กค๋ฐ”๊ฐ€ ์ƒ๊ธฐ์ง€ ์•Š๋Š”๋‹ค */
    height: 100vh;
    font-family: "Pretendard";
    overflow: hidden; /* ์˜์—ญ์„ ๋ฒ—์–ด๋‚˜๋ฉด ์Šคํฌ๋กค ์ƒ๊ธฐ์ง€ ์•Š๊ฒŒ ์•ˆ๋ณด์ด๊ฒŒ ํ•˜๊ธฐ */
    background: url(../img/effect_bg04-2.jpg) no-repeat center / cover;
  }
  
  /* ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€ */
  body.img01 {
    background: url(../img/effect_bg01-2.jpg) no-repeat center / cover;
  }
  body.img02 {
    background: url(../img/effect_bg02-2.jpg) no-repeat center / cover;
  }
  body.img03 {
    background: url(../img/effect_bg03-2.jpg) no-repeat center / cover;
  }
  body.img04 {
    background: url(../img/effect_bg04-2.jpg) no-repeat center / cover;
  }
  body.img05 {
    background: url(../img/effect_bg05-2.jpg) no-repeat center / cover;
  }
  body.img06 {
    background: url(../img/effect_bg06-2.jpg) no-repeat center / cover;
  }
  body.img07 {
    background: url(../img/effect_bg07-2.jpg) no-repeat center / cover;
  }
  body.img08 {
    background: url(../img/effect_bg08-2.jpg) no-repeat center / cover;
  }
  body.img09 {
    background: url(../img/effect_bg09-2.jpg) no-repeat center / cover;
  }
  body.img10 {
    background: url(../img/effect_bg10-2.jpg) no-repeat center / cover;
  }
  
  body::after {
    /* ๊ฐ€์ƒ ์š”์†Œ : ๋ฐฐ๊ฒฝ ์ „์ฒด ๋ถˆํˆฌ๋ช…๋„ ์”Œ์šฐ๊ธฐ */
    content: "";
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100vh;
    background: rgba(23, 20, 88, 0.500);
    z-index: -1;
  }
  
  img {
    width: 100%;
    vertical-align: top; /* ์ด๋ฏธ์ง€ ์ •๋ ฌ */
  }
  
  a {
    color: #fff;
    text-decoration: none;
  }
  
  li {
    list-style: none;
  }
  
  /* scroll */
  ::-webkit-scrollbar {
    width: 6px;
    height: 6px;
  }
  ::-webkit-scrollbar-track {
    background: #28293600;
  }
  ::-webkit-scrollbar-thumb {
    background: rgba(136, 136, 136, 0.500);
    border-radius: 20px;
  }
  
  /* layout */
  #header {
    z-index: 2000; /* ์ œํŠธ ์ธ๋ฑ์Šค๋Š” ์„œ๋กœ ํ˜•์ œ์—ฌ์•ผ ํ•˜๋ฉฐ ๊ผญ ํฌ์ง€์…˜์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด์ฃผ์–ด์•ผ ํ•œ๋””. */
  }
  #main {
    width: 100%;
    height: 100vh;
    position: relative;
    z-index: 1000;
  }
  #footer {
    position: relative;
    z-index: 3000;
  }
  
  /* header */
  #header {
    position: absolute;
    left: 0;
    top: 0;
    color: #fff;
    padding: 20px;
  }
  #header h1 {
    display: inline-block;
    font-weight: 200;
    margin-bottom: 10px;
    border-bottom: 0.5px solid #fff;
  }
  #header p {
    font-weight: 200;
    font-size: 18px;
    margin-bottom: 10px;
  }
  #header li {
    display: inline-block;
  }
  #header li a {
    border: 1px solid #fff;
    border-radius: 50%;
    display: inline-block;
    width: 30px;
    height: 30px;
    text-align: center;
    line-height: 30px;
    font-size: 14px;
  }
  #header li.active a {
    background-color: #fff;
    color: #282936;
  }

#2. JAVASCRIPT : ์Šคํฌ๋ฆฝํŠธ ์งœ๊ธฐ

์ด๋ฒคํŠธ ๋ฉ”์„œ๋“œ์ธ mouseover()๊ณผ mouseout() ๊ทธ๋ฆฌ๊ณ  ์œ„์น˜ ์†์„ฑ๋“ค์„ ํ†ตํ•ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์™„์„ฑํ•ด๋ณด์ž!

๐Ÿ’ก ์ „์ฒด ์Šคํฌ๋ฆฝํŠธ ๋ณด๊ธฐ
//์ปค์„œ ์œ„์น˜ ์ขŒํ‘œ ๊ฐ’
window.addEventListener("mousemove", (event) => {
    document.querySelector(".clientX").innerText = event.clientX; //๋ธŒ๋ผ์šฐ์ € ๊ธฐ์ค€
    document.querySelector(".clientY").innerText = event.clientY;
    document.querySelector(".offsetX").innerText = event.offsetX; //์š”์†Œ ๊ธฐ์ค€
    document.querySelector(".offsetY").innerText = event.offsetY;
    document.querySelector(".pageX").innerText = event.pageX; //๋ฌธ์„œ ๊ธฐ์ค€
    document.querySelector(".pageY").innerText = event.pageY;
    document.querySelector(".screenX").innerText = event.screenX; //๋””๋ฐ”์ด์Šค ๊ธฐ์ค€
    document.querySelector(".screenY").innerText = event.screenY;
});

const cursor = document.querySelector(".mouse__cursor"); //์ง€์—ญ ๋ณ€์ˆ˜(ํ•จ์ˆ˜์•ˆ)๋Š” ์ด๋ฒคํŠธ๊ฐ€ ์ž‘๋™ X ์ „์—ญ๋ณ€์ˆ˜๋กœ ํ•ด์•ผ ์ด๋ฒคํŠธ๊ฐ€ ์ž‘๋™๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ „์—ญ ๋ณ€์ˆ˜๋กœ ์ž‘์—….

//์ปค์„œ ์˜์—ญ ์ •ํ•˜๊ธฐ
window.addEventListener("mousemove", (e) => {

    cursor.style.left = e.clientX -25 + "px";
    cursor.style.top = e.clientY -25  + "px";
});

//์ปค์„œ ์ด๋ฒคํŠธ ์ž‘๋™์‹œํ‚ค๊ธฐ - ๊ฐ๊ฐ ์ถœ๋ ฅ๋ฌธ์œผ๋กœ ์Šคํƒ€์ผ ์ ์šฉ - ๋น„ํšจ์œจ์ 
// 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");
// });

// for๋ฌธ : ๋งˆ์šฐ์Šค ์ปค์„œ ์ด๋ฒคํŠธ ์ž‘๋™์‹œํ‚ค๊ธฐ
//for๋ฌธ ์‹ ์“ฐ๊ณ  ๋ฐ˜๋ณต๋˜๋Š” ์‹คํ–‰๋ฌธ ์“ฐ๊ธฐ - ๊ทธ ๋’ค ์ˆซ์ž ์ง€์šฐ๊ณ  i๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ 
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);
    });
}

// forEach() : ๋งˆ์šฐ์Šค ์ปค์„œ ์ด๋ฒคํŠธ ์ž‘๋™์‹œํ‚ค๊ธฐ
num = [1,2,3,4,5,6];
    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);
        });
    });

// forEach() : ๋งˆ์šฐ์Šค ์ปค์„œ ์ด๋ฒคํŠธ ์ž‘๋™์‹œํ‚ค๊ธฐ
document.querySelectorAll(".mouse__wrap span").forEach((span, num) => {
    span.addEventListener("mouseover", () => {
        cursor.classList.add("style" + (num+1));
    });
    span.addEventListener("mouseout", () => {
        cursor.classList.remove("style" + (num+1));
    });
});

// getAttribute : ๋งˆ์šฐ์Šค ์ปค์„œ ์ด๋ฒคํŠธ ์ž‘๋™์‹œํ‚ค๊ธฐ
    document.querySelectorAll('.mouse__wrap span').forEach((span) => {
        const attr = span.getAttribute('class');
        span.addEventListener("mouseover", () => {
            const cursor = document.querySelector('.mouse__cursor');
            cursor.classList.add(attr);
        });
        span.addEventListener("mouseout", () => {
            const cursor = document.querySelector('.mouse__cursor');
            cursor.classList.remove(attr);
        });
    });

โœ๏ธ ์Šคํฌ๋ฆฝํŠธ ๋ณด๊ธฐ : ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

! JAVASCRIPT TIP !
1. ์œ„์น˜(์ขŒํ‘œ)๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด innerText๋ฅผ ํ†ตํ•ด ์ด๋ฒคํŠธ ์œ„์น˜ ์†์„ฑ์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค.
2. ์ปค์„œ๋ฅผ ์ปค์„œ ์˜์—ญ์˜ ์ค‘์‹ฌ์— ๋งž์ถ”๊ธฐ ์œ„ํ•ด ๋ธŒ๋ผ์šฐ์ € ๊ธฐ์ค€์˜ ์œ„์น˜ ์†์„ฑ์ธ clientX/Y๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
3. ์ด ๋•Œ ๋ฐ˜๋“œ์‹œ ๋ฌธ์ž์—ด์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๊ทธ ๊ฐ’์˜ ๋‹จ์œ„๋ฅผ ์„ค์ •ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค!(+ "px")
4. getAttribute ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ ๋ฉ”์„œ๋“œ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์‹คํ–‰์‹œํ‚ต๋‹ˆ๋‹ค.
5. ๊ทธ ์™ธ์—๋Š” ์•„๋ž˜์˜ ์ฃผ์„์„ ์ฐธ๊ณ ํ•˜์—ฌ ๋” ์ž์„ธํ•˜๊ฒŒ ์•Œ์•„๋ด…์‹œ๋‹ค!

//์ปค์„œ ์œ„์น˜ ์ขŒํ‘œ ๊ฐ’
window.addEventListener("mousemove", (event) => {
    document.querySelector(".clientX").innerText = event.clientX; //๋ธŒ๋ผ์šฐ์ € ๊ธฐ์ค€
    document.querySelector(".clientY").innerText = event.clientY;
    document.querySelector(".offsetX").innerText = event.offsetX; //์š”์†Œ ๊ธฐ์ค€
    document.querySelector(".offsetY").innerText = event.offsetY;
    document.querySelector(".pageX").innerText = event.pageX; //๋ฌธ์„œ ๊ธฐ์ค€
    document.querySelector(".pageY").innerText = event.pageY;
    document.querySelector(".screenX").innerText = event.screenX; //๋””๋ฐ”์ด์Šค ๊ธฐ์ค€
    document.querySelector(".screenY").innerText = event.screenY;
});

const cursor = document.querySelector(".mouse__cursor"); //์ง€์—ญ ๋ณ€์ˆ˜(ํ•จ์ˆ˜์•ˆ)๋Š” ์ด๋ฒคํŠธ๊ฐ€ ์ž‘๋™ X ์ „์—ญ๋ณ€์ˆ˜๋กœ ํ•ด์•ผ ์ด๋ฒคํŠธ๊ฐ€ ์ž‘๋™๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ „์—ญ ๋ณ€์ˆ˜๋กœ ์ž‘์—…

//์ปค์„œ ์˜์—ญ ์ •ํ•˜๊ธฐ
window.addEventListener("mousemove", (e) => {

    cursor.style.left = e.clientX -25 + "px";
    cursor.style.top = e.clientY -25  + "px";
});

// getAttribute : ๋งˆ์šฐ์Šค ์ปค์„œ ์ด๋ฒคํŠธ ์ž‘๋™์‹œํ‚ค๊ธฐ
document.querySelectorAll('.mouse__wrap span').forEach((span) => {
    const attr = span.getAttribute('class');
    span.addEventListener("mouseover", () => {
        const cursor = document.querySelector('.mouse__cursor');
        cursor.classList.add(attr);
    });
    span.addEventListener("mouseout", () => {
        const cursor = document.querySelector('.mouse__cursor');
        cursor.classList.remove(attr);
    });
});

#3. ๊ฒฐ๊ณผ ๋ณด๊ธฐ ๐ŸŽ‰

728x90

๋Œ“๊ธ€


Lucky Charms Rainbow
js
html
css