首页 > 解决方案 > 在滚动动画中将锚链接移高一点

问题描述

我为自己的网站使用锚链接。并且有滚动动画。但是因为标题是粘性的,所以它隐藏了标题。所以我想去锚链接去的更高的地方。可能吗?

HTML:

<li><a href="#example">Example</a></li>

JS:

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

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

标签: javascripthtmljquery-animateanchor-scroll

解决方案


推荐阅读