1

I am trying to start this specific CSS animation with jquery when you click the button. no idea how. Any ideas?

THanks

    <button>Click here to start the animation!!</button>
    <div id="ball"></div>

#ball {
    background:tomato;
    height:50px;
    width:50px;
    border-radius:200px;
    position:absolute;
    animation:bounce 3s infinite;
    -webkit-animation:bounce 3s infinite;
    top:40px;
}
@-webkit-keyframes bounce {
    50% {
        top: calc(100% - 50px);
    }
}
@keyframes bounce {
    50% {
        top: calc(100% - 50px);
    }
}
1
  • 1
    you could try to add the animation to a class and then give that element that class via jquery. Commented Mar 20, 2014 at 23:34

2 Answers 2

2

A very simple method would be to have the CSS animation properties on a seperate class and then toggle the class when the button is clicked.

EXAMPLE HERE

#ball.animate {
    animation:bounce 3s infinite;
    -webkit-animation:bounce 3s infinite;
}

jQuery:

$('button').on('click',function(){
    $('#ball').toggleClass('animate');
});
Sign up to request clarification or add additional context in comments.

Comments

0

Why not just use pure CSS3? There's no need to bring jQUery into the mix. Here's a jsfiddle illustrating how to do it using nothing more than the :checked pseudo selector.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.