Jump to content
Search Community

Difference between local and production environment.

HaCa test
Moderator Tag

Go to solution Solved by GSAP Helper,

Recommended Posts

Hello,

I created an infinite vertical scroll like effect for my cards, it took a while but with the helps of forums and some demos, it worked perfectly for me on local.

However, when I check production, I see that it's not working properly, cards are appearing small and origin seems like below, some cards get bigger some are not, and most ambiguous one is if i scroll up, animation works properly for like 4-5 cards, but when i scroll down card disappears immediately.
I deploy with vercel and there is no club plugin as you can see.

Can you please enlighten me about the issue? I suspected with some css conflict but couldn't find a way to solve it. It took my whole day and decided to ask some help here.

Purpose of animation ( in case there is a much more basic or consistent way to do this )
-Avoid page scroll over container and avoid animation work when page is scrolled outside  card container.
-Give a seamless vertical scroll effect without showing any scroll bar.

Stack:
- Next14, Typescript, Vercel for deployment

Thank you for your help.

 



```

import React, { useEffect } from "react";
import { gsap } from "gsap";
import { IWorkOption, workOptions } from "../../utilities";
import styles from "./card-container.module.css";

 
const scrollSpeedMultiplier = 0.0001;
 
const CardContainer = ({ setItemSelected }: { setItemSelected: React.Dispatch<React.SetStateAction<IWorkOption>> }) => {
useEffect(() => {
const cards = Array.from(document.querySelectorAll(".single-one-pair"));
const spacing = 0.1;
const seamlessLoop = buildSeamlessLoop(cards, spacing, animateCard);
const playhead = { offset: 0 };
const scrub = gsap.to(playhead, {
offset: 0,
onUpdate: () => {
seamlessLoop.time(wrapTime(-playhead.offset));
},
duration: 0.5,
ease: "power3",
paused: true,
});
 
function animateCard(element: Element) {
const tl = gsap.timeline();
 
tl.fromTo(element, { scaleX: 0.8, scaleY: 0.1 }, { scaleX: 1, scaleY: 0.9, opacity: 1, duration: 0.5, yoyo: true, repeat: 1, ease: "power1.in", immediateRender: false }).fromTo(
element,
{ yPercent: -400 },
{ yPercent: 400, duration: 1, ease: "none", immediateRender: false },
0
);
 
return tl;
}
 
function buildSeamlessLoop(items: Element[], spacing: number, animateFunc: (element: Element) => gsap.core.Timeline) {
let rawSequence = gsap.timeline({ paused: true }),
seamlessLoop = gsap.timeline({ paused: true });
 
items
.concat(items)
.concat(items)
.concat(items)
.forEach((item, i) => {
let anim = animateFunc(items[i % items.length]);
rawSequence.add(anim, i * spacing);
});
 
seamlessLoop.fromTo(rawSequence, { time: spacing * items.length }, { time: `+=${spacing * items.length}`, duration: spacing * items.length, ease: "none" });
return seamlessLoop;
}
 
function wrapTime(offset: number) {
return gsap.utils.wrap(0, seamlessLoop.duration())(offset);
}
 
const ulElement = document.querySelector(".pair-container");
 
function handleMouseEnter() {
ulElement?.addEventListener("wheel", handleWheel as EventListener);
ulElement?.addEventListener("touchstart", handleTouchStart as EventListener);
ulElement?.addEventListener("touchmove", handleTouchMove as EventListener);
ulElement?.addEventListener("touchend", handleTouchEnd as EventListener);
scrub.play(); // Start the animation when the mouse enters
}
 
function handleMouseLeave() {
ulElement?.removeEventListener("wheel", handleWheel as EventListener);
ulElement?.removeEventListener("touchstart", handleTouchStart as EventListener);
ulElement?.removeEventListener("touchmove", handleTouchMove as EventListener);
ulElement?.removeEventListener("touchend", handleTouchEnd as EventListener);
scrub.pause(); // Pause the animation when the mouse leaves
}
 
function handleWheel(event: WheelEvent) {
event.stopPropagation();
event.preventDefault();
console.log("Wheel event deltaY:", event.deltaY); // Log deltaY for debugging
scrub.vars.offset += event.deltaY * scrollSpeedMultiplier; // Adjust scroll speed as necessary
scrub.invalidate().restart(false);
}
 
let touchStartY = 0;
 
function handleTouchStart(event: TouchEvent) {
touchStartY = event.touches[0].clientY;
}
 
function handleTouchMove(event: TouchEvent) {
const touchEndY = event.touches[0].clientY;
const deltaY = touchStartY - touchEndY;
touchStartY = touchEndY; // Update the start position for the next move
console.log("Touch move deltaY:", deltaY); // Log deltaY for debugging
scrub.vars.offset += deltaY * scrollSpeedMultiplier; // Adjust scroll speed as necessary
scrub.invalidate().restart();
event.preventDefault();
}
 
function handleTouchEnd(event: TouchEvent) {
touchStartY = 0; // Reset the touch start position
}
 
ulElement?.addEventListener("mouseenter", handleMouseEnter);
ulElement?.addEventListener("mouseleave", handleMouseLeave);
 
return () => {
ulElement?.removeEventListener("mouseenter", handleMouseEnter);
ulElement?.removeEventListener("mouseleave", handleMouseLeave);
ulElement?.removeEventListener("wheel", handleWheel as EventListener);
ulElement?.removeEventListener("touchstart", handleTouchStart as EventListener);
ulElement?.removeEventListener("touchmove", handleTouchMove as EventListener);
ulElement?.removeEventListener("touchend", handleTouchEnd as EventListener);
gsap.killTweensOf(playhead);
};
}, []);
 
return (
<div className={styles.body}>
<ul className="pair-container" style={{ width: "100%", height: "100%", display: "flex", alignItems: "center" }}>
{workOptions.map((card, i) => (
<li
key={i}
className="single-one-pair"
style={{ listStyle: "none", padding: 0, margin: 0, width: "100%", height: "80%", position: "absolute", display: "flex", justifyContent: "center", alignItems: "center" }}
>
<div key={i} className={styles.pairItem} style={{ backgroundColor: "red" }} onClick={() => setItemSelected(card)}>
<div className={styles.overlay}>
<h2 className={styles.overlayText}>{i}</h2>
</div>
</div>
</li>
))}
</ul>
</div>
);
};
 
export default CardContainer;

```

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

  • Solution

Hi there! I see you're using React -

Proper cleanup is very important with frameworks, but especially with React. React 18 runs in strict mode locally by default which causes your useEffect() and useLayoutEffect() to get called TWICE.

 

Since GSAP 3.12, we have the useGSAP() hook (the NPM package is here) that simplifies creating and cleaning up animations in React (including Next, Remix, etc). It's a drop-in replacement for useEffect()/useLayoutEffect(). All the GSAP-related objects (animations, ScrollTriggers, etc.) created while the function executes get collected and then reverted when the hook gets torn down.

 

Here is how it works:

const container = useRef(); // the root level element of your component (for scoping selector text which is optional)

useGSAP(() => {
  // gsap code here...
}, { dependencies: [endX], scope: container }); // config object offers maximum flexibility

Or if you prefer, you can use the same method signature as useEffect():

useGSAP(() => {
  // gsap code here...
}, [endX]); // simple dependency Array setup like useEffect()

This pattern follows React's best practices.

 

We strongly recommend reading the React guide we've put together at https://gsap.com/resources/React/

 

If you still need help, here's a React starter template that you can fork to create a minimal demo illustrating whatever issue you're running into. Post a link to your fork back here and we'd be happy to take a peek and answer any GSAP-related questions you have. Just use simple colored <div> elements in your demo; no need to recreate your whole project with client artwork, etc. The simpler the better. 

  • Like 1
Link to comment
Share on other sites

Hello again, 
It's been very helpful. After I replaced useEffect with useGSAP, I realized it's also not working on local properly, it was a visual illusion. I cleaned the code and solved the issue.
Thanks for your help 🙏

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