Skip to content

Instantly share code, notes, and snippets.

@felquis
Last active November 30, 2015 18:41
Show Gist options
  • Save felquis/1fe93119d378472a21b6 to your computer and use it in GitHub Desktop.
Save felquis/1fe93119d378472a21b6 to your computer and use it in GitHub Desktop.
fInfinite Scroll in fJavaScript language, just for fModern Browsers
// **
// Recurso de infinite scroll, deve ser usado apenas um por página
// uma página não deve ter mais que 2 chamadas de infinite scroll
(function infiniteScrollInit() {
var infiniteScrollCalled = false
function fInfiniteScroll(callback) {
// get fThrottle https://gist.github.com/felquis/299029971e40ad040b34
document.addEventListener('scroll', fThrottle(onScroll, 300))
var distance = 0
var windowHeight = window.innerHeight
var scrollHeight = document.body.scrollHeight
function onScroll(event) {
// O tamanho de todo o documento HTML
// é necessário remover o height do window
// para que 100% seja exatamente quando
// o total do scroll encosta no bottom da janela
scrollHeight = event.target.body.scrollHeight - windowHeight
// Porcentagem do scroll que o usuário já fez
// com relação a quantidade de pixeis que o usuário fez scroll
distance = (event.target.body.scrollTop / scrollHeight) * 100
// executa função do infinite scroll
// quando o scroll estiver a 90% concluido
// significando 10% para completar o scroll
// da página
if (distance > 90 && !infiniteScrollCalled) {
// Próximos scroll enquanto estiver em 90% são bloqueados
// e um próximo infiniteScroll é chamado apenas se a função
// mgInfiniteScrollComplete liberar a variável abaixo
infiniteScrollCalled = true
if (!callback) { throw('propriedade callback não definida') }
// good luck!
callback()
}
}
}
// **
// Libera o infinite scroll
function fInfiniteScrollComplete() {
infiniteScrollCalled = false
}
window.fInfiniteScroll = fInfiniteScroll
window.fInfiniteScrollComplete = fInfiniteScrollComplete
}())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment