Jump to content
Search Community

ScrollTrigger matchMedia timeline on enter function

AdventurousDeveloper test
Moderator Tag

Go to solution Solved by GreenSock,

Recommended Posts

Hi there,

 

Hoping someone may have an answer or can give some insight.

 

Within a ScrollTrigger.matchMedia I have two of the same timelines, difference being they have specific start and end points. Each also have the same onEnter which runs a function. 

 

The issue I'm having is the onEnter function is run twice, which I guess makes sense as theres two timelines.. Is there a particular way onEnter functions need to be added within matchMedia?

 

Here's an example of my code, I can try get a codepen up if needed.

 

ScrollTrigger.matchMedia({

  "(min-width: 1441px)": function() {			

    let item = document.getElementById('item');

    gsap.to(item, {
      ease: "none",
      scrollTrigger: {
        trigger: item,
        start: "+=40% +=40%", 
        end: "+=40% +=40%",
        scrub: true,
        markers: false,
        once: true,
        onEnter: function onEnter() {
          innitFunction();
        },
      }
    });

  },

  "(min-width: 1440px)": function() {	

    let item = document.getElementById('item');		

    gsap.to(item, {
      ease: "none",
      scrollTrigger: {
        trigger: item,
        start: "+=30% +=50%", 
        end: "+=30% +=50%",
        scrub: true,
        markers: false,
        once: true,
        onEnter: function onEnter() {
          innitFunction();
        },
      }
    });

  }

});

innitFunction();

 

Really appreciate any help/info on this :)

 

Cheers, 

Link to comment
Share on other sites

  • Solution

You've got both matchMedia queries almost identical, and they BOTH match anything over 1440px wide: 

"(min-width: 1441px)"
"(min-width: 1440px)"

I think you meant to do this: 

"(min-width: 1441px)"
"(max-width: 1440px)"

Also, you could make your code shorter: 

 

// LONG
onEnter: function onEnter() {
  innitFunction();
}

// SHORT
onEnter: innitFunction

Does that help?

  • Thanks 1
Link to comment
Share on other sites

13 hours ago, GreenSock said:

You've got both matchMedia queries almost identical, and they BOTH match anything over 1440px wide: 

"(min-width: 1441px)"
"(min-width: 1440px)"

I think you meant to do this: 

"(min-width: 1441px)"
"(max-width: 1440px)"

Also, you could make your code shorter: 

 

// LONG
onEnter: function onEnter() {
  innitFunction();
}

// SHORT
onEnter: innitFunction

Does that help?

 

Of course, thank you! 🤦‍♂️ I feel very silly now haha. Thanks for pointing that out and the suggestion on the making the code shorter.

 

Cheers,

 

 

 

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