mirror of
https://github.com/DerTyp7/dertyp7.github.io.git
synced 2025-10-29 12:52:08 +01:00
Add hash change listener to update current section index
This commit is contained in:
@@ -1,9 +1,25 @@
|
|||||||
import { useState } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import "@styles/SectionScroll.scss";
|
import "@styles/SectionScroll.scss";
|
||||||
|
|
||||||
export default function SectionScroll({ sections }: { sections: string[] }) {
|
export default function SectionScroll({ sections }: { sections: string[] }) {
|
||||||
const [currentSectionIndex, setCurrentSectionIndex] = useState(0);
|
const [currentSectionIndex, setCurrentSectionIndex] = useState(0);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const hash = window.location.hash.substring(1);
|
||||||
|
const index = sections.indexOf(hash);
|
||||||
|
if (index !== -1) {
|
||||||
|
setCurrentSectionIndex(index);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
window.onhashchange = () => {
|
||||||
|
const hash = window.location.hash.substring(1);
|
||||||
|
const index = sections.indexOf(hash);
|
||||||
|
if (index !== -1) {
|
||||||
|
setCurrentSectionIndex(index);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
function scrollTo(direction: number) {
|
function scrollTo(direction: number) {
|
||||||
let newIndex = currentSectionIndex + direction;
|
let newIndex = currentSectionIndex + direction;
|
||||||
if (newIndex < 0) newIndex = sections.length - 1;
|
if (newIndex < 0) newIndex = sections.length - 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user