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

[EFFECT] ๋งˆ์šฐ์Šค ํšจ๊ณผ 03 : ์กฐ๋ช… ํšจ๊ณผ

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

๐Ÿ–ฑ ๋งˆ์šฐ์Šค ํšจ๊ณผ : ์กฐ๋ช…ํšจ๊ณผ ๐Ÿ–ฑ

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ์ปค์„œ์—๊ฒŒ ์กฐ๋ช…ํšจ๊ณผ๋ฅผ ์ฃผ๋„๋ก ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜†
๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๋ฅผ ์–ด๋‘ก๊ฒŒํ•œ ์ƒํƒœ์—์„œ ์ปค์„œ ์˜์—ญ์— ๋˜‘๊ฐ™์€ ์ด๋ฏธ์ง€๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๊ทธ ๋ถ€๋ถ„๋งŒ ๋ฐ์•„ ๋ณด์—ฌ ๋งˆ์ง€ ์กฐ๋ช…์„ ๋น„์ถ˜ ๋“ฏ ํ•œ ํšจ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค~~ ์‹ ๊ฐ€ํ•˜์ฃ ~?!
๊ทธ๋Ÿผ ํ•จ๊ป˜ ์‹ค์Šตํ•ด๋ด…์‹œ๋‹ค~!


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

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

โœ๏ธ HTML ์Šคํฌ๋ฆฝํŠธ ๋ณด๊ธฐ
<section id="mouseType03">
    <div class="mouse__cursor"></div>
    <div class="mouse__wrap">
        <p><span class="style1">Energy</span> and <span>persistence</span> conquer all things</p>
        <p><span>๊ธฐ์šด</span>๊ณผ <span>๋ˆ๊ธฐ</span>๋Š” ๋ชจ๋“  ๊ฒƒ์„ ์ด๊ฒจ๋‚ธ๋‹ค.</p>
    </div>
</section>  

! CSS TIP !
position: absolute; : ๊ธฐ์กด์˜ ๋งˆ์šฐ์Šค ์ปค์„œ๋ฅผ ๋”ฐ๋ผ๋‹ค๋‹ˆ๋„๋ก ํฌ์ง€์…˜์„ ์ •ํ•ด์ค๋‹ˆ๋‹ค. (์Šคํฌ๋ฆฝํŠธ์™€ GSAP์„ ํ†ตํ•ด left์™€ top๊ฐ’์ด ์ด๋™ํ•  ๋•Œ ๋งˆ๋‹ค ๋ณ€ํ•˜๋„๋ก ์„ค์ •ํ•ด์ค„ ๊ฒƒ์ด๋ฏ€๋กœ ์™ผ์ชฝ ์ƒ๋‹จ ๋ชจ์„œ๋ฆฌ์— ์ž ์‹œ ์œ„์น˜์‹œ์ผœ ์ค๋‹ˆ๋‹ค.)
background-attachment: fixed; : ์ด๋ฏธ์ง€ ๊ณ ์ •(๋ธŒ๋ผ์šฐ์ € ์ฐฝ์˜ ์ƒ๋Œ€์  ์œ„์น˜)์œผ๋กœ ์Šคํฌ๋กค์„ ๋‚ด๋ ค๋„ ์ด๋ฏธ์ง€๊ฐ€ ์˜ฌ๋ผ๊ฐ€์ง€ ์•Š์Šต๋‹ˆ๋‹ค!

โœ๏ธ 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: 200px;
    height: 200px;
    z-index: -1;
    border-radius: 50%;
    background-image: url(../assets/img/city_bg05-2.jpg);
    background-size: cover;
    background-position: center;
    background-attachment: fixed;
    border: 5px solid #fff;
}


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

์˜ค๋Š˜์˜ ํฌ์ธํŠธ! getBoundingClientRect()๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ปค์„œ์˜ ์œ„์น˜๊ฐ’๊ณผ ํฌ๊ธฐ๊ฐ’์„ ๋ชจ๋‘ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ๊ทธ ๋’ค ์ปค์„œ์˜ ๊ฐ€๋กœ, ์„ธ๋กœ๊ธธ์ด๋ฅผ ๋ถˆ๋Ÿฌ์˜จ ๋’ค ์ปค์„œ๋ฅผ ์˜์—ญ ๊ฐ€์šด๋ฐ๋กœ ๋งž์ถฐ์ค๋‹ˆ๋‹ค!
์˜ค๋Š˜ ์Šคํฌ๋ฆฝํŠธ๋Š” ๊ฐ„๋‹จํ•œ ํŽธ์ด๋‹ˆ ๋จผ์ € ์ฃผ์„๊ณผ ํ•จ๊ป˜ ํ๋ฆ„์„ ์ดํ•ด ํ•œ๋’ค ์ •๋ฆฌ๋œ ์ „์ฒด ์Šคํฌ๋ฆฝํŠธ๋กœ ๋‹ค์‹œ ํ•œ ๋ฒˆ ๋ณต์Šตํ•ด๋ด…์‹œ๋‹ค~!

๐Ÿ’ก ์ „์ฒด ์Šคํฌ๋ฆฝํŠธ ๋ณด๊ธฐ
//์„ ํƒ์ž
const cursor = document.querySelector(".mouse__cursor"); // ์ปค์„œ

// const circleW = cursor.offsetWidth; //200 : ๋ณด๋” ๊ฐ’ ํฌํ•จ
// const circleH = cursor.offsetHdight; //200 : ๋ณด๋” ๊ฐ’ ํฌํ•จ
// const circle2 = cursor.clientWidth; //190 : ๋ณด๋” ๊ฐ’ ์ œ์™ธ

const circle = cursor.getBoundingClientRect(); //์ปค์„œ์˜ ๋ชจ๋“  ์œ„์น˜๊ฐ’
//getBoundingClientRect() ๋ฉ”์„œ๋“œ๊ฐ€ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์œ„์น˜๊ฐ’๋“ค (์ปค์„œ์˜ ์˜์—ญ ํฌ๊ธฐ == 200*200)
// const DOMRect = {
//     x: 0,
//     y: 0,
//     bottom: 200,
//     height: 200,
//     lift: 0,
//     right: 200,
//     top: 0,
//     width: 200
// }

//๋งˆ์šฐ์Šค๊ฐ€ ์›€์ง์ผ ๋•Œ ์ปค์„œ์˜ ์ด๋ฒคํŠธ์„ค์ •
//GSAP์œผ๋กœ ์„ค์ • : ์ง€์†์‹œ๊ฐ„ / ์ปค์„œ์˜ ์™ผ์ชฝ ๊ธฐ์ค€ ์ปค์„œ์˜ ์œ„์ชฝ ๊ธฐ์ค€์œผ๋กœ ์ปค์„œ์˜ ์œ„์น˜๊ฐ’ ์ •ํ•˜๊ธฐ
window.addEventListener("mousemove", (e) => {
    gsap.to(cursor, {
        duration: 0.3, 
        //์ปค์„œ๊ฐ€ ์ปค์„œ ์˜์—ญ ๊ฐ€์šด๋ฐ๋กœ ์˜ค๋„๋ก ๋ฐ˜์„ ๋‚˜๋ˆ„์–ด ์ค€๋‹ค.
        left: e.pageX - circle.width/2,
        top: e.pageY - circle.height/2,
    });            
});
//01 ์„ ํƒ์ž
const cursor = document.querySelector(".mouse__cursor"); // ์ปค์„œ
const circle = cursor.getBoundingClientRect(); //์ปค์„œ์˜ ๋ชจ๋“  ์œ„์น˜๊ฐ’

//02 ๋งˆ์šฐ์Šค๊ฐ€ ์›€์ง์ผ ๋•Œ ์ปค์„œ์˜ ์ด๋ฒคํŠธ์„ค์ • : GSAP
window.addEventListener("mousemove", (e) => {
    gsap.to(cursor, {
        duration: 0.3, 
        left: e.pageX - circle.width/2,
        top: e.pageY - circle.height/2,
    });            
});

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

! JAVASCRIPT TIP !
1. ๋จผ์ € ์ปค์„œ ์„ ํƒ์ž๋ฅผ ๋งŒ๋“ค์–ด์ค€ ๋’ค ์ปค์„œ์˜ ์œ„์น˜๊ฐ’์„ ๋ชจ๋‘ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ์š”์†Œ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ์ธ getBoundingClientRect()๋ฅผ ์ด์šฉํ•ด์ค๋‹ˆ๋‹ค.
2. ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ์˜ ๋ฉ”์„œ๋“œ ์ค‘ mousemove๋ฅผ ํ†ตํ•ด ๋งˆ์šฐ์Šค๊ฐ€ ์›€์ง์ผ ๋•Œ ์‹คํ–‰๋˜๋Š” ์š”์†Œ(์ปค์„œ) ์ด๋ฒคํŠธ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
3. GSAP์„ ํ†ตํ•ด ์ปค์„œ๋ฅผ ์ปค์„œ ์˜์—ญ ๊ฐ€์šด๋ฐ๋กœ ๋งž์ถฐ์ค€ ๋’ค ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ์ž‘๋™๋˜๋„๋ก ์ง€์†์‹œ๊ฐ„์„ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.


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

728x90

๋Œ“๊ธ€


Lucky Charms Rainbow
js
html
css