[英]Why do not I reload the data when I scroll down and do it when I go back to the top of the page in my infinite scroll?
What I'm doing is an infinite scroll with jQuery, AJAX and PHP but when you have to load data, I reload the page when I go to the top of the page and not when it is almost at the bottom of the page, I leave my code of jQuery and AJAX. 我正在做的是使用jQuery,AJAX和PHP进行无限滚动的方法,但是当您必须加载数据时,我会在页面顶部而不是在页面底部时重新加载页面,离开我的jQuery和AJAX代码。 Thank you in advance!
先感谢您!
<script type="text/javascript">
var start = 0;
var limit = 5;
var reachedMax = false;
$(window).scroll(function () {
if ($(window).scrollTop() == $(document).height() - $(window).height())
getData();
});
$(document).ready(function () {
getData();
});
function getData() {
if (reachedMax)
return;
$.ajax({
url: 'publicaciones.php',
method: 'POST',
dataType: 'text',
data: {
getData: 1,
start: start,
limit: limit
},
success: function(response) {
if (response == "reachedMax")
reachedMax = true;
else {
start += limit;
$(".results").append(response);
}
}
});
}
</script>
This is my PHP, I add the code to resolve my problem. 这是我的PHP,我添加了代码来解决我的问题。 I think my PHP its necessary.
我认为我的PHP是必要的。
<?php
session_start();
include ('../conexion.php');
if(isset($_SESSION['id']))
{
$id = mysqli_real_escape_string($conexion, $_SESSION['id']);
$infouser = mysqli_query($conexion, "SELECT * FROM users WHERE id = $id");
$use = mysqli_fetch_array($infouser);
}
if(isset($_POST['getData']))
{
$start = mysqli_real_escape_string($conexion,$_POST['start']);
$limit = mysqli_real_escape_string($conexion,$_POST['limit']);
$consulta = mysqli_query($conexion, "SELECT * FROM publicaciones ORDER BY id_pub DESC LIMIT $start, $limit");
if(mysqli_num_rows($consulta) > 0)
{
$response = "";
while($data = mysqli_fetch_array($consulta)) {
$response .= "
<div class='card mt-3 border-0 rounded-0'>
<div class='card-body'>
<div class='row'>
<div class='col-sm-2'>
<div class='img-user rounded-circle'>
<img class='img-fluid-pub' src='../avatars/".$use['avatar']."' alt='User Image'>
</div>
</div>
<div class='col-sm-10'>
<a class='text-muted h3' href='perfil.php?id=".$use['id']."'>".$use['usuario']."</a><h3>Seguir</h3>
<p>".$data['fecha']."</p>
<p>".$data['titulo']." Me gusta</p>
<p id='descripcion'>".$data['descripcion']."</p>
<p class='leermas' onclick='leerMas(this);'>Leer más...</p>
<video controls>
<source src='../anadir-publicacion/publicaciones/".$data['video']."' type='video/mp4'>
Your browser does not support HTML5 video.
</video>
<p>Kcal: ".$data['kcal']." CarbHid: ".$data['carbohidratos']." Prot: ".$data['proteinas']." Gras: ".$data['grasas']." Tiempo: ".$data['tiempo']."m Personas: ".$data['personas']." Dinero aprox: ".$data['precio']."€ Categoría: ".$data['categoria']."</p>
</div>
</div>
</div>
</div>
";
}
exit($response);
}
else
exit('reachedMax');
}
?>
It is normal $(window).scrollTop()
is equal to 0 at the start and $(document).height()
is equal to $(window).height()
so yes 0 == 0 at the top of your page 正常情况下,
$(window).scrollTop()
开头等于0,而$(document).height()
等于$(window).height()
所以是0 == 0在页面顶部
what you want is 你想要的是
var nb_px_before_load = 100
if($(window).innerHeight() - $(window).scrollTop() < nb_px_before_load){
getData();
}
You can easily see this with the devTools of chrome with breakpoints 您可以使用带有断点的chrome的devTools轻松看到这一点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.