【问题标题】:jQuery scroll offset because of fixed header由于固定标题,jQuery滚动偏移
【发布时间】:2014-09-12 10:03:00
【问题描述】:

我有以下 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();
    });
});

页面有一个高度为 100 像素的固定页眉。显然我需要向下滚动 100 像素,所以标题不会覆盖标题。 我用谷歌搜索,我需要在某处输入“{offset: -100}”.. 但是在哪里?

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-01
      • 1970-01-01
      • 2023-03-16
      • 2023-03-08
      • 1970-01-01
      相关资源
      最近更新 更多