File: /var/www/quadcode/frontend/src/js/components/ScrollAnchor.js
const ScrollAnchor = () => {
document.querySelectorAll('a[href^="#"]').forEach((anchor) => {
anchor.addEventListener("click", function (e) {
const href = this.getAttribute("href");
if (href === "#" || !href) return;
const target = document.querySelector(href);
if (target) {
e.preventDefault();
target.scrollIntoView({
behavior: "smooth",
block: "start",
});
}
});
});
const url = window.location;
if (url.hash) {
const hash = url.hash.replace("#", "");
const block = document.getElementById(hash);
if (block) {
setTimeout(() => {
block.scrollIntoView({ block: "start", behavior: "smooth" });
}, 100);
}
}
};
export default ScrollAnchor;