如何获取jQuery waypoint插件来计算新添加的内容?

时间:2016-05-09 01:20:16

标签: javascript jquery jquery-waypoints

我正在为jQuery使用waypoint插件。它适用于页面上的div。但是当我使用AJAX添加新的div时,它不适用于那些新添加的div。它不算数。

这是我的代码:

Waypoint = $('.Picture-1A').waypoint(function(direction){
    if(direction == 'down'){
        id = $(this.element).children(".PictureID").text();
        $.post('ajax/count-pictures-views.php', {picture_id: id}, function(data){
            Waypoint.refreshAll();
        });
    }
});

我也使用了On()但它并不适用于所有,默认和新添加。 如何使其适用于新添加的内容?

这是我的Ajax页面:

$(document).ready(function(){

endPictures = true;

scrollCount = 0;

$(window).scroll(function(){

    if(endPictures == true){

    if(scrollCount == 0){

        if($(window).scrollTop() >= $(document).height() - $(window).height() - 3000){

        scrollCount = 1;

        picturesCount = $(".Picture-1A").length;

            $.post('ajax/load-latest.php', {off_set:picturesCount},  function(data){
                if(data){
                    $("#loadMore").append(data);
                    scrollCount = 0;
                }else{
                    $("#loadMore").append("<div class=\"NoMorePictures\"><center>There are no pictures left.</center></div><br/ >")
                    endPictures = false;
                    scrollCount = 0;
                }
            });

        }

    }

    }
});});

1 个答案:

答案 0 :(得分:0)

我终于想出了一个解决方案。这是我认为自我解释的代码。

$.post('ajax/load-latest.php', {off_set:picturesCount},  function(data){
    if(data){
        $("#loadMore").append(data);
        scrollCount = 0;
        $('.Picture-1A').waypoint(function(direction){
            if(direction == 'down'){
                id = $(this.element).children(".PictureID").text();
                $.post('ajax/count-pictures-views.php', {picture_id: id}, function(data){

                });
            }
        });
    }else{
        $("#loadMore").append("<div class=\"NoMorePictures\"><center>There are no pictures left.</center></div><br/ >")
                    endPictures = false;
                    scrollCount = 0;
                }
            });

我所做的就是将$('.Picture-1A').waypoint(function(){});添加到无限滚动ajax调用中。因此,每次触发ajax调用时,都会刷新航点。