Jump to content
Search Community

Pin element until condition is satisfied

ceribbo test
Moderator Tag

Recommended Posts

 Hi there,

I'm trying to obtain a simple behaviour on my mobile version of the website:

The website will scroll naturally until I scroll down the horizontal section where the user shoud be able to scroll only 1 section at a time, I tried to use the normal horizontal scrolling functionality together with the snap configuration, however the snap is called only when the scroll ends so it will not result in fluid transition.

The result should be as the image below.

 

image.thumb.png.69223b7d93584a9786c0aa24e2ac58db.png

My only solution has been to use the pinned horizontal scrolltrigger together with an observer in order to scroll it section by section. Now the problem is that obviously the container is pinned for a fixed value, so if the user will scroll longer or shorter the end will be reached sooner or later, so I'd like to make something like a conditional end where it ends only when the last section has been reached.

I should also be able to scroll back and this will be another challenge.

Otherwise something to control the scroll power in order to set to a maximum of on section?

My website is deployed on this link https://m1947.micelio.studio/about-us/ , the horizontal section is displayed when clicking on "Expand" (3rd vertical section).

Here is my code at the moment:

const observer = Observer.create({
	type: "wheel,touch,pointer",
	wheelSpeed: -1,
	onDown: () => {
		if (!animatingHorizontal && currentIndex > 0) {
			animatingHorizontal = true;
			currentIndex--;
			gsap.to(".timeline-year-container", {
				x: -100 * currentIndex + "vw",
				duration: scrollDuration / 2,
			})
			setTimeout(() => animatingHorizontal = false, 1000);
		}
	},
	onUp: () => {
		if (!animatingHorizontal) {
			animatingHorizontal = true;
			currentIndex++;
			gsap.to(".timeline-year-container", {
				x: -100 * currentIndex + "vw",
				duration: scrollDuration / 2,
			})
			setTimeout(() => animatingHorizontal = false, 1000);
		}
	},
	tolerance: 10,
	preventDefault: true,
	onEnable: () => allowScroll = false,
	onDisable: (self) => allowScroll = true
});
observer.disable();

scrollTween = gsap.to(".timeline-year-container", {
	// xPercent: "-700",
	ease: "none",
	scrollTrigger: {
		trigger: "#slide-section-about-3",
		pin: "#slide-section-about-3",
		pinSpacing: true,
		scrub: 0,
		top: "top 0%",
		end: () => `+=${$('.timeline-year-container').width()-window.innerWidth}`,

		snap: {
			snapTo: 1 / (sections.length - 1),
			delay: 0,
			duration: 0.3
		},
		onEnter: () => observer.enable(),
		onEnterBack: () => observer.enable(),
		onLeave: () => observer.disable(),
		onLeaveBack: () => observer.disable(),
	}
});

 

Link to comment
Share on other sites

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependencies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

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

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

Hi,

 

I think in theory this could be done but would require quite some bit of custom logic, which is beyond the scope of what we do in these free forums.

 

One alternative would be to mix Observer and ScrollTrigger like in these demos:

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

 

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

 

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