5

I'm working on a webapp that uses CSS3 animations for some nice effects, but I can't figure out to activate them with an onclick event.

this is the CSS3 Animation: (The name of the DIV this is added to is #smspopup)

#smspopup {    
-webkit-animation: move-sms 1.5s 1;
    -webkit-animation-fill-mode: forwards;
}

And this is my Javascript where I just can't figure out what I need to get it going

function cancel_sms() 
{
    halte.style.opacity = 1;
    document.getElementById('smspopup').style.visibility = 'hidden';
    document.getElementById('up').style.visibility = 'visible';
}

And another thing I want to do is delay the functions 1.5 seconds until the animation is finished. Anyone any idea?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
IanBauters
  • 123
  • 1
  • 2
  • 8
  • [**This question** might help.](http://stackoverflow.com/questions/12062818/how-to-combine-jquery-animate-with-css3-properties-without-using-css-transitions) – Barlas Apaydin Aug 24 '12 at 14:34

1 Answers1

3

Starting the animation

Start by using a class instead of an ID. Change the CSS to this:

.smspopup {    
    -webkit-animation: move-sms 1.5s 1;
    -webkit-animation-fill-mode: forwards;
}

And add class=smspopup to the smspopup element.

Next, inside the handler (cancel_sms?), just add the class to the element to begin the animation:

document.getElementById('smspopup').className = 'smspopup';

Animation end callback

For the second question (targeting the end of the animation), there are two options:

  1. Attach a callback to the transitionEnd event. The only problem with this is that you need to listen to vendor-specific events:

    myDiv.addEventListener('webkitAnimationEnd', callback);

  2. Use a regular timeout. The problem with this is that the timing won't be perfect (but maybe close enough).

    setTimeout(callback, 1500);

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • Ok, no problem, what I was currently trying is this: _italic_document.getElementById('smspopup').-webkit-animation('move-sms 1.5s 1 forwards;');_ Any idea where I went wrong here? – IanBauters Aug 24 '12 at 16:48
  • @IanBauters to add CSS properties directly, you'd use this: *document.getElementById('smspopup').style["-webkit-animation"] = 'move-sms 1.5s 1 forwards';* – McGarnagle Aug 24 '12 at 16:53
  • Great! thanks, you din't happen to know how you reset the animation with webkitAnimationEnd? – IanBauters Aug 24 '12 at 17:01