Last active
April 11, 2019 16:29
-
-
Save gtempesta/189efb95dcdf37aaff948554720b0701 to your computer and use it in GitHub Desktop.
Offset content for anchor navigation in layouts with fixed headers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<style> | |
/* | |
Original idea | |
https://stackoverflow.com/a/13184714/ | |
*/ | |
.anchor{ | |
display: block; | |
position: relative; | |
top: -120px; /* this value can be updated */ | |
visibility: hidden; | |
} | |
</style> | |
<div class="nav"> | |
<div class="container"> | |
<ul class="nav navbar-nav"> | |
<!-- navigation --> | |
</ul> | |
</div> | |
</div> | |
<div class="container"> | |
<div class="description"> | |
<a class="anchor" id="presentation"></a> | |
Content | |
</div> | |
<div class="description"> | |
<a class="anchor" id="pricing"></a> | |
Content | |
</div> | |
</div> | |
<script> | |
// If the fixed header can have a different height | |
// Update the value and call on window.resize if the layout is responsive | |
$(document).ready(function () { | |
var navigationHeight = $('.nav').outerHeight(); | |
// update anchor position | |
$('.anchor').css({ | |
top: -(navigationHeight) | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment