jQuery滚动偏移,因为标题固定

jQuery scroll offset because of fixed header

本文关键字:标题 因为 滚动 jQuery      更新时间:2023-09-26

我有下面的jQuery脚本来向下滚动页面。

$(document).ready(function() {
$('a[href^=#]').on('click', function(e){
    var href = $(this).attr('href');
    $('html, body').animate({
        scrollTop:$(href).offset().top
    },1500);
    e.preventDefault();
    });
});

页面有一个固定的标题,高度为100px。显然,我需要向下滚动100px,这样标题就不会覆盖标题。我在谷歌上搜索了一下,我需要在某个地方输入"{offset:-100}"。。但是在哪里?

只需从scrollTop的数量中减少标题高度。

 $('html, body').animate({
   scrollTop: $(href).offset().top - $("header").outerHeight() + "px"
 }, 1500);