首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >像facebook聊天系统一样在滚动条上加载数据

像facebook聊天系统一样在滚动条上加载数据
EN

Stack Overflow用户
提问于 2014-06-27 19:06:40
回答 1查看 15.7K关注 0票数 6

我正在开发一个聊天系统,我需要显示像Facebook聊天系统一样的滚动向上功能聊天历史。

有人能帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-27 19:17:36

大概是这样的.

HTML

代码语言:javascript
复制
<div id="chatBox">
     <div class='inner'>
     <?php foreach($messages as $m){;?>
       <div class='message'><?php echo $m;?></div>
    <?php ;};?>
     </div>
</div>

JQUERY

代码语言:javascript
复制
$(document).ready(function(){           
$("#chatBox").scrollTop($("#chatBox")[0].scrollHeight);

$('#chatBox').scroll(function(){
    if ($('#chatBox').scrollTop() == 0){

        // Do Ajax call to get more messages and prepend them
        // To the inner div
        // How you paginate them will be the tricky part though
        // You'll likely have to send the ID of the last message, to retrieve 5-10 'before' that 

        $.ajax({
        url:'getmessages.php',
        data: {idoflastmessage:id}, // This line shows sending the data.  How you get it is up to you
        dataType:'html',
        success:function(data){
            $('.inner').prepend(data);
            $('#chatBox').scrollTop(30); // Scroll alittle way down, to allow user to scroll more
        };
        });
    }
});

});

EXAMPLE HERE

代码语言:javascript
复制
// GENERATE FIRST BATCH OF MESSAGES
//This will be where you do your SQL and PHP first
for(var i=0;i<20;i++){
    $('.inner').prepend('<div class="messages">First Batch messages<br/><span class="date">'+Date()+'</span> </div>');}


$("#chatBox").scrollTop($("#chatBox")[0].scrollHeight);

// Assign scroll function to chatBox DIV
$('#chatBox').scroll(function(){
    if ($('#chatBox').scrollTop() == 0){
        // Display AJAX loader animation
         $('#loader').show();
  
      // Youd do Something like this here
      // Query the server and paginate results
      // Then prepend
      /*  $.ajax({
            url:'getmessages.php',
            dataType:'html',
            success:function(data){
                $('.inner').prepend(data);
            };
        });*/
        //BUT FOR EXAMPLE PURPOSES......
        // We'll just simulate generation on server

       
        //Simulate server delay;
        setTimeout(function(){
        // Simulate retrieving 4 messages
        for(var i=0;i<4;i++){
        $('.inner').prepend('<div class="messages">Newly Loaded messages<br/><span class="date">'+Date()+'</span> </div>');
            }
            // Hide loader on success
            $('#loader').hide();
            // Reset scroll
            $('#chatBox').scrollTop(30);
        },780); 
    }
});
代码语言:javascript
复制
body {font-family:Arial;}
#chatBox {width:300px;height:400px;border: 1px solid;overflow:scroll}
#loader {display:none;}
.messages {min-height:40px;border-bottom:1px solid #1f1f1f;}
.date {font-size:9px;color:#1f1f1f;}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="chatBox">
<!--Loading ANIMATION-->
<img id="loader" src='http://opengraphicdesign.com/wp-content/uploads/2009/01/loader64.gif'>
<!--END LOADING ANIMATION-->
    
    <div class='inner'>
        <!-- WHERE YOU WILL LOAD CONTENT-->
    </div>
</div>

这个例子只显示了一个快速而肮脏的卷轴,带有prepend。没有ajax调用或其他任何东西。但你明白我的意思

它可能会比我在上面发布的though....to避免重复数据等内容更复杂一些

此外,您还需要向服务器发送上一篇文章的id,或排序的分页数据,以便它知道下一步要检索什么。如何做到这一点是你的选择。

但基本上,您将查询服务器,检索接下来的10个帖子,并在foreach循环中回显它们,然后获取该html并显示它。

票数 25
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24450304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档