Visual-Q

How to gracefully exit a repeating yoyo?

Recommended Posts

Hey Guys,

 

I was wondering if there is a better way to do this.

 

I have an arrow svg element that when hovered over bounces left and right in repeating yoyo. The only way I could think of to exit it was to pause the animation and then set a new tween telling it to go back to its start position. This works fine but I was wondering is there a more sophisticated way to accomplish this, i.e. is it possible to intercept the first tween and tell it to finish it's current iteration and then stop?

 

homeButton.each(function(){
 $(this).hover(over, out);
   var buttonSVGElemtent = $(this).find('svg');
   var homeButtonAnimation;

   function over(){
    homeButtonAnimation = TweenMax.to(buttonSVGElemtent, .5, {xPercent:50, repeat:-1, yoyo:true, ease:Power1.easeInOut});
    }

    function out(){
    homeButtonAnimation.pause();
    homeButtonAnimation = TweenMax.to(buttonSVGElemtent, .5, {xPercent:0, ease:Power3.easeOut});
    homeButtonAnimation.play();
    }
});

 

Share this post


Link to post
Share on other sites

In this example the animation always returns to the start position when you press the stop button.

Perhaps you can do something similar on over() out(). 

 

If you need something different please provide a reduced test case that we can work with. 

  • Like 3

Share this post


Link to post
Share on other sites

Awesome, thanks Carl,  I'm always amazed at how responsive you guys are.

 

I have a followup question, i was trying something similar with the onRepeat directly on the tween with the yoyo and it seemed to work if the yoyo was  returning to start but would stick at the midpoint if the yoyo was in it's initial yo if that makes sense. Does this mean that a yoyo effect is broadcasting two onRepeat events, one on the way out and one on the way back.

 

Given the way it's wrapped in a timeline I can see why it works in the codepen example if that is the case.

 

 

Share this post


Link to post
Share on other sites

Yes, onRepeat fires at the end of each iteration, regardless of which "yo" it is :)

  • Haha 1

Share this post


Link to post
Share on other sites

Thanks to Carl for pointing me in the right direction.

 

If anyone can benefit from it here's the final code, a pretty simple and straightforward way to connect a repeating yoyo to a jQuery  hover method.

 

var homeButton = $('button.home');


homeButton.each(function(){
 $(this).hover(over, out);
 
   var buttonSVGElement = $(this).find('svg'),
       homeButtonAnimation = new TimelineMax({repeat:-1, onRepeat: pauseAnimation, paused:true}),
       play;
       
       homeButtonAnimation.to(buttonSVGElement, .5, {xPercent:50, repeat:1, yoyo:true, ease:Power1.easeInOut});

   function over(){
      homeButtonAnimation.resume();
      play = true;
   }

   function out(){
      play = false;
   }
    
   function pauseAnimation() {
      if(play === false){
         homeButtonAnimation.pause();
      }
   }
    
});

 

  • Like 2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.