javascript/javascript30
[javscript30]CSS Text Shadow Mouse Move Effect
소영
2021. 7. 19. 16:47
CSS Text Shadow Mouse Move Effect
css shadow가 마우스가 움직일 때마다 움직인다.
1. es6 문법으로 상수,변수 작성
2.좌표값 변경
3. shadow 움직이는 반경 구하기
코드<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Mouse Shadow</title> </head> <body> <div class="hero"> <h1 contenteditable>🔥WOAH!</h1> </div> <style> html { color: black; font-family: sans-serif; } body { margin: 0; } .hero { min-height: 100vh; display: flex; justify-content: center; align-items: center; color: black; } h1 { text-shadow: 10px 10px 0 rgba(0,0,0,1); font-size: 100px; } </style> <script> const hero = document.querySelector('.hero'); const text = hero.querySelector('h1'); const walk = 500; // 500px function shadow(e){ const {offsetWidth : width, offsetHeight : height} = hero;//const offsetWidth = hero.width , offsetHeight =hero.height let {offsetX : x, offsetY: y} = e;//const x = e.offsetX , y = e.offsetY if (this !== e.target) { x = x + e.target.offsetLeft;//.hero와 h1의 offset값이 다르고 h1쪽 offset값이 너무 작아 누적값 넣음 y = y + e.target.offsetTop; } const xWalk = Math.round((x / width * walk) - (walk / 2)); const yWalk = Math.round((y / height * walk) - (walk / 2)); text.style.textShadow = ` ${xWalk}px ${yWalk}px 0 rgba(255,0,255,0.7), ${xWalk * -1}px ${yWalk}px 0 rgba(0,255,255,0.7), ${yWalk}px ${xWalk * -1}px 0 rgba(0,255,0,0.7), ${yWalk * -1}px ${xWalk}px 0 rgba(0,0,255,0.7) `; console.log(x,y); } hero.addEventListener('mousemove',shadow); </script> </body> </html>
알게된 문법
const {offsetWidth : width, offsetHeight : height} = hero;//const offsetWidth = hero.width , offsetHeight =hero.height
let {offsetX : x, offsetY: y} = e;//const x = e.offsetX , y = e.offsetY
es6에서 새로 나온 문법으로 더 가독성있게 작성이 가능하다
x = x + e.target.offsetLeft;
.hero와 h1의 offset값이 다르고 h1쪽 offset값이 너무 작아 누적값 넣은것
const xWalk = Math.round((x / width * walk) - (walk / 2));
const yWalk = Math.round((y / height * walk) - (walk / 2));
이해 안된다;;;;;;;; 왜 반경을 이렇게 구하느거지
느낀점
난 수학이 약하다....