每个函数在无限滚动中选择当前页面

时间:2014-10-04 09:50:00

标签: jquery ajax infinite-scroll pushstate

我已经将一些Jquery放入一个站点,当你接近当前页面的底部时,该站点会加载并附加下一页。我想检测用户在页面上的位置,以便我可以使用history.pushstate()修改URL。

每个页面都包含在'加载部分'类。

var load_number;
var start_offset;
var end_offset;
// Run a function on each page
$('.load-part').each(function() {

    // Get the page number 
    load_number = parseInt($(this).attr('id').replace("load-part-",""));

    // Get the true top offset of the page
    start_offset = $(this).children().first().offset().top;

    // Get the true bottom offset of the page
    end_offset = $(this).children().last().offset().top + window_height;

    // If the window is between the limits - i.e. on this page
    if (start_offset <= $(window).scrollTop() < end_offset) {

        // Update the URL with the current page number
        history.pushState(null, null, load_number)
    }
}); 

我遇到的问题是if语句似乎正在评估为true,即使window.scrollTop()值明显不在限制之间。我做错了什么?

1 个答案:

答案 0 :(得分:0)

if (start_offset <= $(window).scrollTop() && $(window).scrollTop() < end_offset) {

        // Update the URL with the current page number
        history.pushState(null, null, load_number)
    }

更多启用javascript的内容

相关问题