防止“ window.location.hash =”默认行为

时间:2018-11-06 17:32:52

标签: jquery

我一直在使用以下jquery进行动画页面滚动(Mostly from W3)。

$(document).ready(function(){
  $("a").on('click', function(event) {
    if (this.hash !== "") {
      event.preventDefault();
      var hash = this.hash;
      var headerHeight = $(".header").height() + $(".nav-menu").height() + 50;
      $('html').animate({ 
        scrollTop: $(hash).offset().top  - headerHeight
      }, 1500, function(){
        window.location.hash = hash - headerHeight - 50;
      });
    } // End if
  });
});

由于内容在固定标题下消失,我不得不在“ window.location.has = hash” 的末尾添加“-headerHeight-50;” (.header)和即时贴菜单(.nav-menu)。据我所知,默认行为是在动画后将窗口位置捕捉到哈希。

这适用于确实在页面上滚动的那些按钮。但是,我现在遇到一个问题,即单击任何其他ID元素会使页面向上滚动“ headerHeight-50”

我认为最简单的方法是首先防止发生默认行为。我环顾四周并找到了答案,尽管我不确定如何将其应用于此jquery实例。

Accounting for a fixed header with animate.scrolltop and (target).offset().top;

感谢您可以提供的任何帮助

史蒂夫

0 个答案:

没有答案
相关问题