Jump to content
Search Community

ScrollTrigger's markers don't refresh after window resize.

Ramate test
Moderator Tag

Recommended Posts

Hello,

I am using Scrolltrigger to animate several elements of my project. Everything works fine until I resize the window. I am already using ScrollTrigger.refresh(); everytime any element can potentially modify my flow, it still not working.

I suppose my problem is caused by a custom container I am using as scroller to avoid the weird resizing on mobile browsers, I wrapped my content on and it works fine. But still thought the markers are not being updated when i resize the window.

 

ScrollTrigger.defaults({
  scroller: "#scrollable-container",
});


Here is one example animation:
 

function manageScrollAnimations() {
  //// CHANGE ANIMATION OPACITY ON SCROLL
  const contentRows = document.querySelectorAll(".has-animation");

  contentRows.forEach((row, index) => {
    const animation = row.querySelector(".animation");

    if (index !== 0 && index !== contentRows.length - 1) {
      animation.classList.add("invisible");
    }

    ScrollTrigger.refresh();
    ScrollTrigger.create({
      trigger: row,
      start: "top center",
      end: `bottom center`,

      onEnter: () => {
        animation.classList.remove("invisible");
      },
      onLeave: () => {
        if (index !== contentRows.length - 1) {
          animation.classList.add("invisible");
        }
      },
      onEnterBack: () => {
        animation.classList.remove("invisible");
      },
      onLeaveBack: () => {
        if (index !== 0 && index !== contentRows.length - 1) {
          animation.classList.add("invisible");
        }
      },
      markers: true,
      scrub: true,
    });
  });

//// resize 

  window.addEventListener("resize", handleResize);
  
  function handleResize() {
    manageTopBarTextAnimations();
    positionAnimatedWords();
    updateTopBarHeight();
    resizeAnimationWrappers();
    manageScrollAnimations();
    ScrollTrigger.refresh();
  }


I am kind of running out of options. I read something about ScrollTrigger.scrollerProxy, that (maybe) can solve my problem (not sure), but I could not understand how to implement it.

I also used a eventListener, it logs and still thought the markers nothing.

ScrollTrigger.addEventListener("refresh", function () {
  console.log("Refreshed");
});


Here is the link of the project, as a code pen would not serve the issue.
http://kunden.brueckner.studio/frankwords/

Any thoughts about what can be wrong?

Thanks a lot 

Link to comment
Share on other sites

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependancies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

Your issue is probably that you're adding classes to your elements and now have to fix everything that ScrollTrigger does out of the box if you just use tweens. 

 

If you create an animation you want to happen in a timeline and then hook that up to ScrollTrigger (eg animate .from() an opacity of 0 to the default 1 of the browser) 

 

const tl = gsap.timeline({...yourScrollTriggerSettings});

tl.from(".animation", {opacity: 0}); // Aniamte from invisable to viable 
tl.to(".animation", {opacity: 0});// Aniamte from viable to invisable 

 

The beauty of GSAP is that you can build what ever you like in what ever way you like, but there are a lot of things already solved for you if you use it the "intended" way. 

 

If you have some dynamic values that change on resize/refresh you can use a function based value in a tween to force it to recalculate on .refresh().  https://gsap.com/docs/v3/GSAP/gsap.to()/#function-based-values

 

If you still need help please include a minimal demo showing us what you've already tried. Please don't include your whole project, just some coloured divs to demonstrate the issue. Hope it helps and happy tweening! 

  • Like 2
Link to comment
Share on other sites

Thanks a lot for the answer @mvaneijgen. Yes, I am aware about the capabilities (and magic) of the timeline, but thanks for remembering.

In the example function above, I use class toggling as a decision, as I am not planing actually to animate with a transition as the timeline could, but take advantage of the triggers to change the state from visible to invisible in each element, synchronised with my content. I could use display:none; for example, by removing the elements totally and adding it back.

In the exemple function above I switch the visibility of one Lottie animation to the next animation, so I fake a seamless animation. One element appears exactly in the time and position the other disappears.  My animation is made in many pieces (as required from the design I have).

The animations themselves are actually controlled by lottie player in the case of my project. You can see it here: http://kunden.brueckner.studio/frankwords/

///

Unfortunately I could not reproduce the bug as I can see in my project on CodePen. There it seams I have a kind of limitation on the definition of the scroller defaults, (maybe) so it is quite not related to my trouble of resizing the window.  So I would not focus on the minimal demo.

But I add it here anyway. (:

See the Pen xxBawaM?editors=1111 by QuitoSometimesMarcos (@QuitoSometimesMarcos) on CodePen




///

So I still in the trouble of refreshing the markers after resize, when I have the scrolltriger defaults pointing another scroller.

Any ideas?

Thanks

Link to comment
Share on other sites

Hi,

 

I forked your last demo and tweaked a bit in order to make it work:

See the Pen BabOJKZ by GreenSock (@GreenSock) on CodePen

 

IDK if the issue can be reproduced there, it seems to me that everything keeps working after a window resize so maybe I'm missing something here 🤷‍♂️

 

Hopefully this helps.

Happy Tweening!

Link to comment
Share on other sites

@Rodrigo it's finally fixed! It was my mistake on forgetting to refactor the way I was retrieving the height of some elements, that was necessary at certain point, but not anymore. Code blindness 😅

But it totally worth, as i didn't know that way of indenting and identifying  the markers. That's soooo useful!!! Thanks a lot!

Thanks to @mvaneijgen too

  • Like 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...