如何解决错误无限滚动Javascript,向上滚动时不加载?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (100)

以下是我使用的代码:

 var busy = false;
  var limit = 6;
  var offset = 0;
  function displayRecords(lim, off) {
    $.ajax({
      type: "GET",
      async: false,
      url: "load/article-2.php",
      data: "limit=" + lim + "&offset=" + off,
      cache: false,
      beforeSend: function() {
        $("#loader_message").html("").hide();
        $('#loader_image').show();
      },
      success: function(html) {
        $("#results").append(html);
        $('#loader_image').hide();
        if (html == 0) {
          $("#loader_message").html('<br><span class="margintop20 padding10">No more records.</span>').show()
        } else {
          $("#loader_message").html('<br><span class="margintop20"><img src="file-server/loading.gif" width="24" height="24"> Loading please wait...</span>').show();
        }
        window.busy = false;
      }
    });
  }

  $(document).ready(function() {
    if (busy == false) {
      busy = true;
      displayRecords(limit, offset);
    }
    $(window).scroll(function() {
      if ($(window).scrollTop() + $(window).height() > $("#results").height() && !busy) {
        busy = true;
        offset = limit + offset;
        setTimeout(function() { displayRecords(limit, offset); }, 500);
      }
    });
  });

PHP CODE

$limit = (intval($_GET['limit']) != 0 ) ? $_GET['limit'] : 4; $offset = (intval($_GET['offset']) != 0 ) ? $_GET['offset'] : 0;
$article = "SELECT SQL_CALC_FOUND_ROWS * FROM article WHERE 1 AND status ='publish' ORDER BY id  DESC LIMIT $limit OFFSET $offset";
 try {
  $article1 = $pdo-> prepare($article); 
  $article1 ->execute();
  $results = $article1->fetchAll();
} catch (Exception $ex) {
  echo $ex->getMessage();
}if (count($results) > 0) {
foreach ($results as $data) {
include("../data/data.php");
echo "
    <div class='col-md-2 padding5 aos-item' data-aos='fade-up'>
      <div class='col-md-12 bgputih shadow padding10 hover-dark'>
      <a href='blog/$url' title='$stitle'> <img class='w-100 img-fluid' src='$fileserver/article/$dateimage/$imagem' alt='$stitle'> </a>
      <hr>
      <div class='titlearticle'>
        <a href='blog/$url' title='$stitle'>$title</a>
      </div>
      <div class='font10'>
      "; 
      include("../includes/bawah/sumber.php");
      include("../includes/bawah/article.php");
      echo"
    </div>
    </div>
    </div>
    ";
}
}

HTML CODE

<div class='padding10 margintop-10 row' id="results"></div>
<div id="loader_image"><img src="<?php echo "$fileserver/loading.gif"; ?>" width="24" height="24"> Loading... please wait</div>
<div class="col-md-12" align="center" id="loader_message"></div>
提问于
用户回答回答于

只有当窗口滚动到底部时,才应该执行Ajax调用

$(document).ready(function() {
//if (busy == false) { These lines will cause the AJAX call to be performed regardless of the window's scroll position and as such, they ought to be removed
  //busy = true;
  //displayRecords(limit, offset);
//}
$(window).scroll(function() {
  if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight && !busy) {
   //you're at the bottom of the page
    busy = true;
    offset = limit + offset;
    setTimeout(function() { displayRecords(limit, offset); }, 500);
  }
 });
});
用户回答回答于

这样试试:

var busy = false;


if (busy == false) {
  busy = true;
  displayRecords(limit, offset);
}

扫码关注云+社区

领取腾讯云代金券