My aim is to have the menu bar stick at the top of the viewport once the header has been scrolled past.
I have used this jQuery to achieve this and have used the following code to modify the css when the menu bar reaches the top:
jQuery(document).ready(function($){
// Check the initial Poistion of the Sticky Header
var stickyHeaderTop = $('#stickyMenu').offset().top;
$(window).scroll(function(){
if( $(window).scrollTop() > stickyHeaderTop ) {
$('#stickyMenu').css({position: 'fixed', top: '0px'});
$('#stickyAlias').css('display', 'block');
} else {
$('#stickyMenu').css({position: 'static', top: '0px'});
$('#stickyAlias').css('display', 'none');
}
});
});
Here is my dev page with my working example: http://dev.harryg.me/serge/home/
It works a dream in Firefox, but there is an issue with Chrome; the css change seems to kick in much earlier than it should - after only scrolling a few pixels down the menu bar becomes fixed and sticks to the top. I have a feeling the header image is to blame but cannot figure out why...
EDIT:
Thank you for all your suggestions. As I am away from my dev setup I will have to wait to reveiw all your suggestions. As Explosion Pills and Malk was saying, the issue is because the stickyHeaderTop
is calculated before the header image is loaded. This is why it works once the image is cached, but not if you refresh the page.
I will try Explosion Pills' solution(s) later and upvote/accept as required.