2015-05-12 55 views
0

我想滚动到一个特定的div,然后让它在目标div之前停止约70px。下面是本教程中我得到了它关闭:如何将动画滚动到特定ID减X距离?

https://css-tricks.com/snippets/jquery/smooth-scrolling/

$(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; 
     } 
    } 
    }); 
}); 
+0

'scrollTop的:target.offset()顶 - 70'? –

+0

嗨@rjtkoh,如果你提到了你遇到的问题/错误信息---或者至少解释了什么不起作用,它会改善你的问题。 – DilithiumMatrix

回答

2

70。减去从上目标的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 - 70 
      }, 1000); 
      return false; 
      } 
    } 
    }); 
});