Skip to content

Commit 6903199

Browse files
committed
refactor shadow function
1 parent d93d866 commit 6903199

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

  • apps/mouse-move-shadow/assets/script

apps/mouse-move-shadow/assets/script/index.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,29 @@ const container = document.querySelector(".container")
22
const title = document.querySelector(".container__title")
33
const walk = 500
44

5+
const applyShadow = (xWalk, yWalk, title) => {
6+
title.style.textShadow = `
7+
${xWalk}px ${yWalk}px 0 rgba(255,0,255,0.7),
8+
${xWalk * -1}px ${yWalk}px 0 rgba(0,255,255,0.7),
9+
${yWalk}px ${xWalk * -1}px 0 rgba(0,255,0,0.7),
10+
${yWalk * -1}px ${xWalk}px 0 rgba(0,0,255,0.7)
11+
`;
12+
}
13+
514
const shadow = event => {
615
const { offsetWidth: width, offsetHeight: height } = container;
716
let { offsetX: x, offsetY: y } = event;
8-
17+
918
if (this !== event.target) {
1019
x = x + event.target.offsetLeft;
1120
y = y + event.target.offsetTop;
1221
}
1322

1423
const xWalk = Math.round((x / width * walk) - (walk / 2));
1524
const yWalk = Math.round((y / height * walk) - (walk / 2));
16-
title.style.textShadow = `
17-
${xWalk}px ${yWalk}px 0 rgba(255,0,255,0.7),
18-
${xWalk * -1}px ${yWalk}px 0 rgba(0,255,255,0.7),
19-
${yWalk}px ${xWalk * -1}px 0 rgba(0,255,0,0.7),
20-
${yWalk * -1}px ${xWalk}px 0 rgba(0,0,255,0.7)
21-
`;
25+
26+
applyShadow(xWalk, yWalk, title);
27+
2228
}
2329

24-
container.addEventListener('mousemove', shadow);
30+
container.addEventListener('mousemove', shadow);

0 commit comments

Comments
 (0)