Adds smooth scrolling behavior to internal anchor links and works well for one-page WordPress websites and navigation menus.
document.addEventListener('DOMContentLoaded', function () {
document.querySelectorAll('a[href^="#"]').forEach(function (link) {
link.addEventListener('click', function (event) {
const targetId = link.getAttribute('href');
if (!targetId || targetId === '#') {
return;
}
const target = document.querySelector(targetId);
if (!target) {
return;
}
event.preventDefault();
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
});
});
});