2014-01-09 26 views
2

我已经开始使用CSS-Tricks的代码,并且它与偏移量一起工作良好。问题是该网站有一个固定的标题,所以我需要它从另一个页面导航到内部链接时应用偏移量。目前它正在被切断。jQuery平滑滚动链接到其他页面上的偏移量

$('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-144 
      }, 1000); 
      return false; 
     } 
    } 
}); 

任何帮助将不胜感激。有问题的页面是http://marketingmo.wpengine.com/services/#brand-development

回答

1

实际上没有原生js函数可以阻止页面加载时的哈希锚定。但有一个很好的解决方法,它可以在这个SO question找到。我之前使用过这种方法,它工作得很好。

setTimeout(function() { 
    var hash = location.hash 
    if (hash && $(hash).length) { 
     var target = $(hash).offset().top; 
     var headerH = $('header').height(); 
     $(window).scrollTop(target - headerH) 
     /* 
     //or with animate, 
     //but you'll need to reset the scrollTop to 0 (the top) first: 
     $(window).scrollTop(0); 
     $('html,body').animate({scrollTop:target - headerH}, 1000); 
     */ 
    } 
}, 1); 
+0

这就是我正在寻找的。除了原始代码之外,我还使用了那一点代码,它们在一起玩的很好。谢谢马克! –