javascript - How to change offset on smooth scroll JQuery animation by a height of the navbar -
all smooth scrolling works perfectly, however, anchor stops @ top of page, navigation bar fixed top, need do, offset top height of navbar. how go this?
<script> $(document).ready(function(){ // add smooth scrolling links $("a").on('click', function(event) { // make sure this.hash has value before overriding default behavior if (this.hash !== "") { // prevent default anchor click behavior event.preventdefault(); // store hash var hash = this.hash; // using jquery's animate() method add smooth page scroll // optional number (800) specifies number of milliseconds // takes scroll specified area $('html, body').animate({ scrolltop: $(hash).offset().top }, 1000, function(){ // add hash (#) url when done scrolling (default click behavior) window.location.hash = hash; }); } // end if }); }); </script>
pretty simple: scrolltop: $('#hash').offset().top - navbarheight
.
Comments
Post a Comment