0

I have a balloon that when hovered, will expand n disappear (a popping-like animation). I made this in CSS but when the cursor moves, the balloon returned. I want the balloon to disappear forever until I refresh the page, so I guess it needs to be onclick, but that selector is not available in CSS.

Here's what I have in CSS

@keyframes pop
{
    from{
        opacity:1;
        transform: translateZ(0) scale(1,1);
    }

    to{
        opacity:0;
        transform: translateZ(0) scale(1.5,1.5);
    }
}

.balloon:hover
{
    animation: pop 0.5s cubic-bezier(0.16, 0.87, 0.48, 0.99) forwards;
}

I saw another question that said the closest thing is :active but it requires the mouse to be held down. If I want it to be onclick, I need to use Javascript. But I don't know what I need to write to trigger the animation.

And is it also possible to make it so that when I pop 1 balloon, all the others will pop too automatically with a 1s delay inbetween? (There are 5 balloons).

jhashane
  • 774
  • 1
  • 9
  • 17
Novelcie
  • 56
  • 7
  • Does this answer your question? [Trigger CSS Animations in JavaScript](https://stackoverflow.com/questions/44846614/trigger-css-animations-in-javascript) – Rob Apr 12 '20 at 11:09

2 Answers2

1

You can add and remove the class of the animation with JS using classList.

Add:

object.classList.add('balloon');

Remove:

object.classList.remove('balloon');

Working example:

const add = () => {
  document.getElementById('balloon').classList.add('animation')
}

const remove = () => {
  document.getElementById('balloon').classList.remove('animation')
}
@keyframes pop {
  from {
    opacity: 1;
    transform: translateZ(0) scale(1, 1);
  }
  to {
    opacity: 0;
    transform: translateZ(0) scale(1.5, 1.5);
  }
}

.animation {
  animation: pop 0.5s cubic-bezier(0.16, 0.87, 0.48, 0.99) forwards;
}

.balloon {
  height: 125px;
  width: 110px;
  background-color: #FF6B6B;
  border-radius: 50%;
}

.controls{
  position: absolute;
  bottom: 10px;
  right: 10px;
}
<div id="balloon" class="balloon" onmouseover="add()"></div>
<div class="controls">
  <button onClick="add()">Hide</button>
  <button onClick="remove()">Show</button>
</div>
Tamas Szoke
  • 5,426
  • 4
  • 24
  • 39
0

Here is a solution which makes balloons hiding one by one with interval .5s between them

var balloons = document.getElementsByClassName('balloon');
[...balloons].forEach( (e, i)=>{
  e.onmouseover = function() {
    this.classList.add('hidden');
    setTimeout(hideAll, 500, balloons);
    }
});
function hideAll(arg){
    [...arg].forEach( (e, i)=>{
      if ( ! e.classList.contains('hidden') ) {
        e.style.animationDelay = i+'s';
        e.classList.add('hidden');
      }
    });
}
@keyframes pop
{
    from{
        opacity:1;
        transform: translateZ(0) scale(1,1);
    }

    to{
        opacity:0;
        transform: translateZ(0) scale(1.5,1.5);
    }
}

.balloon.hidden
{
    animation: pop .5s cubic-bezier(0.16, 0.87, 0.48, 0.99) forwards;
}
<div class="balloon">Balloon</div>
<div class="balloon">Balloon</div>
<div class="balloon">Balloon</div>
<div class="balloon">Balloon</div>
<div class="balloon">Balloon</div>
Banzay
  • 9,310
  • 2
  • 27
  • 46