dongxuxian6930 2014-06-27 11:06
浏览 14
已采纳

像facebook聊天系统一样向上滚动加载数据[关闭]

i am developing a chat system where i need to display chat history on scroll up function like Facebook chat system.

can anybody help me?

  • 写回答

1条回答 默认 最新

  • dqyp50298 2014-06-27 11:17
    关注

    It'll go something like this...

    HTML

    <div id="chatBox">
         <div class='inner'>
         <?php foreach($messages as $m){;?>
           <div class='message'><?php echo $m;?></div>
        <?php ;};?>
         </div>
    </div>
    

    JQUERY

    $(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

    // 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); 
        }
    });
    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;}
    <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>


    This example just shows a quick and dirty scroll, with prepend. No ajax call or anything. But you get the idea

    It'll likely be a little more complex then what I posted above though....to avoid double data, etc

    And also, you need to send the server the id of last post, or pagination data of sorts, so it know what to retrieve next. How you do that is your choice.

    But basically youll query the server, and retieve the next 10 posts, and echo them in a foreach loop, then fetch that html, and display it

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?