nosugar Posted August 2, 2020 Share Posted August 2, 2020 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 More sharing options...
Solution GreenSock Posted August 2, 2020 Solution Share Posted August 2, 2020 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? 2 1 Link to comment Share on other sites More sharing options...
nosugar Posted August 2, 2020 Author Share Posted August 2, 2020 Thank you so much. @GreenSock After I fix all issues based on your feedback from my codes It works as I originally planned. I appreciate your help. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now