Tweenmax callback being executed before the animat

2019-07-21 11:54发布

I am using the greensock gsap for animations on my web site. Problem are the callbacks who are executing before the end of the animation. In the example bellow the elements are being removed somewhere on the half of the animation.

TweenLite.to($(".flipper"), 2, {rotationY:180,onComplete:function(){
    $(this).remove()
}});

Did anyone experienced the same issue?

3条回答
三岁会撩人
2楼-- · 2019-07-21 12:10

No, works fine for me (see jsfiddle below). However, this in your callback function is not your animated object, it's the tween. So you have to use this.target instead if you want to remove it after animation, like this:

TweenLite.to($(".flipper"), 1, {rotationY:180,onComplete:function(){
    (this.target).remove()
}});

http://jsfiddle.net/brian_griffin/5Ltfg/

查看更多
一纸荒年 Trace。
3楼-- · 2019-07-21 12:24

As @hjuster pointed out, a transition declared in CSS can conflict with onComplete callback in TweenMax. I think that the reason why onComplete is invoked in the wrong time

查看更多
姐就是有狂的资本
4楼-- · 2019-07-21 12:26

I always recommend using the onUpdateScope, onCompleteScope, etc so you are completely clear on what the scope of this you are doing. The documentation is rather slim on this because it is kinda buried. All onDoSomething functions for greensock have a scope parameter.

TweenLite.to($(".flipper"), 2, {rotationY:180,onCompleteScope: $(".flipper"),
    onComplete:function(){$(this).remove()
}});

Makes it completely clear to the Tween that $(this) = $(".flipper"). This is also extremely helpful for when you are wanting to make changes out of scope to the tween. Depending on where you ran you tween it may not have access to jquery objects outside of it's scope but you can pass in a different scope via the onCompleteScope.

查看更多
登录 后发表回答