λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
CSS

CSS μ• λ‹ˆλ©”μ΄μ…˜ λ§Œλ“€κΈ° : 마우슀λ₯Ό λ”°λΌκ°€λŠ” λˆˆμ•Œ

by μ½”νŒŒμΉ΄ 2022. 9. 26.
728x90

CSS μ• λ‹ˆλ©”μ΄μ…˜ : 마우슀λ₯Ό λ”°λΌκ°€λŠ” λˆˆμ•Œ

μ˜€λŠ˜μ€ 마우슀λ₯Ό 따라 λˆˆμ•Œμ΄ λΉ™κΈ€λΉ™κΈ€ λŒμ•„κ°€λŠ” μ• λ‹ˆλ©”μ΄μ…˜μ„ λ§Œλ“€μ–΄ 보도둝 ν•˜κ² μŠ΅λ‹ˆλ‹€. μž‘μ—…ν•΄λ³Ό μ• λ‹ˆλ©”μ΄μ…˜μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

See the Pen eye by mj0614k (@mj0614k) on CodePen.


1. HTML μ†ŒμŠ€

<div class="wrapper">
    <div class="card">
        <div class="me">
            <div class="eye"></div>
        </div>
        <div class="text">EYE</div>
    </div>
</div>

wrapper μ•ˆμ— card, card μ•ˆμ— me와 text, me μ•ˆμ— eye 클래슀λ₯Ό κ°€μ§„ div λ°•μŠ€λ₯Ό λ§Œλ“€μ–΄ μ€λ‹ˆλ‹€. textλŠ” 자유둭게 μž‘μ„±ν•©λ‹ˆλ‹€.

2. CSS μ†ŒμŠ€

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html, body {
    height: 100%;
    overflow: hidden;
}

.wrapper {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 369px;
    height: 547px;
    transform-style: preserve-3d;
    transform: translate(-50%, -50%);
}

.me {
    position: absolute;
    width: 369px;
    height: 547px;
    transform: translatez(80px) scale(0.8);
}

.me::before, .me::after {
    position: absolute;
    content: '';
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    border-radius: 10px;
    background: url(https://slack-imgs.com/?c=1&o1=ro&url=http%3A%2F%2Fwww.supah.it%2Fdribbble%2F006%2Fprofile.png) no-repeat 0 0;
}

.me::after {
    height: 30px;
    top: auto;
    bottom: -15px;
    filter: blur(15px);
    background-size: 100% 30px;
    border-radius: 100px;
}

.eye, .eye::before {
    width: 70px;
    height: 70px;
    position: absolute;
    top: 175px;
    left: 119px;
    z-index: -1;
    background: url(http://www.supah.it/dribbble/006/eye.png);
}

.eye::before {
    content: '';
    top: -3px;
    left: 99px;
}

.text {
    position: absolute;
    left: 0;
    top: 0;
    z-index: 2;
    width: 390px;
    height: 595px;
    transform: translatez(150px) translatex(-11px) translatey(-26px) scale(0.55);
    border: 3px solid #fff;
    text-align: center;
    font-size: 35px;
    line-height: 1000px;
    color: #fff;
    opacity: 0.3;
    border-radius: 15px;
    background: linear-gradient(135deg, #fff 0%, rgba(255,255,255,0.36) 39%, rgba(255,255,255,0) 51%, #000 100%)
}

.text::after {
    content: 'moving';
    width: 100%;
    position: absolute;
    bottom: 22px;
    left: 0;
    z-index: 1;
    line-height: 1;
    font-size: 24px;
    text-align: center;
    text-transform: uppercase;
    letter-spacing: 29px;
    text-indent: 20px;
}

μ΄λ―Έμ§€μ˜ ν”„λ ˆμž„μ„ λ§Œλ“€κ³ , 각각의 이미지 μœ„μΉ˜λ₯Ό μ‘°μ •ν•΄ μ€λ‹ˆλ‹€.

3. JAVASCRIPT μ†ŒμŠ€

const wrap = document.querySelector('.wrapper');
const eye = document.querySelector('.eye');
const speed = 1;
let x = 0;
let y = 0;
let followX = 0;
let followY = 0;

function animate() {
  x += (followX - x) * speed;
  y += (followY - y) * speed;
  eye.style.transform = `translate(${-x}px, ${-y}px)`;
  wrap.style.transform = `translate(-50%, -50%) perspective(600px) rotateX(${-y}deg) rotateY(${-x}deg)`;
  requestAnimationFrame(animate);
}

window.addEventListener('mousemove', (e) => {
  let mouseX = Math.max(-100, Math.min(100,window.innerWidth / 2 - e.clientX));
  let mouseY = Math.max(-100, Math.min(100,window.innerHeight / 2 - e.clientY));
  
  followX = (12 * mouseX) / 100;
  followY = (10 * mouseY) / 100;
});

window.addEventListener('keydown', (e) => {
  switch(e.keyCode) {
    case 37: followX = 12; break; //쒌
    case 38: followY = 10; break; //상
    case 39: followX = -12; break; //우
    case 40: followY = -10; break; //ν•˜
    default: break;
  }
})

animate();

keydown μ΄λ²€νŠΈλŠ” λ°©ν–₯ν‚€λ₯Ό λˆŒλ €μ„ λ•Œ λˆˆμ•Œμ΄ ν•΄λ‹Ή λ°©ν–₯을 바라보도둝 μž‘μ—…ν•΄ μ£Όμ—ˆμŠ΅λ‹ˆλ‹€.

μœ„μ˜ HTML, CSS, JAVASCRIPT μ†ŒμŠ€λ₯Ό ν•©μΉ˜λ©΄ μ• λ‹ˆλ©”μ΄μ…˜μ΄ μ™„μ„±λ©λ‹ˆλ‹€. μ†”μ§νžˆ μ§•κ·ΈλŸ½λ„€μš”...

728x90

λŒ“κΈ€

κ°μ‚¬ν•©λ‹ˆλ‹€. πŸ¦™

CSS
κ΄‘κ³  쀀비쀑