📜  javascript 平滑滚动到锚元素 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:16.440000             🧑  作者: Mango

代码示例1
//add smooth scrolling when clicking any anchor link
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
    anchor.addEventListener('click', function (e) {
        e.preventDefault();
        document.querySelector(this.getAttribute('href')).scrollIntoView({
            behavior: 'smooth'
        });
    });
});
// Go to Other Element Smoothly