Skip to content

Instantly share code, notes, and snippets.

@AshKyd
Last active July 24, 2016 02:42
Show Gist options
  • Save AshKyd/c27435fde4f102d6dbde756c1ae0aca1 to your computer and use it in GitHub Desktop.
Save AshKyd/c27435fde4f102d6dbde756c1ae0aca1 to your computer and use it in GitHub Desktop.
var arbitraryHeight = 100000; // number of pixels of screen to load
var previousHeight, previousArticles;
document.body.style.opacity = 0;
var scrollTimeout;
var loadLoop = setInterval(() => {
var articles = Array.from(document.querySelectorAll('article'));
// how many pixels of screen have we loaded?
var loadedHeight = parseInt(window.getComputedStyle(document.querySelector('section')).height);
// If we've hit our targeted amount, stop looping & sort our images.
if (loadedHeight <= arbitraryHeight) {
// If more has been loaded, trigger another scroll (after waiting)
if (previousHeight !== loadedHeight) {
scrollTimeout = setTimeout(() => window.scrollTo(0, arbitraryHeight), previousHeight ? 500 : 0);
}
previousHeight = loadedHeight;
previousArticles = articles.length;
} else {
clearTimeout(loadLoop);
clearTimeout(scrollTimeout);
var parentNode = articles[0].parentNode;
var getTime = article => new Date(article.querySelector('time').attributes.datetime.textContent);
articles.sort((a, b) => getTime(a) < getTime(b) ? 1 : -1);
articles.forEach(article => parentNode.appendChild(article));
window.scrollTo(0, 0);
document.body.style.opacity = 1;
}
}, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment