Bug 140853

Summary: Animations not showing upon adding/removal of classes
Product: WebKit Reporter: lentiformnucleus
Component: AnimationsAssignee: Nobody <webkit-unassigned>
Status: RESOLVED INVALID    
Severity: Normal CC: dino, graouts
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: PC   
OS: Linux   
Attachments:
Description Flags
Test none

Description lentiformnucleus 2015-01-24 00:31:32 PST
Animations fail to "execute" when they are called the SECOND time around by the method of adding and removing classes that reference that animation in a CSS stylesheet(or inline CSS), with JavaScript's classList.add() and classList.remove() methods.

CSS RULES START

@-webkit-keyframes fadeIn {
  to {
    opacity: 1;
  }
}
@keyframes fadeIn {
  to {
    opacity: 1;
  }
}

.fadeIn {
  -webkit-animation: fadeIn .5s ease-in 1 forwards;
  animation: fadeIn .5s ease-in 1 forwards;
}

CSS RULES END

JAVASCRIPT CODE START

element = document.getElementById("anyElement");

var effect = {

    fadeIn: function(el) {

        el.style.opacity = 0;
        el.classList.remove("fadeIn");
        el.classList.add("fadeIn");

    }

};

effects.fadeIn(element);

JAVASCRIPT CODE END

By coding it this way, it works only under Firefox. However, there's a workaround in Webkit browsers, and that is by adding a timeout to the class addition, like so:

    fadeIn: function(el) {

        el.style.opacity = 0;
        el.classList.remove("fadeIn");
        setTimeout(el.classList.add("fadeIn"), 25);

    }

This way, the animation starts as it should, but a timeout shouldn't be required for it to work. I'm thinking, therefore, that it is a bug. (Note that the timeout of 25 is more or less arbitrary, but a number too small won't fix the behaviour)
Comment 1 lentiformnucleus 2015-01-24 00:36:51 PST
Sorry, there's a small typo in the code. var effect should be var effects. And also, the 'fadeIn' method should be called more than once for the bug to be noticeable (note that in the code, I only call it once). Please make the pertintent amendments.
Comment 2 lentiformnucleus 2015-01-24 00:42:32 PST
(In reply to comment #1)
> Sorry, there's a small typo in the code. var effect should be var effects.
> And also, the 'fadeIn' method should be called more than once for the bug to
> be noticeable (note that in the code, I only call it once). Please make the
> pertintent amendments.

Aaaah, setTimeout(el.classList.add("fadeIn"), 25); should read instead:

setTimeout(function(){ el.classList.add("fadeIn"); }, 25);

As you of course know. Sorry, I'm getting sleepy.
Comment 3 Antoine Quint 2023-05-10 01:12:58 PDT
Created attachment 466305 [details]
Test

It would help to have a test case to run, but I believe I've recreated one that shows what was described by the originator.
Comment 4 Antoine Quint 2023-05-10 01:14:05 PDT
I'm going to close this, removing the "fadeIn" class and adding straight away without waiting for style resolution should indeed NOT trigger a new animation. The test case I wrote behaves the same between Firefox and Safari. If you find this is not correct, please reopen this bug with a test case showing the issue.