Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在DIV中实现滚动_Javascript_Ajax_Infinite Scroll - Fatal编程技术网

Javascript 在DIV中实现滚动

Javascript 在DIV中实现滚动,javascript,ajax,infinite-scroll,Javascript,Ajax,Infinite Scroll,下面是我的Javascript代码。这有助于我在网页中获得无限滚动。如果我想在一个DIV中实现无限滚动,该怎么办?如何修改此代码以在DIV中使用它? 谁能帮帮我吗。假设我的DIV的id是要显示滚动内容的包装器 $(窗口)。滚动(函数(){ if($(窗口).scrollTop()==$(文档).height()-$(窗口).height()){ $('div#loadmoreajaxloader').show(); $.ajax({ url:“loadmore.php?lastid=”+$(“

下面是我的Javascript代码。这有助于我在网页中获得无限滚动。如果我想在一个DIV中实现无限滚动,该怎么办?如何修改此代码以在DIV中使用它? 谁能帮帮我吗。假设我的DIV的id是要显示滚动内容的包装器


$(窗口)。滚动(函数(){
if($(窗口).scrollTop()==$(文档).height()-$(窗口).height()){
$('div#loadmoreajaxloader').show();
$.ajax({
url:“loadmore.php?lastid=”+$(“.positem:last”).attr(“id”),
成功:函数(html){
如果(html){
$(“#postswraper”).append(html);
$('div#loadmoreajaxloader').hide();
}否则{
$('div#loadmoreajaxloader').html('不再显示帖子');
}
}
});
}
});

只要用您选择的div替换对
窗口的提及即可

另外,将
scrollTop()
scrollHeight
进行比较,而不是设计可能准确也可能不准确的高度,并使用
=
出于安全考虑-并非所有浏览器都能正常工作。

将其替换为($(窗口)。scrollHeight()>=$(文档)。height()-$(包装器)。height()){或者什么?
<script type="text/javascript">
        $(window).scroll(function(){
            if($(window).scrollTop() == $(document).height() - $(window).height()){
                $('div#loadmoreajaxloader').show();
                $.ajax({
                    url: "loadmore.php?lastid=" + $(".postitem:last").attr("id"),
                    success: function(html){
                        if(html){
                            $("#postswrapper").append(html);
                            $('div#loadmoreajaxloader').hide();
                        }else{
                            $('div#loadmoreajaxloader').html('<center>No more posts to show.</center>');
                        }
                    }
                });
            }
        });
    </script>