Fixed issues
This commit is contained in:
@@ -55,77 +55,3 @@ export function useScrollAnimation<T extends HTMLElement = HTMLDivElement>(
|
||||
reset,
|
||||
};
|
||||
}
|
||||
|
||||
export function useCountUp(
|
||||
end: number,
|
||||
duration: number = 2000,
|
||||
start: number = 0,
|
||||
) {
|
||||
const [count, setCount] = useState(start);
|
||||
const [isAnimating, setIsAnimating] = useState(false);
|
||||
const countRef = useRef(start);
|
||||
|
||||
const startAnimation = useCallback(() => {
|
||||
if (isAnimating) return;
|
||||
|
||||
setIsAnimating(true);
|
||||
const startTime = Date.now();
|
||||
const range = end - start;
|
||||
|
||||
const animate = () => {
|
||||
const elapsed = Date.now() - startTime;
|
||||
const progress = Math.min(elapsed / duration, 1);
|
||||
|
||||
const easeOut = 1 - Math.pow(1 - progress, 3);
|
||||
const currentCount = Math.floor(start + range * easeOut);
|
||||
|
||||
countRef.current = currentCount;
|
||||
setCount(currentCount);
|
||||
|
||||
if (progress < 1) {
|
||||
requestAnimationFrame(animate);
|
||||
} else {
|
||||
setCount(end);
|
||||
setIsAnimating(false);
|
||||
}
|
||||
};
|
||||
|
||||
requestAnimationFrame(animate);
|
||||
}, [end, duration, start, isAnimating]);
|
||||
|
||||
const reset = useCallback(() => {
|
||||
setCount(start);
|
||||
setIsAnimating(false);
|
||||
}, [start]);
|
||||
|
||||
return { count, startAnimation, reset, isAnimating };
|
||||
}
|
||||
|
||||
export function useParallax(speed: number = 0.5) {
|
||||
const ref = useRef<HTMLDivElement>(null);
|
||||
const [offset, setOffset] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
if (typeof window === "undefined") return;
|
||||
|
||||
const handleScroll = () => {
|
||||
if (!ref.current) return;
|
||||
|
||||
const rect = ref.current.getBoundingClientRect();
|
||||
const scrolled = window.scrollY;
|
||||
const elementTop = rect.top + scrolled;
|
||||
const relativeScroll = scrolled - elementTop + window.innerHeight;
|
||||
|
||||
setOffset(relativeScroll * speed * 0.1);
|
||||
};
|
||||
|
||||
window.addEventListener("scroll", handleScroll, { passive: true });
|
||||
handleScroll();
|
||||
|
||||
return () => {
|
||||
window.removeEventListener("scroll", handleScroll);
|
||||
};
|
||||
}, [speed]);
|
||||
|
||||
return { ref, offset };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user