gpt4 book ai didi

javascript - 如何向jquery scrolltop添加偏移量

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:25:22 25 4
gpt4 key购买 nike

我正在使用以下代码在 Bootstrap 的导航栏中从菜单链接滚动动画:

$("#subnavbar ul li a[href^='#']").on('click', function(e) {
e.preventDefault();
$('html, body').animate({ scrollTop: $(this.hash).offset().top }, 600);

// edit: Opera and IE requires the "html" elm. animated
});

目前,固定导航栏将 anchor 隐藏在下方。我如何添加 60px 的偏移量来对此进行调整?

最佳答案

您需要从目标元素的 offset().top 中减去 60,以便为导航栏留出空间。我通过获取 #subnavbarheight() 动态完成此操作,因此如果您将来需要更改它的高度,则不需要担心破坏此代码。

$("#subnavbar ul li a[href^='#']").on('click', function(e) {
e.preventDefault();
$('html, body').animate({
scrollTop: $(this.hash).offset().top - $('#subnavbar').height()
}, 600);
});

关于javascript - 如何向jquery scrolltop添加偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18289184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com