Something is undefined, and it's not working for me. I think I'm doing something wrong, but I can't figure out what.
//change the window hash
var changeHash = function(a) {
document.location.hash = a
};
//get the scrollPos
var scrollPos = function(){
$(window).scrollTop()
};
//find the position of the top of element
var topPos = function(a){
$(a).offset().top
};
//find the position of the bottom of element
var bottomPos = function(a){
$(a).offset().top + $(a).height()
};
$(window).scroll(function() {
if ($(this).scrollTop() < bottomPos('#top')) {
$('.navbar').fadeOut('fast');
changeHash('');
}
else {
$('.navbar').fadeIn('fast');
return;
if(scrollPos <= topPos('#services') && scrollPos >= bottomPos('#services')){
changeHash('services');
}
if(scrollPos <= topPos('#work') && scrollPos >= bottomPos('#work')){
changeHash('work');
}
if(scrollPos <= topPos('#connect') && scrollPos >= bottomPos('#connect')){
changeHash('connect');
}
}
});
It looks like you're not actually returning calculated values in 3 of your functions. Try this:
//get the scrollPos
var scrollPos = function(){
return $(window).scrollTop();
};
//find the position of the top of element
var topPos = function(a){
return $(a).offset().top;
};
//find the position of the bottom of element
var bottomPos = function(a){
return $(a).offset().top + $(a).height();
};
Side Note: You may want to get ride of the return
in the window
- scroll
- else
statement?