How To Create Ripple Animation Button In HTML CSS And Javascript | letsbug
Hi, today we are creating a ripple animation on a button using HTML CSS and Javascript. We are going to implement this feature in a way that we can use the code multiple times basically D.R.Y.
Ripple Animation Button
code:
index.html
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>ripple Effect</title><link rel="stylesheet" href="./style.css"><script src="./script.js" defer ></script></head><body><button class="ripple-btn">CLICK me</button><button class="ripple-btn">hello</button></body></html>
styles.css
*{margin:0;padding: 0;box-sizing: border-box;}body{min-height: 100vh;display: grid;place-items: center;font-family: system-ui;}.ripple-btn{appearance: none;position: relative;display: inline-grid;place-items: center;isolation: isolate;font-size: 3em;font-weight: 500;padding: 1em 3em;text-transform: uppercase;background-color: transparent;border: 5px solid currentColor;border-radius: 0.125em;overflow: hidden;cursor: pointer;box-shadow: 10px 10px 5px red;}.ripple-btn::after{content: ' ';position: absolute;top:var(--y);left:var(--x);transform: translate(-50%, -50%) scale(0);width: 200%;display: block;z-index: -1;aspect-ratio: 1/1;border-radius: 50%;background-color: red;opacity: .35;transition: transform .5s;}.ripple-animation::after{animation: ani 500ms;}@keyframes ani {0%{transform: translate(-50%, -50%) scale(0);opacity: 0.5;}100%{transform: translate(-50%, -50%) scale(1);opacity: 0;}}
script.js
const buttons = document.querySelectorAll('.ripple-btn')buttons.forEach(btn => {btn.addEventListener("click", (e) => ripple(e, btn))})function ripple(event, element) {let posX = event.offsetX;let posY = event.offsetY;element.style.setProperty("--x", `${posX}px`)element.style.setProperty("--y", `${posY}px`)element.classList.add('ripple-animation')element.addEventListener("animationend", () => {element.classList.remove("ripple-animation")}, {once:true})}
output:
Thankyou
Comments
Post a Comment