Image flickering problem in ScrollStack of tsx and tailwind version #517
Unanswered
ronishpaudel
asked this question in
help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
`import React, { ReactNode, useLayoutEffect, useRef, useCallback } from "react";
import Lenis from "lenis";
export interface ScrollStackItemProps {
itemClassName?: string;
children: ReactNode;
}
export const ScrollStackItem: React.FC = ({
children,
itemClassName = "",
}) => (
interface ScrollStackProps {
className?: string;
children: ReactNode;
itemDistance?: number;
itemScale?: number;
itemStackDistance?: number;
stackPosition?: string;
scaleEndPosition?: string;
baseScale?: number;
scaleDuration?: number;
rotationAmount?: number;
blurAmount?: number;
useWindowScroll?: boolean;
onStackComplete?: () => void;
}
const ScrollStack: React.FC = ({
children,
className = "",
itemDistance = 100,
itemScale = 0.03,
itemStackDistance = 30,
stackPosition = "20%",
scaleEndPosition = "10%",
baseScale = 0.85,
scaleDuration = 0.5,
rotationAmount = 0,
blurAmount = 0,
useWindowScroll = false,
onStackComplete,
}) => {
const scrollerRef = useRef(null);
const stackCompletedRef = useRef(false);
const animationFrameRef = useRef<number | null>(null);
const lenisRef = useRef<Lenis | null>(null);
const cardsRef = useRef<HTMLElement[]>([]);
const lastTransformsRef = useRef(new Map<number, any>());
const isUpdatingRef = useRef(false);
const calculateProgress = useCallback(
(scrollTop: number, start: number, end: number) => {
if (scrollTop < start) return 0;
if (scrollTop > end) return 1;
return (scrollTop - start) / (end - start);
},
[]
);
const parsePercentage = useCallback(
(value: string | number, containerHeight: number) => {
if (typeof value === "string" && value.includes("%")) {
return (parseFloat(value) / 100) * containerHeight;
}
return parseFloat(value as string);
},
[]
);
const getScrollData = useCallback(() => {
if (useWindowScroll) {
return {
scrollTop: window.scrollY,
containerHeight: window.innerHeight,
scrollContainer: document.documentElement,
};
} else {
const scroller = scrollerRef.current;
return {
scrollTop: scroller ? scroller.scrollTop : 0,
containerHeight: scroller ? scroller.clientHeight : 0,
scrollContainer: scroller,
};
}
}, [useWindowScroll]);
const getElementOffset = useCallback(
(element: HTMLElement) => {
if (useWindowScroll) {
const rect = element.getBoundingClientRect();
return rect.top + window.scrollY;
} else {
return element.offsetTop;
}
},
[useWindowScroll]
);
const updateCardTransforms = useCallback(() => {
if (!cardsRef.current.length || isUpdatingRef.current) return;
}, [
itemScale,
itemStackDistance,
stackPosition,
scaleEndPosition,
baseScale,
rotationAmount,
blurAmount,
useWindowScroll,
onStackComplete,
calculateProgress,
parsePercentage,
getScrollData,
getElementOffset,
]);
const isAnimating = useRef(false);
const handleScroll = useCallback(() => {
if (!isAnimating.current) {
isAnimating.current = true;
requestAnimationFrame(() => {
updateCardTransforms();
isAnimating.current = false;
});
}
}, [updateCardTransforms]);
const setupLenis = useCallback(() => {
if (useWindowScroll) {
const lenis = new Lenis({
duration: 1.2,
easing: (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
smoothWheel: true,
touchMultiplier: 2,
infinite: false,
wheelMultiplier: 1,
lerp: 0.1,
syncTouch: true,
syncTouchLerp: 0.075,
});
lenis.on("scroll", handleScroll);
}, [handleScroll, useWindowScroll]);
useLayoutEffect(() => {
if (!useWindowScroll && !scrollerRef.current) return;
}, [
itemDistance,
itemScale,
itemStackDistance,
stackPosition,
scaleEndPosition,
baseScale,
scaleDuration,
rotationAmount,
blurAmount,
useWindowScroll,
onStackComplete,
setupLenis,
updateCardTransforms,
]);
return (
<div
className={
relative w-full h-full overflow-y-auto overflow-x-visible ${className}
.trim()}ref={scrollerRef}
style={{
overscrollBehavior: "contain",
WebkitOverflowScrolling: "touch",
scrollBehavior: "smooth",
WebkitTransform: "translateZ(0)",
transform: "translateZ(0)",
willChange: "scroll-position",
}}
>
{children}
{/* Spacer so the last pin can release cleanly */}
);
};
export default ScrollStack;`
this is my scrollstack version and not much issue and im using it to display image in here "use client";
`import React from "react";
import ScrollStack, { ScrollStackItem } from "./ScrollStack";
import { Button } from "./ui/button";
import { ArrowRight } from "lucide-react";
import Image from "next/image";
export const ImageStack: React.FC<{ className?: string }> = ({ className }) => {
return (
Start Learning Today
Master every part of React and Next.js
Learn the tools companies actually hire for with real-world projects,
step-by-step guidance, and zero confusion.
Start Building Free Today
<Image
src="/static/img/pic1.jpeg"
alt="React Course Preview"
className="object-cover shadow-lg"
width={1920}
height={1080}
style={{
transform: "translateZ(0)",
backfaceVisibility: "hidden",
willChange: "transform",
borderRadius: "40px",
}}
priority
/>
);
![Uploading flicker.jpeg…]()
};`
but when i scroll up or down i face image flickering issue if someone faced issue like this hlepmme
Beta Was this translation helpful? Give feedback.
All reactions