-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lllzhmio
committed
Nov 12, 2024
1 parent
4401c01
commit 9cfb791
Showing
5 changed files
with
52 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useEffect } from 'react'; | ||
|
||
export function useScrollToMark( | ||
selectedSentence: { paragraphId: number; sentId: number } | null, | ||
selector: string, | ||
containerSelector: string | ||
) { | ||
useEffect(() => { | ||
if (!selectedSentence) return; | ||
|
||
const container = document.querySelector(containerSelector) as HTMLElement; | ||
if (!container) return; | ||
|
||
const elements = container.querySelectorAll(selector); | ||
elements.forEach(el => { | ||
const element = el as HTMLElement; // 添加类型断言 | ||
const paragraphId = Number(element.getAttribute('data-paragraph-id')); | ||
const sentId = Number(element.getAttribute('data-sent-id')); | ||
|
||
if (selectedSentence.paragraphId === paragraphId && | ||
selectedSentence.sentId === sentId) { | ||
// 计算元素相对于容器的位置 | ||
const elementTop = element.offsetTop; | ||
const containerHeight = container.clientHeight; | ||
const scrollTop = elementTop - containerHeight / 2 + element.offsetHeight / 2; | ||
|
||
container.scrollTo({ | ||
top: scrollTop, | ||
behavior: 'smooth' | ||
}); | ||
} | ||
}); | ||
}, [selectedSentence]); | ||
} |