Skip to content

Instantly share code, notes, and snippets.

@diogoca
Created May 17, 2018 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diogoca/ebdca7c52b8f651940625a0a6df89d0a to your computer and use it in GitHub Desktop.
Save diogoca/ebdca7c52b8f651940625a0a6df89d0a to your computer and use it in GitHub Desktop.
Scroll infinito com jQuery
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script>
var carregando = false;
var paginaAtual = 1;
function carregarConteudo(pagina) {
carregando = true;
let url = 'http://api.seuSite.localhost/cards-access-log?page=' + pagina;
$.get(url, function(data) {
if (data == null) {
$(window).off();
}
let linhas = data.data;
let $conteudo = $('#conteudo');
if (linhas) {
$.each(linhas, function(index, key) {
$conteudo.append(key.created_at + "<br>");
});
carregando = false;
}
});
}
function estaNoFinal() {
if (window.scrollY == document.body.scrollTop &&
window.scrollY > $('body').height() - window.innerHeight) {
return true;
}
}
window.onload = function() {
carregarConteudo(1);
}
$(window).on('scroll', function() {
if (estaNoFinal() && !carregando) {
paginaAtual++;
carregarConteudo(paginaAtual);
}
});
</script>
</head>
<style>
body{width: 60px}
</style>
<body>
<div id="conteudo"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment