亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Way to prevent CSS animation from stopping when removing a class
P粉412533525
P粉412533525 2023-08-29 16:15:06
0
1
652
<p>I have a grid. When I receive an update message from the backend, I need to highlight the rows in orange within 3 seconds. When I receive an update I add the css class 'highlight' to my row and play my animation. </p> <p> <pre class="brush:css;toolbar:false;">.highlight { animation-name: highlight; animation-duration: 3s; } @keyframes highlight { 0% { background-color: orange; } 99.99% { background-color: orange; } }</pre> </p> <p>Due to some reason with the message flow in the application, I need to remove the highlight class before the 3 seconds are up so that my animation stops working. I want my animation to play until the end of 3 seconds. </p> <p>How can I make my animation play to the end even if I delete the highlight class? </p>
P粉412533525
P粉412533525

reply all(1)
P粉265724930

One possible approach would be to add a data-attribute to the element and then add an animationend event listener to it so that when the animation completes, the event listener knows to remove the class. See example below.

document.getElementById('clicky').addEventListener('click', () => {
  const element=document.querySelector('.highlight');
  element.setAttribute('data-remove-class', 'highlight');
  element.innerHTML='將在動畫結(jié)束時移除類';
});

document.querySelector('.highlight').addEventListener('animationend', (e) => {
  const removeClass = e.target.getAttribute('data-remove-class');
  if (removeClass == 'highlight') {
    e.target.classList.remove(removeClass);
    e.target.removeAttribute('data-remove-class');
    e.target.innerHTML='類已移除!';
  }
});
.highlight {
  animation-name: highlight;
  animation-duration: 3s;
}

@keyframes highlight {
  0% {
    background-color: yellow;
  }
  99.99% {
    background-color: orange;
  }
}
<div class='highlight'>正在動畫中!</div>
<button id='clicky'>移除類</button>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template