search

Home  >  Q&A  >  body text

css3动画 - 如何重新运行css3的动画?

如何通过一个事件(hoverclick..)触发css3动画的重新执行?
在stackoverflow找到一个相关的问题,但是下面给出的答案也不起作用。

http://jsfiddle.net/arunpjohny/8WQcy/1/


这篇文章挺好的,介绍了重新运行css3动画的不同方法


animation必须是infiniteanimation-play-state才有效。

restart用这种方法挺好:

// retrieve the element
element = document.getElementById("logo");

// reset the transition by...
element.addEventListener("click", function(e) {
  e.preventDefault;

  // -> removing the class
  element.classList.remove("run-animation");

  // -> triggering reflow /* The actual magic */
  // without this it wouldn't work. Try uncommenting the line and the transition won't be retriggered.
  element.offsetWidth = element.offsetWidth;

  // -> and re-adding the class
  element.classList.add("run-animation");
}, false);
大家讲道理大家讲道理2788 days ago515

reply all(6)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 11:14:15

    Explanation: http://www.w3school.com.cn/cssref/pr_animation-play-state.asp
    Test: http://www.w3school.com.cn/tiy/c.asp?f=css_animation-play-state&p=...

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 11:14:15

    $it.css('-webkit-animation-play-state', 'running');
    

    You wrote the attribute name wrong here

    reply
    0
  • PHPz

    PHPz2017-04-17 11:14:15

    I am also looking for how to use events to restart css3 animation, but I find that this question is too simple or something. I can’t find the answer at all in China. I can only bite the bullet and go to stackoverflow to search, and it’s very difficult. Coincidentally, I also found your article. It was very well written and very detailed. Although my English is not good, I benefited a lot. I wonder why this seemingly simple question cannot be found on Baidu at all. Don’t you think that everyone has it? Have you ever encountered it?

    reply
    0
  • 黄舟

    黄舟2017-04-17 11:14:15

    I have also encountered this problem, but fortunately it is easy to solve. Add a controllable CSS to the DOM element to be animated.

    js. Control $('.animation').removeClass('active').delay(10).queue(function(next){

                $(this).addClass('active');
                next();  //让下面的队列函数得以执行; 猜的
                });

    reply
    0
  • PHPz

    PHPz2017-04-17 11:14:15

    Still not working on IPHONE9.1. PC browser is normal. Is there any solution?

    reply
    0
  • 迷茫

    迷茫2017-04-17 11:14:15

    Why not use the animationend event? This is also very compatible

    el.addEventListener('click',function(){
        this.classList.add('active');
        this.addEventListener('animationend',function(){
            this.classList.remove('active');
        });
    });
        

    reply
    0
  • Cancelreply