📜  平滑滚动到锚点 - CSS 代码示例

📅  最后修改于: 2022-03-11 14:47:44.908000             🧑  作者: Mango

代码示例4
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
    anchor.addEventListener('click', function (e) {
        e.preventDefault();

        document.querySelector(this.getAttribute('href')).scrollIntoView({
            behavior: 'smooth'
        });
    });
});