滚动到动态添加数据的第一个div



我正在制作一个无尽滚动条。

当我按下'Get more posts'按钮时,在添加数据后,它应该向下滚动到动态添加数据的第一个帖子(在div中)。

所以我想得到动态添加的数据的第一个DIV。

我试图使jQuery找到div id(这是一个帖子的post_id号)后得到添加的最后一个帖子,然后使jQuery找到下一个最低的帖子数(这应该是动态添加的数据的第一个div id)。然后向下滚动到这里

例如:

    <div id = "5">
        //Post.
    </div>
    <div id = "4">
        //Post.
    </div>
    //THIS IS THE DYNAMICALLY ADDED CONTENT, IT IS ONLY ADDED AFTER THE AJAX CALL WITH JQUERY.
    <div id = "2"> //<--- It should scroll down to that one.
        //Post.
    </div>
    <div id = "1">
        //Post.
    </div>
//Dynamically added content ends.
<script type = "text/javascript">
$(document).ready(function(){
    $('#get-more-post').click(function(){
    var post_id = $('div.Posted:last').attr('rel');
    //In this case, the post_id = 4.
    $.post("add_more_post.php", {post_id: post_id} , function(data){
        if(data){
            var scroll_div = //I want jQuery to find the post which is lower than post_id = 4, that is post_id = 2. How is it done?;
            parent.$("html, body").animate({ scrollTop: $("div#"+scroll_div).offset().top; }, "slow");
        }
    });
    });
});
</script>

这样应该可以。

var scrollDiv = $("#4").next("div").attr("id");
http://jsfiddle.net/637eT/1/

最新更新