AJAX无限滚动$(窗口).scroll(function(){无法正常工作

时间:2021-07-01 21:28:59

I am trying to incorporate infinite scroll in my rails application, but with limited success. I have pagination working right now with the rails gem will_paginate, but my script for infinite scroll is not getting past $(window).scroll(function() { function.

我试图在我的rails应用程序中加入无限滚动,但成功有限。我现在使用rails gem will_paginate进行分页工作,但我的无限滚动脚本没有超过$(window).scroll(function(){function。

$(document).on('ready page:load', function() {
    console.log('test');
    $(window).scroll(function() {
        console.log('test2');
        var url = $('.pagination span.next').children().attr('href');
        if (url && $(window).scrollTop() > $(document).height() - $(window).height() - 50) {
            $('.pagination').text("Fetching more products...");
            return $.getScript(url);
        }
    });
});

The first test is passing, but the script does not make it to the second test when I inspect the element in Chrome. Can you see why?

第一个测试是传递,但是当我检查Chrome中的元素时,脚本没有进入第二个测试。你能明白为什么吗?

1 个解决方案

#1


0  

Put $(window).scroll() out side of ready() as

把$(window).scroll()放在ready()的一边

$(window).scroll(function() {
        console.log('test2');
        var url = $('.pagination span.next').children().attr('href');
        if (url && $(window).scrollTop() > $(document).height() - $(window).height() - 50) {
            $('.pagination').text("Fetching more products...");
            return $.getScript(url);
        }
    });

#1


0  

Put $(window).scroll() out side of ready() as

把$(window).scroll()放在ready()的一边

$(window).scroll(function() {
        console.log('test2');
        var url = $('.pagination span.next').children().attr('href');
        if (url && $(window).scrollTop() > $(document).height() - $(window).height() - 50) {
            $('.pagination').text("Fetching more products...");
            return $.getScript(url);
        }
    });