Jump to content
Search Community

ScrollTrigger.matchMedia() resize bug

Julia Shikanova test
Moderator Tag

Go to solution Solved by OSUblake,

Recommended Posts

Hello

So I stumbled across some weird behaviour (video), ScrollTrigger.matchMedia() is not recalculating elements positions when breakpoint changes, even with ScrollTrigger.refresh() on resize.

window.addEventListener("resize", () => ScrollTrigger.refresh());

I wonder if it's a bug or am I missing something?

Thanks in advance!

See the Pen RwgwEoP by jshikanova (@jshikanova) on CodePen

Link to comment
Share on other sites

By the way, this is completely useless: 

window.addEventListener("resize", () => ScrollTrigger.refresh());

In fact, it only hurts things because ScrollTrigger automatically calls ScrollTrigger.refresh() on resize anyway but it throttles things so that it only fires after the user stops resizing for 0.2 seconds. Otherwise, it would fire the expensive operation too often and degrade performance. Your code above only hurts performance. :)

 

Blake looks like he nailed it. Let us know if you need anything else. 

Link to comment
Share on other sites

21 hours ago, OSUblake said:

You have a straggler here.

const timeline2 = gsap.timeline({
  scrollTrigger: {
    trigger: "#section-2",
    start: "center center",
    end: `+=200%`,
    scrub: 0.5,
    pin: true,
    invalidateOnRefresh: true,
    id: "section-2",
    markers: true
  }
});

 

 

I really appreciate the solution, but...

In my project I have a bunch of sections (let's say 10, for example), each of the sections animating with different functions, and in some of those functions I need to have a different animations on desktop and mobile (= using matchMedia). And calling every other functions inside every matchMedia is not possible. Is there are any other way to trigger recalculating scrollTrigger's values (start and end positions, sizes, etc)?  Isn't it weird it's not doing it and breaking layout, overlaying sections that supposed to go one after another?

 

 

 

Link to comment
Share on other sites

  • Solution
19 minutes ago, Julia Shikanova said:

And calling every other functions inside every matchMedia is not possible.

 

Not sure why that wouldn't be possible. I would think that would make it super easy.

ScrollTrigger.matchMedia({
  "(max-width: 767px)": mobile,
  "(min-width: 768px)": desktop
});

function desktop() {
  // create desktop animations and scroll triggers
}

function mobile() {
  // create mobile animations and scroll triggers
}

 

Another option to try might be something like this.

ScrollTrigger.addEventListener("refresh", () => ScrollTrigger.sort());

 

  • Like 3
Link to comment
Share on other sites

2 hours ago, OSUblake said:

 

Not sure why that wouldn't be possible. I would think that would make it super easy.

ScrollTrigger.matchMedia({
  "(max-width: 767px)": mobile,
  "(min-width: 768px)": desktop
});

function desktop() {
  // create desktop animations and scroll triggers
}

function mobile() {
  // create mobile animations and scroll triggers
}

 

It's not very suitable for the particular project and calling every other functions with scrollTrigger animation inside every matchMedia() doesn't seem like very nice solution, I think

 

2 hours ago, OSUblake said:

 

Another option to try might be something like this.

ScrollTrigger.addEventListener("refresh", () => ScrollTrigger.sort());

 

It works, but it takes quite a few moments to recalculate so it flickers (video) even with small demo

See the Pen VwWYbaK by jshikanova (@jshikanova) on CodePen

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