使用滚动更改部分更改的哈希值

时间:2014-02-19 19:15:41

标签: jquery scroll scrolltop hashchange

有些东西是不确定的,它对我不起作用。我想我做错了什么,但我无法弄清楚是什么。

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

1 个答案:

答案 0 :(得分: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