-
Notifications
You must be signed in to change notification settings - Fork 0
/
button-ripple.js
53 lines (44 loc) · 1.58 KB
/
button-ripple.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const ripples = document.querySelectorAll("[ripple]");
function addRipple(e) {
const rippleContainer = this.querySelector(".ripple--container");
const size = rippleContainer.offsetWidth;
const pos = rippleContainer.getBoundingClientRect();
const rippler = document.createElement("span");
const x = e.pageX - pos.left - size / 2;
const y = e.pageY - pos.top - size / 2;
let style = `top: ${y}px; left: ${x}px; height: ${size}px; width: ${size}px;`;
rippleContainer.appendChild(rippler);
rippler.setAttribute("style", style);
// console.log(rippleContainer);
}
function cleanUp(e) {
const rippleContainer = this.querySelector(".ripple--container");
while (rippleContainer.firstChild) {
rippleContainer.removeChild(rippleContainer.firstChild);
// console.log("remove first child");
}
}
ripples.forEach((ripple) => {
const rippleContainer = document.createElement("div");
rippleContainer.className = "ripple--container";
// optional property for ripple
if (ripple.getAttribute("ripple-color")) {
ripple.style.setProperty(
"--ripple-color",
ripple.getAttribute("ripple-color")
);
}
if (ripple.getAttribute("ripple-radius")) {
ripple.style.setProperty(
"--ripple-radius",
ripple.getAttribute("ripple-radius")
);
}
// add ripple on mouse click
ripple.addEventListener("mousedown", addRipple);
// remove ripple after mouse click
ripple.addEventListener("mouseup", debounce(cleanUp, 2000));
// render ripple container on ripple button
ripple.rippleContainer = rippleContainer;
ripple.appendChild(rippleContainer);
});