i’m trying to create simple ripple effect. When i add classes to element newClick i expected ripple effect. Got troblued because appending class is instantly and i dont see the effect. Could you help me?
const button = document.querySelector('.button');
const handleClick = (e) => {
const newClick = document.createElement('div');
newClick.classList.add('point');
newClick.style.top = `${e.offsetY}px`;
newClick.style.left = `${e.offsetX}px`;
newClick.classList.add('active');
button.appendChild(newClick);
};
button.addEventListener('click', handleClick);
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #000;
}
.button {
position: relative;
background-color: #7c1a76;
border: none;
padding: 1em 2em;
color: #fff;
letter-spacing: 1px;
text-transform: uppercase;
cursor: pointer;
overflow: hidden;
}
.point {
position: absolute;
width: 5px;
height: 5px;
border-radius: 50%;
opacity: 1;
background-color: #fff;
transform: scale(1);
transition: transform 0.5s, opacity 0.5s;
}
.active {
transform: scale(10);
background-color: #fff;
opacity: 0;
transition: transform 0.5s, opacity 0.5s;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Button Ripple Effect</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<button class="button">click me</button>
<script src="script.js"></script>
</body>
</html>
I spend few hours on this trouble. Why i dont see effect of transition? This is project from tutorial but i want do it on my way.
2
Answers
It doesn’t work because you add element to button after adding class.
To solve this problem, you must have clickContainer in button because your code create newClick element on every click event.
ok?
like this
And then, follow steps below.
-create newClick element
-empty clickContainer
-append newClick element to clickContainer
-add class to newClick
Then it’ll works.
And you used transition.
I think it’s not suitable for this problem because after circle getting
bigger, it’s opacity is 0 but it keep it’s size.
And also, you used 2 classes for this.
It’s not good.
So I think it’s easy and suitable to use animation .
I add codes below.
-script
-css
Transitions only happen when a CSS property changes it’s value.
You are creating the
<div>
, adding both classes and appending it to the DOM, all in the same frame. From the CSS perspective you’re creating and adding adiv.point.active
. The styles never change so there’s no need to transition anything.Delay adding
.active
by a frame.But you also never clean up so the divs for the ripples are piling up. And if you click too fast,
offsetX
andoffsetY
are wrong and the next ripple appears top left in the button. You should prevent the ripples catching mouse events.Or, instead of using transitions, check out the animation API