Skip to content

Commit b973984

Browse files
committed
refactor: getClickPosition
1 parent cb1d0b3 commit b973984

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

020-button ripple effect/script.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
const buttons = document.querySelectorAll(".ripple");
22

3+
function getClickPosition(e, element) {
4+
const x = e.clientX;
5+
const y = e.clientY;
6+
const buttonTop = element.offsetTop;
7+
const buttonLeft = element.offsetLeft;
8+
return {
9+
xInside: x - buttonLeft,
10+
yInside: y - buttonTop,
11+
};
12+
}
13+
314
buttons.forEach((button) => {
415
button.addEventListener("click", function (e) {
5-
const x = e.clientX;
6-
const y = e.clientY;
7-
const buttonTop = e.target.offsetTop;
8-
const buttonLeft = e.target.offsetLeft;
9-
const xInside = x - buttonLeft;
10-
const yInside = y - buttonTop;
16+
const { xInside, yInside } = getClickPosition(e, this);
1117
const circle = document.createElement("span");
1218
circle.classList.add("circle");
1319
circle.style.top = yInside + "px";

0 commit comments

Comments
 (0)