function smoothScrollToAnchor(anchorId) {
const target = document.getElementById(anchorId);
if (target) {
if (window.requestAnimationFrame) {
let start = null;
const scroll = (timestamp) => {
if (!start) start = timestamp;
const progress = timestamp - start;
const easeInOutQuad = (t) => t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
const scrollTop = target.offsetTop * easeInOutQuad(progress / 1000);
window.scrollTo(0, scrollTop);
if (progress < 1000) {
window.requestAnimationFrame(scroll);
}
};
window.requestAnimationFrame(scroll);
} else {
const targetTop = target.offsetTop;
const step = targetTop / 20;
let currentTop = window.pageYOffset;
const timer = setInterval(() => {
currentTop += step;
if ((step > 0 && currentTop >= targetTop) || (step < 0 && currentTop <= targetTop)) {
clearInterval(timer);
window.scrollTo(0, targetTop);
} else {
window.scrollTo(0, currentTop);
}
}, 50);
}
}
}