Skip to content

Instantly share code, notes, and snippets.

@HamedFathi
Created November 11, 2020 14:36
Show Gist options
  • Save HamedFathi/ca7f1bc05290ed7697778765645724c0 to your computer and use it in GitHub Desktop.
Save HamedFathi/ca7f1bc05290ed7697778765645724c0 to your computer and use it in GitHub Desktop.
import { injectStyle } from 'styl-injector';
const rippleStyleObj = {
"[data-animation]": { position: "relative", overflow: "hidden" },
".ripple": {
width: "2px",
height: "2px",
position: "absolute",
borderRadius: "50%",
backgroundColor: "rgba(255, 255, 255, 0.5)",
WebkitAnimation: "rippleEffect 0.5s ease-in-out",
animation: "rippleEffect 0.5s ease-in-out",
},
"@-webkit-keyframes rippleEffect": {
"0%": { WebkitTransform: "scale(1)", transform: "scale(1)" },
"100%": {
opacity: "0",
WebkitTransform: "scale(var(--scale))",
transform: "scale(var(--scale))",
},
},
"@keyframes rippleEffect": {
"0%": { WebkitTransform: "scale(1)", transform: "scale(1)" },
"100%": {
opacity: "0",
WebkitTransform: "scale(var(--scale))",
transform: "scale(var(--scale))",
},
},
};
export function applyRippleEffect(htmlElement: HTMLElement) : void {
injectStyle(rippleStyleObj, "at-ripple-style", false);
htmlElement.setAttribute("data-animation", "ripple");
htmlElement.addEventListener("mousedown", function (e) {
const x = e.pageX - this.offsetLeft;
const y = e.pageY - this.offsetTop;
const w = this.offsetWidth.toString();
const ripple = document.createElement("span");
ripple.className = "ripple";
ripple.style.left = x + "px";
ripple.style.top = y + "px";
ripple.style.setProperty("--scale", w);
this.appendChild(ripple);
setTimeout(() => {
ripple.remove();
}, 500);
});
}
/*
[data-animation] {
position: relative;
overflow: hidden;
}
.ripple {
width: 2px;
height: 2px;
position: absolute;
border-radius: 50%;
background-color: rgba(255, 255, 255, 0.5);
-webkit-animation: rippleEffect 0.5s ease-in-out;
animation: rippleEffect 0.5s ease-in-out;
}
@-webkit-keyframes rippleEffect {
0% {
-webkit-transform: scale(1);
transform: scale(1);
}
100% {
opacity: 0;
-webkit-transform: scale(var(--scale));
transform: scale(var(--scale));
}
}
@keyframes rippleEffect {
0% {
-webkit-transform: scale(1);
transform: scale(1);
}
100% {
opacity: 0;
-webkit-transform: scale(var(--scale));
transform: scale(var(--scale));
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment