如何修改我的脚本,使其比我的目标DIV高出50px。这是因为我在屏幕顶部有一个固定的导航栏,宽度为50px,并希望将其考虑在内。
$(document).ready(function(){
$("a").on('click', function(event) {
if (this.hash !== "") {
event.preventDefault();
var hash = this.hash;
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function(){
window.location.hash = hash;
});
}
});
});
答案 0 :(得分:1)
试试这个在我的案例中起作用
scrollTop: $(hash).offset().top - 50;
其中50是你头的高度。相反,您也可以使用
scrollTop: $(hash).offset().top - $(navbarSelector).height();