如何使用“;滚动到“;以考虑不同媒体查询时的固定导航高度

How do you use "Scroll To" to factor in the fixed nav heights at different media queries?

本文关键字:查询 导航 高度 媒体 滚动 何使用      更新时间:2023-09-26

我的条幅中有一个按钮。单击此按钮时,我希望它能将我滚动到横幅下方的div。这是我正在使用的代码:

$(function() {
      $('a[href*="#"]:not([href="#"])').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;
          }
        }
      });
    });

当我点击它时,它会滚动我,但会滚动我超过div。我认为我需要做的是考虑固定导航的高度,可能还有我滚动到的div的padding-top

接下来的事情是,如果我现在在移动视图中,导航的高度发生了变化,该怎么办?我该如何考虑这一点?

我不太懂Javascript,所以任何帮助/建议都将不胜感激。

你可以这样做,这样你就可以修改滚动长度

function scrollNav() {
      $('a').click(function(){
        $('html, body').stop().animate({
            scrollTop: $( $(this).attr('href') ).offset().top - 160
        }, 400);
     return false;
  });
};