1

For some unknown reason to me, my navigation bar is blocking the div content.

Below is my example code. How can I fix it so the div wouldn't be blocked by the sticky navigation bar when I click the button?

<div style="height:90px; border: 1px solid red; background-color: grey; position: fixed; width: 100%;">Navbar</div>

<div id="A">
This text will blocked by sticky navbar
<br/>A<br/><br/><br/><br/><br/>---
</div>

<div id="B">
This text will blocked by sticky navbar
<br/>B<br/><br/><br/><br/><br/>---
</div>

<br/><br/><br/><br/><br/><br/><br/>

<button onClick="document.getElementById('A').scrollIntoView();">A</button>
<button onClick="document.getElementById('B').scrollIntoView();">B</button>
kvantour
  • 25,269
  • 4
  • 47
  • 72
Dwipa Arantha
  • 93
  • 1
  • 7

1 Answers1

1

You have to leave some pixels from top so that the content will appear to start from the bottom of navbar. In this case padding-bottom worked.

function scrollToView(id){
  var top = document.getElementById(id).offsetTop-document.getElementById("nav").offsetHeight-10;
  //show 10 pixels down the border
  window.scrollTo(0, top);
}
<div style="height:90px; border: 1px solid red; background-color: grey; position: fixed; width: 100%;" id="nav">Navbar</div>

<div id="A" style="padding-top: 100px">
This text will blocked by sticky navbar
<br/>A<br/><br/><br/><br/><br/>---
</div>

<div id="B">
This text will blocked by sticky navbar
<br/>B<br/><br/><br/><br/><br/>---
</div>

<br/><br/><br/><br/><br/><br/><br/>
<div id="C">
This text will blocked by sticky navbar
<br/>C<br/><br/><br/><br/><br/>---
</div>

<button onClick="scrollToView('A')">A</button>
<button onClick="scrollToView('B')">B</button>
Hari Das
  • 10,145
  • 7
  • 62
  • 59