jquery原生滚屏分页 滚动到指定位置自动加载下一页
XML/HTML Code
- <div>
- <div id="lists"></div>
- <div class="nodata"></div>
- </div>
JavaScript Code
- <script type="text/javascript">
- i = 1; //设置当前页数
- $(function() {
- var totalpage = 4; //总页数,防止超过总页数继续滚动
- var winH = $(window).height(); //页面可视区域高度
- $(window).scroll(function() {
- if (i < totalpage) { // 当滚动的页数小于总页数的时候,继续加载
- var pageH = $(document.body).height();
- var scrollT = $(window).scrollTop(); //滚动条top
- var aa = (pageH - winH - scrollT) / winH;
- if (aa < 0.01) {
- getJson(i)
- }
- } else { //否则显示无数据
- showEmpty();
- }
- });
- getJson(0); //加载第一页
- });
- function getJson(page) {
- $(".nodata").show().html("<img src='../../images/loader-green.gif'/>");
- $.getJSON("ajax.php", {page: i}, function(json) {
- if (json) {
- var str = "";
- $.each(json, function(index, array) {
- var str = "<div class='per'>";
- var str = str + "<div class='title'>" + array['id'] + "、" + array['title'] + "</div></div>";
- $("#lists").append(str);
- });
- $(".nodata").hide()
- } else {
- showEmpty();
- }
- });
- i++;
- }
- function showEmpty() {
- $(".nodata").show().html("别滚动了,已经到底了。。。");
- }
- </script>
PHP Code
- $page = intval($_GET['page']); //获取请求的页数
- $pagenum = 6; //每页数量
- $start = ($page - 1) * $pagenum;
- $query = mysqli_query($lr,"SELECT * FROM product ORDER BY articleid desc LIMIT $start," . $pagenum . "");
- $arr = array();
- while ($row = mysqli_fetch_array($query)) {
- $arr[] = array(
- 'id' => $row['articleid'],
- 'title' => $row['product_name']
- );
- }
- if ($arr) {
- echo json_encode($arr); //转换为json数据输出
- }
原文地址:http://www.freejs.net/article_fenye_836.html