Jump to content
Search Community

How to access scroll position when using ScrollSmoother

Levin Riegner test
Moderator Tag

Go to solution Solved by Levin Riegner,

Recommended Posts

I need to access the scroll position.

 

I have a function that looks to when the user scrolls 100px then fires a function, and the same on scroll back up. However ofcourse window doesnt work.

 

How do you access this in ScrollSMoother?

Heres my current code, that doesnt work.

 

// NOTE Before render, calculate smooth scrolling
useLayoutEffect(() => {
  const ctx = gsap.context(() => {
    smoother.current = ScrollSmoother.create({
      smooth: 2, // seconds it takes to "catch up" to native scroll position
      effects: true, // look for data-speed and data-lag attributes on elements and animate accordingly
      onUpdate: update
    });
  }, main);

  return () => ctx.revert();
}, []);

const update = () => {
  // Get scrollY position
  const scrollY = smoother.current.scroll.y; // Scroll doesnt exist
  console.log(scrollY);
};

 

Link to comment
Share on other sites

  • Solution

Fixed!

I needed to do this instead:
 

// NOTE Before render, calculate smooth scrolling
useLayoutEffect(() => {
  const ctx = gsap.context(() => {
    smoother.current = ScrollSmoother.create({
      smooth: 2, // seconds it takes to "catch up" to native scroll position
      effects: true, // look for data-speed and data-lag attributes on elements and animate accordingly
      onUpdate: (self) => {
        let scrollY = self.scrollTop();

        console.log(scrollY);
      }
    });
  }, main);

  return () => ctx.revert();
}, []);

 

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