Jump to content
Search Community

ScrollTrigger matchmedia does not work as I expected.

nosugar test
Moderator Tag

Go to solution Solved by GreenSock,

Recommended Posts

ScrollTrigger Matchmedia function does not work as I expected. It works successfully when i scroll without resizing screen. 

Please check my codesandbox link (https://codesandbox.io/s/blissful-franklin-j6rdo?file=/src/contents.js) here.

Once I resize the screen to tablet view (~1024px) or opposite way from tablet view to Desktop view, 

 

- ScrollTrigger behavior does not work as initial setting. 

- Having console error message with `GSAP target null not found`

 

As I found out the simple examples of matchmedia was working with resizing. But, my code does not work. Once resize the screen,

seems scrollTrigger is getting confused. What did I miss on here?

 

 

Link to comment
Share on other sites

  • Solution

That error means you're trying to animate/set a null target. In other words, something is off in your code that's causing it to try to animate a non-existent object. 

 

Also, you're using the wrong syntax for saveStyles() - it only accepts one parameter. You can pass in an Array if you'd like. 

 

The main problem is that you're creating your ScrollTrigger OUTSIDE of the matchMedia() call, thus it's not linked to any of them. It looks like you're trying to constantly reuse the same instance which is definitely going to cause problems. 

 

// BAD
const tl = gsap.timeline({scrollTrigger:{...}});
ScrollTrigger.matchMedia({
  "(min-width: 1024px)": () => {
    tl.to(...);
  },
  "(max-width: 1023px)": () => {
    tl.to(...);
  }
});


// GOOD
ScrollTrigger.matchMedia({
  "(min-width: 1024px)": () => {
    const tl = gsap.timeline({scrollTrigger:{...}});
    tl.to(...);
  },
  "(max-width: 1023px)": () => {
    const tl = gsap.timeline({scrollTrigger:{...}});
    tl.to(...);
  }
});

Does that clear things up? 

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