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

时间:2013-04-14 01:16:42

标签: jquery html ajax

我正在制作一个无尽的卷轴。

当我按下“获取更多帖子”按钮时,在添加数据后,它应向下滚动到动态添加数据的第一个帖子(在div中)。

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

我正在尝试让jQuery在添加的最后一篇文章之后找到div id(这是帖子的post_id号码),然后让jQuery找到下一个最低的帖子号码(应该是FIRST 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>

1 个答案:

答案 0 :(得分:0)

这样的事情应该有效。

var scrollDiv = $("#4").next("div").attr("id");

http://jsfiddle.net/637eT/1/