I have a website where the header becomes fixed in position as the user scrolls down the page - standard stuff.
As a consequence of that, when I was clicking a link to go to an anchor tag further down the page, the beginning of that section I wanted to jump to was underneath the header.
To resolve this, I followed the advice here: offsetting an html anchor to adjust for fixed header which worked perfectly when I was jumping to a link that's on the same page.
However, if I tried to jump to the same anchor tag from a different page - E.G:
<a href="http://www.example.com/different-page.html#anchor-name">link text</a>
then I don't end up in the correct place on the page. I'm guessing what's happening is, I'm being taken to the anchor tag before the css has been loaded. As a result, I get placed at a certain point on the page (where the anchor currently resides) but then the content is loaded and the css moves the anchor tag and I'm left in the wrong place - nowhere near the tag.
So my question is, should it be possible to do this whilst still using just a simple hyperlink or should I try and hijack it and do some stuff with javascript to try and get me to the correct point on the page?
Update:
OK tried to use jquery to check the hash and navigate to it on document.ready, but it still appears to be executing too quickly.
If I use the following code:
if( $(".blogPost").length ) {
// var anchorID = window.location.hash;
//setTimeout(function() {
$('html,body').animate({
scrollTop: $("#comments").offset().top
}, 'slow');
//}, 3000);
}
The page does not scroll to the #comments element. However, if I use the setTimeout and wait an additional 3 seconds - the page then happily scrolls to that element. It's as if it needs that extra time for the page to load fully?