Jump to content
Search Community

[Solved] ScrollTrigger.matchMedia is not a function

xposedbones test
Moderator Tag

Recommended Posts

Started using ScrollTrigger for the first time on a project and was happy to hear about the matchmedia function, but I can't get it to work. I want to disable the instance on mobile and enable it on desktop and I'm getting a 'ScrollTrigger.matchMedia is not a function' in my console. I'm also not seeing the matchMedia in the autocompletion of ScrollTrigger. Here is my code: 

 

import { gsap, Power4 } from 'gsap';
import { ScrollTrigger } from 'gsap/ScrollTrigger';
/*
. . .
*/
gsap.registerPlugin(ScrollTrigger);
gsap.defaults({ease: Power4.easeOut, duration: 2});
this.tl = gsap.timeline({paused: true});

for(let i: number = 0; i < sections.length; i++) {
  this.tl.from(sections[i], {yPercent: i == 0 ? 0 : 200, autoAlpha: i == 0 ? 1 : 0});
}

ScrollTrigger.matchMedia({
  '(min-width: 768px)': () => {
    if (this.instance == null) {
      this.instance = ScrollTrigger.create({
        animation: this.tl,
        trigger: this.viewport,
        start: 'center center',
        end: `+=${window.innerHeight * 2}`,
        scrub: true,
        pin: true, 
        anticipatePin: 1
      });
      this.instanceDisabled = false;
    } else if (this.instanceDisabled) {
      this.instance.enable();
    }
  }, 
  '(max-width: 767px)': () => {
    if (this.instance && !this.instanceDisabled) {
      this.instance.disable();
      this.instanceDisabled = true;
    }
  }
});

image.png.7d485c7a79f7a1a9e6897678a32bcda7.png

The ScrollTrigger.create works fine call it outside of the matchmedia so I know I'm correctly loading the plugin. I'm on 3.4.2

Link to comment
Share on other sites

That error definitely sounds like you're loading a version of GSAP that's BEFORE 3.4.0 (which is when .matchMedia() was introduced). I realize your package.json indicates 3.4.2, but perhaps you need to clear your cache or something? Also, try console.log(ScrollTrigger.version) and see what that logs out. 

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