When clicking the “big cookie” in cookie clicker, there is a popup showing how many cookies you earned (+276.341 septillion in this image), which slowly moves upward and fades out.
I wanted to implement a similar feature in my game, I sucessfully made the moving up and fading out part with css animations, however, there will be more than one of these numbers showing up at once, so how do I clone elements? And how do I make these show up at the cursor position?
Advertisement
Answer
I don’t know what technologies you are using so here is how to do it with vanilla HTML and JS. You say you already made the HTML & CSS, so turn your HTML into a template
JavaScript
x
4
1
<template id="floating-text-template">
2
<!-- your existing code -->
3
</template>
4
Now in your javascript, clone the template on each click
JavaScript
1
8
1
function click(event) {
2
const template = document.getElementByID('#floating-text-template').content.cloneNode(true);
3
const element = template.querySelector('.floating-text') //replace class with yours
4
element.style.left = `${event.clientX}px`
5
element.style.top = `${event.clientY}px`
6
document.appendChild(element);
7
}
8