Created
May 17, 2018 16:36
-
-
Save diogoca/ebdca7c52b8f651940625a0a6df89d0a to your computer and use it in GitHub Desktop.
Scroll infinito com jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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