Hi everyone,
I can't provide a CodePen example, since a work in progress and too complex. The problem only appears on mobile (iPhone Safari), Desktop and even iPad works fine. But I think my explanation of the problem might already be enough:
I have a one-pager, where I use GSAP together with ScrollTrigger and ScrollSmoother. I have several paragraphs with a headline and some text. Currently, I pin each headline to the top, as soon as it gets there. In the end value, I define how long it should be (until the complete paragraph of text below it has scrolled above it).
I have this code for every headline, with its own classes and start / stop values. The start / stop value is set depending on the current device.
gsap.to(".headline_sortiment", {
scrollTrigger: {
trigger: ".headline_sortiment",
start: (() => {
if (deviceDesktop) {
return "top 0px";
} else if (deviceTablet) {
return "top 0px";
} else if (deviceMobile) {
return "top 0px";
}
})(),
end: (() => {
if (deviceDesktop) {
return "4000px top";
} else if (deviceTablet) {
return "4000px top";
} else if (deviceMobile) {
return "4000px top";
}
})(),
pin: true,
pinSpacing: false,
scrub: true,
onEnter: () => {
const headlineSortiment = document.querySelector(".headline_sortiment");
headlineSortiment.style.background = "linear-gradient(0deg, rgba(255,255,255,0) 0%, rgba(0,0,0,0.8407738095238095) 46%)";
},
onLeave: () => {
const headlineSortiment = document.querySelector(".headline_sortiment");
headlineSortiment.style.background ="linear-gradient(0deg, rgba(255,255,255,0) 0%, rgba(0,0,0,0.8407738095238095) 46%)";
},
onLeaveBack: () => {
const headlineSortiment = document.querySelector(".headline_sortiment");
headlineSortiment.style.background = "transparent";
},
},
});
My problem is the following: When I'm on mobile view and scroll down, the first headline pins fine, but the second one (code above) starts pinning about 5-600px below the top of the window.
This happens for every headline following as well. I guess it has something to do with the mobile calculation of the document height. When I scroll to the first headline and wait until it sticks (wrongly) around 600px from the top, and then enter ScrollTrigger.refresh() into my JS Console, it instantly places the headline where it should be.
This works also for every other headline. Is there any way to programmatically refresh everything once the headlines come into view? I guess that could solve my problem. I tried creating an event listener and refresh after every scroll, but that's of course no viable option.
Any help is greatly appreciated. Thank you!