Jump to content
Search Community

Match Media + event listener

kabocreative test
Moderator Tag

Go to solution Solved by Cassie,

Recommended Posts

Hi all!

 

I need to hide a section entirely on narrower screen widths, including killing the animation to avoid wasted resources on something you can't see.

 

I've stumbled across the lovely new match media, which looks just the thing.

 

I've successfully applied it on a simplified version of my animation by surrounding the entire timeline in a min-width match media: 

 

[codepen not displaying in place for some reason - see simpler codepen titled GSAP forum: simple timeline matchmedia test which is showing at bottom of this post]

 

My more complex animation this quite working..

 

Again I've wrapped a timeline (t1) in a min-width match media query and that is working as expected. The animation does not play below 1200px. However I'm getting an error in my event listener for the mousenter hover effect:

 

Quote

Uncaught ReferenceError: t1 is not defined

 

This I kind of get, as now I guess t1 is only defined for widths above 800px due to match media, but it means the whole mousenter instructions aren't playing even above 800px.

 

See the Pen jOzjRjz by kabocreative (@kabocreative) on CodePen

 

I've tried wrapping the whole lot in a matchmedia query, but that results in nothing playing at any screen size.

 

Pointers always appreciated :) Thanks!



 

See the Pen mdxZgYr by kabocreative (@kabocreative) on CodePen

Link to comment
Share on other sites

Can you give me your original pen without any matchMedia stuff in it? 

 

You've got a matchMedia inside a matchMedia here. That's not valid. You don't need to wrap every timeline.

 

I've tried to unpick it but I'm not sure what it's meant to look like originially.

  • Like 1
Link to comment
Share on other sites

Here's a 'how-to' and a 'why'

Video explanation -

Codepen demo 

See the Pen OJvwJge by GreenSock (@GreenSock) on CodePen



But I'm not quite sure how to manage the scope of your event listeners in order to remove them in the cleanup function.

You can say 'only run once' on an eventListener though, so maybe that's a solution?

See the Pen qBozzey?editors=0011 by GreenSock (@GreenSock) on CodePen

  • Like 1
Link to comment
Share on other sites

Yep - thanks for the video. I watched that today, match media looks ace and I've happily been going through a few animations using scrolltrigger matchmedia and updating to the newer match media, perfect.

 

I'm stuck where you've mentioned.. it's the event listeners! I've no idea what to do with that. Perhaps it's not something match media should be covering and I'm trying to make it do something it shouldn't be?

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