无限滚动并重新加载附加数据

时间:2014-01-14 04:51:05

标签: javascript php html ajax

我有一个网页,其中我有无限滚动并且效果很好,但是当从第二页(例如scroling.php)无限滚动到第一页(比如主页)之后附加的数据是重装它只是消失了。我的意思是它没有显示。

花了好几个小时后我才知道这是因为当我尝试重新加载任何附加的div时,第二页(scrolling.php)中变量(请求页面)的值消失了。< / p>

我认为我的代码会更清晰:

$(document).ready(function() {
    var page   = 1;
    var height = $("#forheight").height();
    $(window).scroll(function() {
        if ($(window).scrollTop() + height >= $("#forscrilling").height()-10) {
            document.getElementById('infiscroll').style.display = 'block';
            page++;
            var data = {
                requested_page: page,
                listoffrndsinimplode: "<?php echo $listoffrndsinimplode; ?>"
            };
            var actual_count = "<?php echo $noofposts; ?>";
            if ((page-1)*13 >= actual_count) {
                document.getElementById('infiscroll').style.display='none';
            } else {
                $.ajax({
                    type: "POST",
                    url: "scrolling.php",
                    data:data,
                    success: function(res) {
                        $("#loadinto").append(res);
                        document.getElementById('infiscroll').style.display='none';
                    }
                });
            }
        }
    });
});

这是第一页无限滚动,下面的代码在第二页:

<?php
session_start();                            
include_once('conn.php');
$name = $_SESSION['name'];
$id = $_SESSION['id'];
$email = $_SESSION['email'];
include_once('time.php');
$requested_page = $_POST['requested_page'];
$set_limit = (($requested_page - 1) * 13).",13";
$listoffrndsinimplode = $_POST['listoffrndsinimplode'];
$slashes = array("\'");
$friendswithoutslashes = str_replace($slashes, "'", "$listoffrndsinimplode");
$detailsoffrienda = mysql_query("
    select * from fk_views where (
        session_id IN ($friendswithoutslashes) or
        onprf_of IN ($friendswithoutslashes)) and
        (views !='' or uploadpic !='')
    order by id desc
    limit $set_limit
");
?>

这是我用于使用AJAX重新加载div的代码,它位于第一页:

function hits(obj) {
    var currentrateform = $(obj);
    var id = $(obj).attr("id"); //hitsform....
    $.ajax({
        type: 'POST',
        url: 'rating-manager.php', 
        data: currentrateform.serialize(),
        success: function(){
            $("#rate"+id).load("home.php #rate"+id);
        }
    });
    return false;
}

每个div都有一个唯一ID,例如评分+唯一的评论ID,因此ID看起来像rate1,rate2,rate3

我认为当请求页面的值离开第二页时,则不存在评论的ID,因此当页面没有任何具有该ID的div时,它就会消失。

我只想问一下如何在第二页上存储所请求页面的值。我无法将其存储到会话或cookie中,因为请求的页面可以是2,3,4,5,6,7 - 任何东西 - 我有很多这样的页面。

1 个答案:

答案 0 :(得分:0)

你使用的是最新版本吗?

https://github.com/paulirish/infinite-scroll

我认为有一个新选择:

appendCallback:true

相关问题