【问题标题】:Changing Hash on Section Change with Scroll使用滚动更改部分更改的哈希
【发布时间】:2014-02-19 19:15:40
【问题描述】:

有些东西是未定义的,它对我不起作用。我想我做错了什么,但我不知道是什么。

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

【问题讨论】:

    标签: jquery scroll scrolltop hashchange


    【解决方案1】:

    看起来您实际上并未在 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();
    };
    

    旁注:您可能想在window - scroll - else 声明中搭上return

    【讨论】:

      猜你喜欢
      • 2013-03-31
      • 2011-05-01
      • 1970-01-01
      • 2015-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多