forked from Rosetta/landing
Исправлен скролл к обьектам
This commit is contained in:
@@ -5,15 +5,42 @@ export function ScrollToTop() {
|
|||||||
const { pathname, hash } = useLocation();
|
const { pathname, hash } = useLocation();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const frame = window.requestAnimationFrame(() => {
|
if (hash) {
|
||||||
if (hash) {
|
let targetId: string;
|
||||||
const target = document.getElementById(decodeURIComponent(hash.slice(1)));
|
|
||||||
if (target) {
|
try {
|
||||||
target.scrollIntoView();
|
targetId = decodeURIComponent(hash.slice(1));
|
||||||
return;
|
} 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' });
|
window.scrollTo({ top: 0, left: 0, behavior: 'instant' });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user