13

I am using CSS shader + animation. My shader class is defined as follows:

.shader{
-webkit-filter :custom(url(v.vs) mix(url(f.fs) multiply destination-over), 200 200);
-webkit-animation-name: test;
-webkit-animation-duration: 2s;
-webkit-animation-iteration-count: 1
}

I am trying to set/unset the styles(shader+animation) dynamically using jquery through $('#holder').addClass('shader'); and $('#holder').removeClass('shader');

However, the weird thing is when I reset the class (e.g., calling addClass after removeClass), only the shader gets reapplied but the animation doesn't (I have hooked the AnimationStart event to see when my animation starts). Anyone know why this is the case and how I can solve it?

Edit: I added a simplified version of JSfiddle snippet here. Essentially I'm re-applying the animation to a div twice but the actual animation only gets called the first time.

Discombobulous
  • 1,112
  • 2
  • 14
  • 25

9 Answers9

29

The problem is that, even though you remove and then re-apply the animated class, you do this in the course of a single, blocking function. When your function exits, it appears to the rendering engine that nothing has changed.

One solution (the one that you chose), is to clone the element and destroy the original. This is fine, but if you had any event bindings to the original element (i think) they they will be destroyed too.

Another approach is to remove the animated class from the element, and then wrap the code that re-applies the class inside of a setTimeout() call with a very small delay, e.g.

$('#holder').removeClass('shader');
setTimeout(
    function(){$('#holder').addClass('shader')}
    , 1);

I've tweaked your jsfiddle to use this approach: http://jsfiddle.net/HuFBN/7/

NobodyMan
  • 2,381
  • 4
  • 29
  • 35
  • 4
    This is the correct answer. Extra points go for explaining why this hack is needed ("When your function exits, it appears to the rendering engine that nothing has changed"). – Agoston Horvath Jul 10 '14 at 09:47
  • This worked for me. What I also like about it this too is that you can easily adjust the delay in this approach as when it should start again. thanks! – klewis Nov 10 '15 at 17:18
5

According to a 2011 article on css-tricks.com, triggering a reflow in between removing and adding the class will restart the animation. Example (verbose):

$('#holder').removeClass('shader');
$('#holder').offsetWidth = $('#holder').offsetWidth; // triggers reflow
$('#holder').addClass('shader'); // restarts animation
feklee
  • 7,555
  • 9
  • 54
  • 72
4

I think I figured it out. According to this, css animation can't get applied to the same node twice (even if you have a different animation!). So I had to clone the node, remove the original, and add back the cloned node.

Discombobulous
  • 1,112
  • 2
  • 14
  • 25
1

You need to recreate your element.

function resetAnimation(jqNode) {
   var clone = jqNode.clone();
   jqNode.after( clone );
   jqNode.remove();
   jqNode[0] = clone[0];
}
Denis535
  • 3,407
  • 4
  • 25
  • 36
1

with this simple code you do it ( you must use to triggers reflow .width() ):

$('#holder').removeClass('shader');
$('#holder').width();
$('#holder').addClass('shader');
hossein naghneh
  • 469
  • 7
  • 11
0

Try using this just after you apply the animation - given that "e" is your animated element:

e.outerHTML = e.outerHTML;
ElDoRado1239
  • 3,938
  • 2
  • 16
  • 13
0

way to trigger a reflow:

  element.classList.remove("class");
  element.scrollBy(0, 0);
  element.classList.add("class");

works in strict mode! doesn't require a write operation on a read-only value! doesn't require a whole new function! one line and go! :)

0

I'd be happy to hear any criticism with my method using requestAnimationFrame as I don't see anyone else using it:

element.classList.add("class");
window.requestAnimationFrame(() => element.classList.remove("class"));
koral
  • 525
  • 7
  • 23
-1

You definitely have to remove class that contains animation and then add it again. It should also work without .offsetWidth . It worked for me. So

$('#id').removeClass('animationClass');
$('#id').addClass('animationClass'); // starts animation again

should do the trick.

Matic Brank
  • 207
  • 1
  • 2
  • 6