Jump to content
Search Community

ScrollTrigger find center of multiple sections (snapTo)

Sacha test
Moderator Tag

Recommended Posts

Hey, 

I'm trying to create two sections with scrolltrigger events and a snapTo, which snaps to the center of each section. Because it shouldn't be two single events, but the events should seamlessly transition from one to the other, I need to put both sections in one, and target this with a single scrolltrigger.

My idea was, that I use snapTo: [0.25, 0.75], so it would snap to the center of each section. This logic would work if both sections would be of the same height, but as soon as they have different heights, this doesn't indicate their true center.

If there a way to use scrollTrigger and calculate both centers, based on their heights, and using it in the wrapper scrollTrigger? Or do I have to find another way, calculating the centers of each section with normal JS code?

See the Pen by s (@s) on CodePen

Link to comment
Share on other sites

You would need to calculate that yourself. Put all the section heights inside an array, and then try something like this.

 

let heights = [356, 129, 293, 188];
let totalHeight = 0;
let prev = 0;

heights.forEach(height => totalHeight += height);

let snapArray = heights.map((height, i) => {  
  let current = (prev + height / 2) / totalHeight;
  prev += height;
  return current;
});

 

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