用于自动滚动的偏移锚点

Offset anchors for autoscrolling

本文关键字:滚动 用于      更新时间:2023-09-26

我目前正在使用 css-tricks 中的代码来执行自动滚动功能到我的锚点:

http://css-tricks.com/snippets/jquery/smooth-scrolling/

我需要做什么来偏移"页面顶部"?基本上,我希望页面在距离锚点位置约150px的地方停止。

谢谢!!!!

更改行:

var targetOffset = $target.offset().top;

自:

var targetOffset = $target.offset().top - 150;

这会将其推高到比目标位置顶部高 150 像素的位置。

关于哈希:

更改此内容:location.hash = target;

自:

if(history.pushState) {
    history.pushState(null, null, target);
}
else {
    location.hash = target;
}

搞砸的部分在这里:

location.hash = target;

删除它,并改用它:

history.pushState({}, "", "#"+target);

如果不支持pushState,则可以使用历史记录.js。