Изменение хэша при изменении раздела с помощью прокрутки

0

Что-то не определено, и это не работает для меня. Я думаю, что я делаю что-то неправильно, но я не могу понять, что.

//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');
        }
    }
});
Теги:
scroll
scrolltop
hashchange

1 ответ

0
Лучший ответ

Похоже, вы фактически не возвращаете расчетные значения в 3 из ваших функций. Попробуй это:

//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();
};

Сторона Примечание. Возможно, вам захочется return в window - scroll - else выражение?

Ещё вопросы

Сообщество Overcoder
Наверх
Меню