fix: close panel when scroll

This commit is contained in:
shinya
2025-08-14 22:52:46 +08:00
parent 2b123d29e0
commit a9c5b9408d
2 changed files with 41 additions and 38 deletions
+7 -3
View File
@@ -111,15 +111,19 @@ const SearchResultFilter: React.FC<SearchResultFilterProps> = ({ categories, val
useEffect(() => {
const handleScroll = () => {
if (activeCategory) calculateDropdownPosition(activeCategory);
// 滚动时直接关闭面板,而不是重新计算位置
if (activeCategory) {
setActiveCategory(null);
}
};
const handleResize = () => {
if (activeCategory) calculateDropdownPosition(activeCategory);
};
window.addEventListener('scroll', handleScroll);
// 监听 body 滚动事件,因为该项目的滚动容器是 document.body
document.body.addEventListener('scroll', handleScroll, { passive: true });
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('scroll', handleScroll);
document.body.removeEventListener('scroll', handleScroll);
window.removeEventListener('resize', handleResize);
};
}, [activeCategory]);