Scroll infinito
Publicado por Mariguuan (3 intervenciones) el 19/06/2021 16:49:11
Mirad, soy inútil. Llevo más de un mes tratando de hacer un scroll infinito para mi pagina y me es imposible. He probado de todo, he buscado videos de YouTube, enlaces de Google, cogido código de otras personas, preguntado en foros y nada. Me han dado algunas soluciones, pero no consigo hacerlas. He conseguido hacer una función ajax que se active cuando deslices y inserte el código en un div. Pero solo lo hace una vez. Luego como el div ya tiene texto no funciona más. Tan solo quiero un scroll infinito que cargue unas publicaciones cuando deslices. Nada más. Muchas gracias.
Este es el código que tengo:
Y este es el codigo que quiero cargar. El de articulo.php:
Este es el código que tengo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<script>
function ajax(){
const http = new XMLHttpRequest();
const url = 'http://localhost/Registro/articulo.php';
http.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
console.log(this.responseText);
document.getElementById("response").innerHTML = this.responseText;
}
}
http.open("GET", url);
http.send();
}
$(document).ready(function(){
var count=0;
$(document).scroll(function(){
if($(document).scrollTop()+$(window).height()>=$(document).height())
{
count+=1;
$("body").append( "<article>",ajax(),"</article><article>",ajax(),"</article><article>",ajax(),"</article>");
}
});
});
</script>
Y este es el codigo que quiero cargar. El de articulo.php:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php include("includes/header3.php")?>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<link rel="stylesheet" href="estilos4.css">
<div class= "articulo" id="articulo">
<article>
<?php $sql="SELECT*FROM publicaciones ORDER BY RAND() limit 1";
$result=mysqli_query($conn,$sql);
while($mostrar=mysqli_fetch_array($result)){
$publicacion=$mostrar['publicacion'];
?>
<?php } ?>
<hgroup>
<div id="user" onclick="location.href='Perfil.php'"><img src="Imagenes/Foto de perfil - copia.png" id="fotouser">
<h1 id="nombreuser">
<?php $sql="SELECT*FROM publicaciones WHERE publicacion= '".$publicacion."' ORDER BY RAND() limit 1";
$result=mysqli_query($conn, $sql);
while($mostrar=mysqli_fetch_array($result)){
$autor= ucfirst($mostrar['nombre']);?>
<?php echo $autor ?>
<?php } ?>
</h1></button>
</div>
<h1 id="Tituloarticulo">Título del primer artículo</h1>
</hgroup>
<time datetime="08-02-2021"> Publicado el 08-02-2021</time>
<table>
<?php
$result=mysqli_query($conn,$sql);
while($mostrar=mysqli_fetch_array($result)){
?>
<tr>
<td><?php echo $mostrar['publicacion'] ?> </td>
</tr>
<?php
}
?>
</table>
<footer>
<p>Comentarios (0)</p>
<button id="Comentarios"></button>
</footer>
</article>
</div>
Valora esta pregunta


0