jquery - How do I navigate to slightly above an anchor tag? -
i know how navigate given anchor tag on page - i'm trying link part of page 30-40 pixels above target anchor. reason have bootstrap navigation ends covering part of relevant content if directly part of page anchor.
currently have link:
%a{:href => root_path + "#how_it_works"} how works and on page links to:
.span12#how_it_works any thoughts on how can link navigate section of page above .span12#how_it_works?
you might able hack around adding padding in css, surest way javascript:
$('a[href="#how_it_works"]').on('click',function(e){ // prevent normal scrolling action e.preventdefault(); // grab target url anchor's ``href`` var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); if (target.length) { window.scrollto(0, target.offset().top - 50); // <-- our offset in px adjust necessary return false; } }); here's codepen.
this uses modified version of chris coyier's smooth scroll script. i've taken "smoothness" out of scrolling, add in animating scrolltop so:
if (target.length) { $('html,body').animate({ scrolltop: target.offset().top + 20 }, 1000); return false; }
Comments
Post a Comment