Peter
Peter

Reputation: 29

How can I repeat an animation (CSS and Javascript onclick)

How can I repeat an animation which is set with CSS and Javascript by onclick? I am a very beginner code writer and know HTML, CSS and Javascript. I do not know why the following online click and function just work once. I will appreciate if you help me.

Code

function discharge() {
    document.getElementById("electron").style.visibility = "visible";
    document.getElementById("electron").style.WebkitAnimation = "mymove 5s 1   backwards"; // Code for Chrome, Safari, and Opera
    document.getElementById("submit").style.backgroundColor = "lightblue";
    document.getElementById("submit").style.boxShadow = "0px 3px 3px red";
    document.getElementById("submit1").style.backgroundColor = "#4CAF50";
    document.getElementById("submit1").style.boxShadow = "0px 0px 0px red";
}
#electron {
    position: relative;
    left: -10px;
    visibility: hidden;
}
@-webkit-keyframes mymove {
    0% {
        left: 0px;
        top: 0px;
    }
    25% {
        left: -32px;
        top: 0px;
    }
    50% {
        left: -32px;
        top: -20px;
    }
    75% {
        left: 158px;
        top: -20px;
    }
    100% {
        left: 158px;
        top: 5px;
    }
}
<div>
    <button id="submit" onclick="discharge()">Discharge</button>
    <button id="submit1">charge</button>
</div>

<div id="electron">
    <br /><i class="fa fa-minus-circle"></i>
    <br /><i class="fa fa-minus-   circle"></i>
    <br />
</div>

Upvotes: 0

Views: 1210

Answers (3)

Matt Ellen
Matt Ellen

Reputation: 11612

How I managed it was with a timeout and an extra class:

function doFadeOut()
{
  let f = document.getElementById('f');
  f.classList.remove('unseen');
  setTimeout(() =>
  {
    f.classList.add('unseen');
  }, 2000);
}
#f
{
    opacity:0%;
    animation: fadeout 2s linear 1;
}

    #f.unseen
    {
        visibility: hidden;
        opacity:100%;
        animation:inherit;
    }

@keyframes fadeout
{
    from
    {
        opacity: 100%;
    }

    to
    {
        opacity: 0%;
    }
}
<div id="f" class="unseen">
this will appear then fade out with every click of the button below
</div>
<button onclick="doFadeOut()">show the message</button>

The main trick seems to be switching the CSS animation property at the end of the animation.

The way I have done it couples the time in the CSS with the time in the JS, making it a little brittle, so if there is an easy way to only set the time once, that would be great.

Upvotes: 0

Jijo Cleetus
Jijo Cleetus

Reputation: 2867

Please use this answer to solve your issues

html

    <div>
  <button id="submit" onclick="discharge()">Discharge</button>
  <button id="submit1" onclick="charge()">charge</button>
</div>

<div id="electron">
  <br/><i class="fa fa-minus-circle"></i>
  <br/><i class="fa fa-minus-   circle"></i>
  <br/>
</div>

javascript

discharge = function() {
  document.getElementById("electron").style.visibility = "visible";
  document.getElementById("electron").style.WebkitAnimation = "mymove 5s 1   backwards"; // Code for Chrome, Safari, and Opera
  document.getElementById("submit").style.backgroundColor = "lightblue";
  document.getElementById("submit").style.boxShadow = "0px 3px 3px red";
  document.getElementById("submit1").style.backgroundColor = "#4CAF50";
  document.getElementById("submit1").style.boxShadow = "0px 0px 0px red";
}

charge = function(){
document.getElementById("electron").removeAttribute('style');
document.getElementById("submit").removeAttribute('style');
document.getElementById("submit1").removeAttribute('style');
}

No change in css.

Upvotes: 1

Jijo Cleetus
Jijo Cleetus

Reputation: 2867

the given javascript code having an issue with defining function. Please use discharge = function (){ There are some missing elements like electron and li Also i cannot find any function called charge()

So i have commented missing elements and provided below.

discharge = function () {
// document.getElementById("electron").style.visibility = "visible";
// document.getElementById("li").style.visibility = "visible";
// document.getElementById("electron").style.WebkitAnimation = "mymove 5s 1   backwards"; // Code for Chrome, Safari, and Opera
document.getElementById("submit").style.backgroundColor = "lightblue";
document.getElementById("submit").style.boxShadow = "0px 3px 3px red";
document.getElementById("submit1").style.backgroundColor = "#4CAF50";
document.getElementById("submit1").style.boxShadow = "0px 0px 0px red";}

Upvotes: 1

Related Questions