5

我有一个使用Animate.CSS的动画,如果用户愿意,我想重播它,但我尝试的方法不起作用。这是代码:

HTML:

    <div class="img-center">
       <img src="path.jpg" class="feature-image animated rotateInDownRight" />
    </div>
       <p class="textcenter">&nbsp;</p>
    <div class="img-center">
       <a href="#" id="replay">Replay</a>
    </div>

JS:

var $j = jQuery.noConflict();
$j("#replay").click(function() {
    $j('.feature-image').removeClass('animated rotateInDownRight').addClass('animated rotateInDownRight');
});

我确实知道脚本本身可以工作,因为我可以在 Firebug 中看到它发生,但是动画不会再次动画。如何使用 Animate.CSS 实现这一目标?

4

6 回答 6

6

这只是一个猜测,但似乎 jQuery 在将其添加回之前并未“完成”删除该类。我知道这没有任何意义,但这就是 JavaScript 的工作方式。它可以在第一个函数的所有内容完成之前调用链中的下一个函数。我浏览了 Animate.CSS 网站上的代码,发现他们在动画功能中使用了超时。你也可以试试。这是他们的代码:

function testAnim(x) {
    $('#animateTest').removeClass().addClass(x);
    var wait = window.setTimeout( function(){
        $('#animateTest').removeClass()},
        1300
    );
}

这与您正在做的完全一样,只是它等待动画完成,然后删除类。这样,当另一个类被重新添加时,它对标签来说是真正的“新”。这是一个稍微修改的函数:

function testAnim(elementId, animClasses) {
    $(elementId).addClass(animClasses);
    var wait = window.setTimeout( function(){
        $(elementId).removeClass(animClasses)},
        1300
    );
}

请注意两件事:首先,此代码将允许您更改获取动画的元素。其次,在 1300 毫秒后删除添加的类。仍然不是 100%,但它可能会让你走得更远。

需要注意的是,如果对象上已经有一些动画类,它可能会破坏这个 JS。

于 2012-09-13T04:05:43.157 回答
2

在animate.css 问题#3中找到了正确答案

var $at = $('#animateTest').removeClass();  
//timeout is important !!
setTimeout(function(){ 
   $at.addClass('flash') 
}, 10);

实际上,更简单的版本也可以避免使用 JQuery。

el.classList.remove('animated','flash');  
//timeout is important !!
setTimeout(function(){ 
   el.classList.add('animated','flash');
}, 10);
于 2015-08-19T06:47:20.103 回答
1

我相信这里的问题是,当我删除课程时,它会快速添加课程。以下是我解决此问题的方法:

(HTML 与上述问题相同)。

JS:

var $j = jQuery.noConflict();

window.setTimeout( function(){
   $j('.feature-image').removeClass('animated rotateInDownRight')},
1300);


$j("#replay").click(function() {
    $j('.feature-image').addClass('animated rotateInDownRight');
});

我相信正在发生的是 jQuery 代码正在快速删除和添加类。不管这个代码工作的原因是什么。

于 2012-09-13T04:09:08.957 回答
0

如果你愿意,你也可以试试这个支持animate.css动画的 javaScript 端开发。这是一个使用示例。

 //Select the elements to animate and enjoy!
 var elt = document.querySelector("#notification") ;
 iJS.animate(elt, "shake") ;
 //it return an AnimationPlayer object
 //animation iteration and duration can also be indicated.
 var vivifyElt = iJS.animate(elt, "bounce", 3, 500) ;
 vivifyElt.onfinish = function(e) {
     //doSomething ...;
 }
 // less than 1500ms later...changed mind!
 vivifyElt.cancel();

看看这里

于 2015-11-15T09:10:11.663 回答
0

我的回答是添加/删除css class色调延迟的技巧:

$('#Box').removeClass('animated').hide().delay(1).queue(function() { 
        $(this).addClass('animated').show().dequeue();
});

您也可以在没有隐藏/显示方法的情况下对其进行测试:

$('#Box').removeClass('animated').delay(1).queue(function() { 
        $(this).addClass('animated').dequeue();
});

我填充它工作顺利,chrome但它有更多意想不到的延迟FF,所以你可以测试这个 js 超时:

$('#Box').removeClass('animated');  
setTimeout(function(){ 
    $('#Box').addClass('animated');
}, 1);
于 2016-11-06T01:25:12.123 回答
0

这个解决方案依赖于 React useEffect,它相当干净,因为它避免了直接操作类名。

它并没有真正回答 OP 问题(似乎使用 jQuery),但它可能对许多使用 React 和 Animate CSS 库的人仍然有用。

  const [repeatAnimation, setRepeatAnimation] = useState<boolean>(true);

  /**
   * When the displayedFrom changes, replay the animations of the component.
   * It toggles the CSS classes injected in the component to force replaying the animations.
   * Uses a short timeout that isn't noticeable to the human eye, but is necessary for the toggle to work properly.
   */
  useEffect(() => {
    setRepeatAnimation(false);
    setTimeout(() => setRepeatAnimation(true), 100);
  }, [displayedFrom]);

  return (
    <div
      className={classnames('block-picker-menu', {
        'animate__animated': repeatAnimation,
        'animate__pulse': repeatAnimation,
      })}
  ...
  )
于 2021-02-09T15:40:06.767 回答