Reputation: 4609
I have an a tag that I am using to specify the section on an HTML page where I want to move when a link is clicked:
<a name="ourServicesSection"></a>
This works fine, the only problem is that I have a fixed navbar on top of the page. So, when I click the link it moves to the section of the page, but a slice of it is covered up by the navbar. Is there a way I can "offset" the move by the size of the navbar? How would I do that?
Thanks
Upvotes: 1
Views: 674
Reputation:
If you are willing to use jQuery, something like this would work.
$('a[href^="#"]').on('click', function(e) {
e.preventDefault();
var scrollTo = $(this).attr('href');
var pageOffset = $(scrollTo).offset().top - ($(window).height() / 2);
$(document).scrollTop(pageOffset);
});
Example: http://jsfiddle.net/vDQaT/
Upvotes: 0
Reputation: 6381
You can use Javascript (jQuery) to handle the scroll with offset or reposition element using margin
Upvotes: 0
Reputation: 19466
Putting a margin/padding at the top of whatever element you're scrolling to seems to be the common practice. Or move up the achor destination.
Upvotes: 1