Jump to content
Search Community

Multiple Scrolltrigger items on one page

the_gmo test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

I'm trying to achieve an effect where a container stops in the center of the screen until all its content has scrolled through. As long as there is only one such element on a page it works great. However, I will need several of those elements on the same page and can't figure out how to do it.

Any pointers what I need to change to enable this effect for multiple elements would be greatly appreciated.
 

var autoscroll = gsap.utils.toArray(".autoscrollContainer");
    
autoscroll.forEach(panel => {

    var ratio = panel.querySelector(".ratio");
    var ratioHeight = ratio.offsetHeight;

    var image = panel.querySelector(".scroll-image");
    var imageHeight = image.offsetHeight;

    var difference = imageHeight - ratioHeight;
    var distance = (difference / imageHeight) * -100;

    var tl = gsap.timeline({
    	scrollTrigger: {
        	trigger: autoscroll,
            start: "center center",
            end: "+=3500",
            scrub: true,
            snap: 0,
            pin: true,
            //markers: true
        }
    });
  
    tl.to(image, {
    	yPercent: distance,
    });
});

 

See the Pen yLEBVzy by the_gmo (@the_gmo) on CodePen

Link to comment
Share on other sites

  • Solution

Hi,

 

The issue is that you are setting the array of elements as the trigger for each ScrollTrigger instance:

var autoscroll = gsap.utils.toArray(".autoscrollContainer"); // <- Array
    
autoscroll.forEach(panel => {
  var tl = gsap.timeline({
    scrollTrigger: {
      trigger: autoscroll, // <- SAME ARRAY
      start: "center center",
      end: "+=3500",
      scrub: true,
      snap: 0,
      pin: true,
      //markers: true
    }
  });
});

The trigger element can't be, for obvious logical reasons, more than one element. This seems to work the way you expect:

var autoscroll = gsap.utils.toArray(".autoscrollContainer");
    
autoscroll.forEach(panel => {

  var ratio = panel.querySelector(".ratio");
  var ratioHeight = ratio.offsetHeight;

  var image = panel.querySelector(".scroll-image");
  var imageHeight = image.offsetHeight;

  var difference = imageHeight - ratioHeight;
  var distance = (difference / imageHeight) * -100;

  var tl = gsap.timeline({
    scrollTrigger: {
      trigger: panel,
      start: "center center",
      end: "+=3500",
      scrub: true,
      snap: 0,
      pin: true,
      markers: true,
    }
  });

  tl.to(image, {
    yPercent: distance,
  });

});

Let us know if you have more questions.

 

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