滚动加载更多内容
本例与《无刷新动态加载数据,滚动条加载》功能是相同的,自己查看用哪个更好.
数据库很简单content表 字段id和message
XML/HTML Code
- <?php
- include_once('../../conn.php');
- $last_msg_id=$_GET['last_msg_id'];
- $action=$_GET['action'];
- if($action <> "get")
- {
- ?>
- <link rel="stylesheet" href="css.css" type="text/css" />
- <script type="text/javascript" src="../../js/jquery-1.9.1.min.js"></script>
- <script type="text/javascript">
- $(document).ready(function(){
- function last_msg_funtion()
- {
- var ID=$(".message_box:last").attr("id");
- $('div#last_msg_loader').html('<img src="bigLoader.gif">');
- $.post("index.php?action=get&last_msg_id="+ID,
- function(data){
- if (data != "") {
- $(".message_box:last").after(data);
- }
- $('div#last_msg_loader').empty();
- });
- };
- $(window).scroll(function(){
- if ($(window).scrollTop() == $(document).height() - $(window).height()){
- last_msg_funtion();
- }
- });
- });
- </script>
- <link rel="stylesheet" type="text/css" href="../demo.css">
- </head>
- <body>
- <div align="center">
- <?php
- include('load_first.php');
- ?>
- <div id="last_msg_loader"></div>
- </div>
- </div>
- </body>
- </html>
- <?php
- }
- else
- {
- include('load_second.php');
- }
- ?>
load_first.php
PHP Code
- <?php
- $sql=mysql_query("SELECT * FROM content ORDER BY id DESC LIMIT 15");
- while($row=mysql_fetch_array($sql))
- {
- $msgID= $row['id'];
- $msg= $row['message'];
- ?>
- <div id="<?php echo $msgID; ?>" align="left" class="message_box" >
- <span class="number"><?php echo $msgID; ?></span><?php echo $msg; ?>
- </div>
- <?php
- }
- ?>
load_second.php
PHP Code
- <?php
- $last_msg_id=$_GET['last_msg_id'];
- $sql=mysql_query("SELECT * FROM content WHERE id < '$last_msg_id' ORDER BY id DESC LIMIT 5");
- $last_msg_id="";
- while($row=mysql_fetch_array($sql))
- {
- $msgID= $row['id'];
- $msg= $row['message'];
- ?>
- <div id="<?php echo $msgID; ?>" align="left" class="message_box" >
- <span class="number"><?php echo $msgID; ?></span><?php echo $msg; ?>
- </div>
- <?php
- }
- ?>
原文地址:http://www.freejs.net/article_jquerywenzi_86.html