Alex Alex - 1 year ago 135
jQuery Question

jQuery scroll to anchor not working correctly

I have the the following link:

and I am using this to scroll to a specific div with an id:

$('.project-nav a').on('click', function(e){

var anchor = $(this).attr('data-id'),
el = $('#' + anchor);
$('html, body').animate({ scrollTop: el.offset().top}, 500);


it works almost right, it doesn't scroll at a correct location, they all seem to be incorrect.

If you click details you wil be scrolled just above cast for example.

Answer Source

You should try:

 $('html, body').animate({ scrollTop: el.offset().top - $('header.close').height()}, 500);

The scrolling is working correctly. If you hide the header, you will see that 'Details' is perfectly at the top of the window...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download