-1

Here I want to display particles

Hi guys, I want to program in javascript that every time you click on the button a particle appears which then shows +1 for example. Like in this cookie clicker (I hope you can see it): Particles on https://orteil.dashnet.org/cookieclicker/

I already looked at particles.js, but I didn't find the right result for me there. I would like to display the particle on every click, I want it to float to the top and then disappear.

protob
  • 3,317
  • 1
  • 8
  • 19
Lama_Agent
  • 29
  • 5

1 Answers1

1

I think you need something like this, I am adding one button and when clicking on it it will append +1. As per this now you can modify it according to your requirements.

const addBtn = document.getElementById("add-button");
const particleBox = document.getElementById("particle-area");

function createParticle(parentElement = document.body) {
  const particle = document.createElement("div");
  particle.classList.add("particle");
  particle.innerHTML = "<h1>+1</h1>";
  if (parentElement) {
    parentElement.append(particle);
  }
  return particle;
}

addBtn.addEventListener("click", function() {
  const particle = createParticle(particleBox);
  particle.addEventListener("animationend", function() {
    particle.remove();
  });
});
#particle-area {
  position: relative;
  width: 100px;
  height: 100px;
  display: flex;
  flex-direction: column-reverse;
}

.particle {
  width: 30px;
  height: 30px;
  animation: particle-animation 2s ease-out;
}

@keyframes particle-animation {
  0% {
    transform: translate(-50%, -50%) scale(0);
    opacity: 1;
  }

  100% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 0;
  }
}
<div id="particle-area"></div>
<button id="add-button"> Mine Gold</button>
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Rutik Patel
  • 183
  • 3
  • 15