Files
gitlab-instance-0a899031_no…/app/components/custom/CustomAnimateOnScroll.tsx
2026-03-10 01:35:37 -05:00

44 lines
1020 B
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
/**
* 自实现滚动入场动画 - 封装待用
* 基于 IntersectionObserver无第三方依赖
*/
import { useEffect, useRef, useState, type ReactNode } from "react";
type Props = {
children: ReactNode;
className?: string;
delay?: number;
};
export function CustomAnimateOnScroll({ children, className = "", delay = 0 }: Props) {
const ref = useRef<HTMLDivElement>(null);
const [visible, setVisible] = useState(false);
useEffect(() => {
const el = ref.current;
if (!el) return;
const observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) setVisible(true);
},
{ threshold: 0.08, rootMargin: "0px 0px -40px 0px" }
);
observer.observe(el);
return () => observer.disconnect();
}, []);
return (
<div
ref={ref}
className={`animate-on-scroll ${visible ? "animate-on-scroll-visible" : ""} ${className}`}
style={{ transitionDelay: `${delay}ms` }}
>
{children}
</div>
);
}