In this project of "whack-a-mole game", I have to create a visual effect of when a popped up mole is "whacked". I've decided that when I click on the mole, I want it to create a radiating circle animation effect. I have tried to accomplish the task with only CSS although some JS starter code was given.
I posted a snippet of the code and I have to post all of it in order for the game to work. As you can see, whenever I click on a "mole head", it does create the circle but it surrounds the mole-head div only and then disappears really quickly. It does not seem to create the intended animated effect I am looking for which is when you click on the head or in the area, it will create an effect like the picture shown below:
If I have too much code or have not formatted anything right I apologize ahead of time. Jake has provided a solution and has kindly given me a solution with just the circles but I have provided my problem.
let score = 0;
let molesLeft = 30;
let popupLength = 3000;
let hideTimeout;
let clickable = false;
function popUpRandomMole() {
if (molesLeft <= 0) {
document
.querySelector(".sb__game-over")
.classList.remove("sb__game-over--hidden");
return;
}
const moleHeads = document.querySelectorAll(".wgs__mole-head");
if (moleHeads.length === 0) {
return;
}
const moleIndex = Math.floor(Math.random() * moleHeads.length);
const moleHead = moleHeads[moleIndex];
clickable = true;
// UNCOMMENT THIS LINE OF CODE WHEN DIRECTED
moleHead.classList.remove(
"wgs__mole-head--hidden",
"wgs__mole-head--whacked"
);
molesLeft -= 1;
document.querySelector(".sb__moles").innerHTML = molesLeft;
hideTimeout = setTimeout(() => hideMole(moleHead), popupLength);
}
function hideMole(mole) {
clickable = false;
mole.classList.add("wgs__mole-head--hidden");
setTimeout(popUpRandomMole, 500);
}
window.addEventListener("DOMContentLoaded", () => {
setTimeout(popUpRandomMole, 0);
const moleHeads = document.querySelectorAll(".wgs__mole-head");
for (let moleHead of moleHeads) {
moleHead.addEventListener("click", (event) => {
if (!clickable) return;
score += 1;
document.querySelector(".sb__score").innerHTML = score;
popupLength -= popupLength / 10;
clearTimeout(hideTimeout);
hideMole(event.target);
// UNCOMMENT THIS LINE OF CODE WHEN DIRECTED
event.target.classList.add("wgs__mole--hidden");
// UNCOMMENT THIS LINE OF CODE WHEN DIRECTED FOR THE BONUS
event.target.classList.add("wgs__mole-head--whacked");
});
}
});
/* Your code here */
.wgs {
height: 241px;
position: relative;
width: 320px;
display: inline-block;
overflow: hidden;
}
.wgs__mole-head {
position: absolute;
height: 178px;
width: 188px;
left: 55px;
transition: margin-top 0.25s;
}
.wgs__dirt-pile {
position: absolute;
height: 110px;
width: 320px;
top: 130px;
}
.wgs__mole-head--hidden {
margin-top: 482px;
}
.pf {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: 1fr 1fr;
}
/* UNCOMMENT THE CODE BELOW WHEN DIRECTED */
.sb {
background-color: cornflowerblue;
border-bottom: 1px solid #645ded;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
color: white;
display: flex;
font-size: 30px;
margin-bottom: 50px;
padding: 10px 20px;
}
.wgs__mole-head--whacked {
border-radius: 50%;
border: 5px solid blue;
position: absolute;
opacity: 0;
animation: circle 1s ease-out;
width: 20px;
box-sizing: border-box;
pointer-events: none;
aspect-ratio: 1/1;
transform: translateX(-50%) translateY(-50%);
}
@keyframes circle {
0% {
width: 20px;
opacity: 1;
}
100% {
width: 100px;
opacity: 0.2;
}
}
*/ .sb__mole-counter {
flex: 1 0 0;
text-align: right;
}
.sb__score-holder {
flex: 1 0 0;
}
.sb__game-over--hidden {
display: none;
}
<link href="https://github.com/RandyGoldsmith/practice-for-prepwork-css-whack-a-mole/blob/main/mole.css" rel="stylesheet"/>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mole.css" />
<script type="text/javascript" src="mole.js"></script>
</head>
<body>
<div class="pf">
<div class="wgs">
<img
src="https://png.pngtree.com/png-clipart/20210425/original/pngtree-mole-head-vector-png-image_6256794.png"
class="wgs__mole-head wgs__mole-head--hidden wgs__mole-head--whacked"
>
<img src="https://static.wikia.nocookie.net/scribblenauts/images/a/a8/Mole_Hill.png/revision/latest?cb=20140412011314" class="wgs__dirt-pile" >
</div>
<div class="wgs">
<img
src="https://png.pngtree.com/png-clipart/20210425/original/pngtree-mole-head-vector-png-image_6256794.png"
class="wgs__mole-head wgs__mole-head--hidden wgs__mole-head--whacked">
<img src="https://static.wikia.nocookie.net/scribblenauts/images/a/a8/Mole_Hill.png/revision/latest?cb=20140412011314" class="wgs__dirt-pile">
</div>
<div class="wgs">
<img
src="https://png.pngtree.com/png-clipart/20210425/original/pngtree-mole-head-vector-png-image_6256794.png"
class="wgs__mole-head wgs__mole-head--hidden wgs__mole-head--whacked">
<img src="https://static.wikia.nocookie.net/scribblenauts/images/a/a8/Mole_Hill.png/revision/latest?cb=20140412011314" class="wgs__dirt-pile">
</div>
<div class="wgs">
<img
src="https://png.pngtree.com/png-clipart/20210425/original/pngtree-mole-head-vector-png-image_6256794.png"
class="wgs__mole-head wgs__mole-head--hidden wgs__mole-head--whacked">
<img src="https://static.wikia.nocookie.net/scribblenauts/images/a/a8/Mole_Hill.png/revision/latest?cb=20140412011314" class="wgs__dirt-pile">
</div>
<div class="wgs">
<img
src="https://png.pngtree.com/png-clipart/20210425/original/pngtree-mole-head-vector-png-image_6256794.png"
class="wgs__mole-head wgs__mole-head--hidden wgs__mole-head--whacked">
<img src="https://static.wikia.nocookie.net/scribblenauts/images/a/a8/Mole_Hill.png/revision/latest?cb=20140412011314" class="wgs__dirt-pile">
</div>
<div class="wgs">
<img
src="https://png.pngtree.com/png-clipart/20210425/original/pngtree-mole-head-vector-png-image_6256794.png"
class="wgs__mole-head wgs__mole-head--hidden wgs__mole-head--whacked">
<img src="https://static.wikia.nocookie.net/scribblenauts/images/a/a8/Mole_Hill.png/revision/latest?cb=20140412011314" class="wgs__dirt-pile">
</div>
<div class="wgs wgs__mole--whacked">
<img
src="https://png.pngtree.com/png-clipart/20210425/original/pngtree-mole-head-vector-png-image_6256794.png"
class="wgs__mole-head wgs__mole-head--hidden wgs__mole-head--whacked">
<img src="https://static.wikia.nocookie.net/scribblenauts/images/a/a8/Mole_Hill.png/revision/latest?cb=20140412011314" class="wgs__dirt-pile">
</div>
<div class="wgs">
<img
src="https://png.pngtree.com/png-clipart/20210425/original/pngtree-mole-head-vector-png-image_6256794.png"
class="wgs__mole-head wgs__mole-head--hiddenwgs__mole-head--whacked">
<img src="https://static.wikia.nocookie.net/scribblenauts/images/a/a8/Mole_Hill.png/revision/latest?cb=20140412011314" class="wgs__dirt-pile">
</div>
</div>
<div class="sb">
<div class="sb__score-holder">Score <span class="sb__score">0</span></div>
<div class="sb__game-over sb__game-over--hidden">Game Over</div>
<div class="sb__mole-counter">
Moles <span class="sb__moles">30</span>
</div>
</div>
</body>
</html>