Jump to content
Search Community

How to disable ScrollTrigger auto play when an element is visible after page load

flim test
Moderator Tag

Go to solution Solved by OSUblake,

Recommended Posts

I want the element to show in the animation "end" position when it is already on the screen, i.e. after page loaded.

For instance, in the codepen, change the zoom to "0.25x", "b" will be visible, "b" will start to play just if I run the codepen again. How to make "b" start in the animation end position if it is visible already? Like when "b" is visible after page loaded, the tween duration is "0".

See the Pen abyRXVb by fredericklim (@fredericklim) on CodePen

Link to comment
Share on other sites

3 hours ago, OSUblake said:

Hi flim, 

 

Check out the new .isInViewport() method.

https://greensock.com/docs/v3/Plugins/ScrollTrigger/static.isInViewport()

 

So if it's in the viewport, just set the animation's progress to 1.

if (ScrollTrigger.isInViewport(myElement)) {
  myAnimation.progress(1);
}

 

Hi OSUblake,

 

Thanks for the advice, I shall read the doc more carefully. Here is my trial, if I change the zoom to 0.25x, the element 'b' will be at the end position, change the zoom to 1x the normal scroll animation resume.

 

See the Pen NWvewXE by fredericklim (@fredericklim) on CodePen

 

It is not perfect, the animation position will jump to the end if I resize the viewport which is large enough to make the element in the viewport. Any advice?

 

Link to comment
Share on other sites

If you want it to only run once, you shouldn't put it inside a callback. 

 

var animb = 
    gsap.to(".b", {
      x: 400,
      rotation: 360,
    });

ScrollTrigger.create({
  trigger: ".b",
  animation: animb,
  start: "top bottom",
  end: "bottom top",
  toggleActions: "play none none reset",
});

if (ScrollTrigger.isInViewport(".b")) {
  animb.progress(1);
};

 

  • Thanks 1
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...