Skip to content

Instantly share code, notes, and snippets.

@DungGramer
Created February 25, 2021 19:01
Show Gist options
  • Save DungGramer/615b64ec4b7abc534d3902254e6fc903 to your computer and use it in GitHub Desktop.
Save DungGramer/615b64ec4b7abc534d3902254e6fc903 to your computer and use it in GitHub Desktop.
function createRipple(event) {
const button = event.currentTarget;
const circle = document.createElement("span");
const diameter = Math.max(button.clientWidth, button.clientHeight);
const radius = diameter / 2;
circle.style.width = circle.style.height = `${diameter}px`;
circle.style.left = `${event.clientX - button.offsetLeft - radius}px`;
circle.style.top = `${event.clientY - button.offsetTop - radius}px`;
circle.setAttribute("ripple", "");
button.appendChild(circle);
setTimeout(() => {
button.removeChild(circle);
}, 600);
}
const buttons = document.querySelectorAll("button[ripple]");
for (const button of buttons) {
button.addEventListener("click", createRipple);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment