Jump to content
Search Community

How to gracefully exit a repeating yoyo?

Visual-Q test
Moderator Tag

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

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();
    }
});

 

Link to comment
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.

 

 

Link to comment
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
Link to comment
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.
×
×
  • Create New...