我有一个平滑的滚动功能,用于在点击链接时滚动到页面的特定部分。然而,当它自动滚动到一个部分时,它不会考虑到我固定导航栏的高度,这最终会覆盖一些内容。我想要计算导航栏的高度,并从当前函数中的滚动位置减去它
$(function() {
$('a[href*="#"]:not(.carousel-control)').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});发布于 2017-05-29 10:01:07
您需要将导航栏的高度添加到scrollTop: target.offset().top。
下面是一个示例,假设导航栏的类为navbar
scrollTop: target.offset().top + $('.navbar').height()希望这能有所帮助!:)
https://stackoverflow.com/questions/44233710
复制相似问题