forked from Rosetta/landing
Исправлен скролл к обьектам
This commit is contained in:
@@ -5,15 +5,42 @@ export function ScrollToTop() {
|
||||
const { pathname, hash } = useLocation();
|
||||
|
||||
useEffect(() => {
|
||||
const frame = window.requestAnimationFrame(() => {
|
||||
if (hash) {
|
||||
const target = document.getElementById(decodeURIComponent(hash.slice(1)));
|
||||
if (target) {
|
||||
target.scrollIntoView();
|
||||
return;
|
||||
}
|
||||
if (hash) {
|
||||
let targetId: string;
|
||||
|
||||
try {
|
||||
targetId = decodeURIComponent(hash.slice(1));
|
||||
} catch {
|
||||
targetId = hash.slice(1);
|
||||
}
|
||||
|
||||
const scrollToTarget = () => {
|
||||
const target = document.getElementById(targetId);
|
||||
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
|
||||
target.scrollIntoView({ block: 'start' });
|
||||
return true;
|
||||
};
|
||||
|
||||
if (scrollToTarget()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const observer = new MutationObserver(() => {
|
||||
if (scrollToTarget()) {
|
||||
observer.disconnect();
|
||||
}
|
||||
});
|
||||
|
||||
observer.observe(document.body, { childList: true, subtree: true });
|
||||
|
||||
return () => observer.disconnect();
|
||||
}
|
||||
|
||||
const frame = window.requestAnimationFrame(() => {
|
||||
window.scrollTo({ top: 0, left: 0, behavior: 'instant' });
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user