2

I have set up a fixed navigation with scrollspy and smooth scrolling:

HTML

<body data-spy="scroll" data-target=".navbar-collapse">
<!-- ---------- Navigation ---------- -->
<div class="navbar navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="sr-only">Toggle navigation</span>
      </button>
      <a class="navbar-brand" href="index.html"> <img src="http://placehold.it/200x50"></a>
    </div>
    <div class="navbar-collapse collapse">
       <ul class="nav navbar-nav navbar-right">
        <li class="active"><a href="#home">Home</a></li>
        <li><a href="#works">Works</a></li>
        <li><a href="#about">About</a></li>
        <li><a href="#timeline">Timeline</a></li>
        <li><a href="#contact">Contact</a></li>
      </ul>
    </div><!--/.nav-collapse -->
  </div>
</div>
<!-- ---------- /Navigation ---------- -->

JS

$(".navbar-nav li a[href^='#']").on('click', function(e) {
e.preventDefault();

var hash = this.hash;
$('html, body').animate({
   scrollTop: $(this.hash).offset().top
 }, 1000, function(){
   window.location.hash = hash;
 });

}); 

This works great, unfortunately it scrolls without any offset (my nav has a height of 80px). I tried to add "data-offset=80" but this doesn't do anything. I am not too sure what I have to add to the JS, to make this work :(

Paranoia
  • 2,040
  • 5
  • 25
  • 27
  • The answer to [this question](http://stackoverflow.com/questions/9288482/how-do-i-set-the-offset-for-scrollspy-in-bootstrap?rq=1) may help you. – mongol Aug 19 '14 at 16:22
  • Try this tutorial with css trick, no javascript: https://css-tricks.com/hash-tag-links-padding/ – Daniel Hári Jul 26 '15 at 11:39

1 Answers1

0

Since you have a navbar-fixed-top, you need to manually set the offset. Try

$("body").scrollspy({offset: 80});
Danii
  • 99
  • 1
  • 6