简体   繁体   English

动画完成时触发事件(无法控制动画)

[英]Trigger event on animation complete (no control over animation)

I've a scenario that requires me to detect animation stop of a periodically animated element and trigger a function. 我有一个场景,要求我检测周期性动画元素的动画停止并触发功能。 I've no control over the element's animation . 我无法控制该元素的动画 The animation can be dynamic so I can't use clever setTimeout . 动画可以是动态的,所以我不能使用聪明的setTimeout

Long Story 很长的故事

The simplified form of the problem is that I'm using a third party jQuery sliding banners plugin that uses some obfuscated JavaScript to slide banners in and out. 问题的简化形式是我正在使用第三方jQuery 滑动横幅插件 ,该插件使用一些混淆的JavaScript来滑动横幅。 I'm in need of figuring out a hook on slideComplete sort of event, but all I have is an element id . 我需要弄清楚slideComplete事件的种类,但我所拥有的只是一个元素id Take this jsfiddle as an example and imagine that the javascript has been obfuscated. 这个jsfiddle为例,想象一下JavaScript已经被混淆了。 I need to trigger a function when the red box reaches the extremes and stops. 当红色框达到极限并停止时,我需要触发一个函数。

I'm aware of the :animated pseudo selector but I think it will need me to constantly poll the required element. 我知道:animated伪选择器,但我认为它将需要我不断轮询所需的元素。 I've gone through this , this , and this , but no avail. 我经历了thisthisthis ,但无济于事。 I've checked jquery promise but I couldn't figure out to use that in this scenario. 我已经检查了jQuery Promise,但是我不知道在这种情况下可以使用它。 This SO question is closest to my requirements but it has no answers. 这样的问题最接近我的要求,但没有答案。

PS Some more information that might be helpful: PS一些可能有用的更多信息:

  1. The element isn't created by JavaScript, it is present on page load. 元素不是由JavaScript创建的,它会在页面加载时显示。
  2. I've control over when to apply the plugin (that makes it periodically sliding banner) on the element 我已经控制了何时在元素上应用插件(使其定期滑动横幅)

Most of the slideshow plugins I have used use changing classes at the end of the animation... You could extend the "addClass" method of jQuery to allow you to capture the class change as long as the plugin you use is using that method like it should: 我使用过的大多数幻灯片插件在动画末尾都使用更改类...您可以扩展jQuery的“ addClass”方法,以使您可以捕获类更改,只要您使用的插件正在使用该方法即可,例如这应该:

(function($){

$.each(["addClass","removeClass"],function(i,methodname){
      var oldmethod = $.fn[methodname];
      $.fn[methodname] = function(){
            oldmethod.apply( this, arguments );
            this.trigger(methodname+"change");
            return this;
      }
   });
})(jQuery);

I threw together a fiddle here 在这里摆了一个小提琴

Even with obfuscated code you should be able to use this method to check how they are sending in the arguments to animate (I use the "options" object when I send arguments to animate usually) and wrap their callback function in an anonymous function that triggers an event... like this fiddle 即使使用混淆的代码,您也应该能够使用此方法检查它们如何发送参数进行动画处理(通常,当我发送参数进行动画处理时,我会使用“ options”对象)并将其回调函数包装在触发的匿名函数中一个事件... 像这个小提琴

Here is the relevant block of script: 这是相关的脚本块:

(function($){
$.each(["animate"],function(i,methodname){
      var oldmethod = $.fn[methodname];
      $.fn[methodname] = function(){
          var args=arguments;
          that=this;
          var oldcall=args[2];
          args[2]=function(){
              oldcall();
              console.log("slideFinish");
          }
          oldmethod.apply( this, args );

          return this;
      }
    });
})(jQuery);

Well since you didn't give any indication as to what kind of animation is being done, I'm going to assume that its a horizontal/vertical translation, although I think this could be applied to other effects as well. 好吧,既然您没有说明正在执行哪种动画,那么我将假定其为水平/垂直平移,尽管我认为这也可以应用于其他效果。 Because I don't know how the animation is being accomplished, a setInterval evaluation would be the only way I can guess at how to do this. 因为我不知道动画是如何完成的,所以setInterval评估将是我猜测如何做到这一点的唯一方法。

var prevPos = 0;
var isAnimating = setInterval(function(){
    if($(YOUROBJECT).css('top') == prevPos){
        //logic here
    }
    else{
        prevPos = $(YOUROBJECT).css('top');
    }
},500);

That will evaluate the vertical position of the object every .5 seconds, and if the current vertical position is equal to the one taken .5 seconds ago, it will assume that animation has stopped and you can execute some code. 这将每隔0.5秒评估对象的垂直位置,如果当前垂直位置等于.5秒钟前获取的垂直位置,则将假定动画已停止并且您可以执行一些代码。

edit -- 编辑-

just noticed your jsfiddle had a horizontal translation, so the code for your jsfiddle is here http://jsfiddle.net/wZbNA/3/ 刚刚注意到您的jsfiddle进行了水平翻译,因此您的jsfiddle的代码位于http://jsfiddle.net/wZbNA/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM