Jump to content
Search Community

ScrollSmoother & matchMedia

Marc Storch test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

Hi,

I am trying to create a desktop only smooth scroll enabled website.

Everything works as expected. The only problem is the construction/deconstruction of smoothscroll when resizing the window (below 768px and back).

Scrolling the website suddenly stops working.

 

I don't know what I am doing wrong.

 

Thanks in advance!

See the Pen zYQEzKq by Marc-Storch (@Marc-Storch) on CodePen

Link to comment
Share on other sites

  • Marc Storch changed the title to ScrollSmoother & matchMedia
  • Solution

Hi @Marc Storch and welcome to the GSAP Forums!

 

I fiddled with your demo for a bit and can't seem to replicate the issue, the only  thing I did notice is your HTML structure and your ScrollSmoother config object. By default ScrollSmoother will look for this HTML structure:

<div id="smooth-wrapper">
  <div id="smooth-content">
    <!--- ALL YOUR CONTENT HERE --->
  </div>
</div>

But yours is like this:

<div class="page">
  <!-- CONTENT HERE -->
</div>

So you need to set a content target in your ScrollSmoother config:

ScrollSmoother.create({
  smooth: 1,
  effects: true,
  smoothTouch: 0.1,
  content: ".page",
});

Here is a fork of your demo:

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

 

Finally there is no need for this in the return callback of GSAP MatchMedia:

mm.add("(min-width: 768px)", () => {

  ScrollSmoother.create({
    smooth: 1,
    effects: true,
    smoothTouch: 0.1,
  });

  return () => {
    const smooth = ScrollSmoother.get();
    if(smooth) ScrollSmoother.get().kill();
  };
});

Since GSAP MatchMedia is a wrapper for a GSAP Context instance, so it'll pick all the GSAP instances inside and revert them automatically for you when the breakpoint is passed.

 

Hopefully this helps.

Happy Tweening!

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