mirror of
https://github.com/labring/FastGPT.git
synced 2025-08-01 20:27:45 +00:00
v4.6.1 (#497)
This commit is contained in:
28
projects/app/src/web/common/hooks/useSticky.ts
Normal file
28
projects/app/src/web/common/hooks/useSticky.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
|
||||
export function useSticky(props?: { threshold?: number }) {
|
||||
const { threshold = 20 } = props || {};
|
||||
const parentRef = useRef<HTMLDivElement>(null);
|
||||
const divRef = useRef<HTMLDivElement>(null);
|
||||
const [isSticky, setIsSticky] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const cb = () => {
|
||||
if (!divRef.current) return;
|
||||
const rect = divRef.current.getBoundingClientRect();
|
||||
const isSticky = rect.top <= threshold;
|
||||
setIsSticky(isSticky);
|
||||
};
|
||||
parentRef.current?.addEventListener('scroll', cb);
|
||||
|
||||
return () => {
|
||||
parentRef.current?.removeEventListener('scroll', cb);
|
||||
};
|
||||
}, [threshold]);
|
||||
|
||||
return {
|
||||
parentRef,
|
||||
divRef,
|
||||
isSticky
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user