Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working prototype of infinite scroll.
- Loading branch information
Showing
7 changed files
with
190 additions
and
85 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
--- | ||
{ | ||
"posts" : [ | ||
{% for post in site.categories.blog %} | ||
"{{ post.url }}"{% unless forloop.last %},{% endunless %} | ||
{% endfor %} | ||
] | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
$(function() { | ||
|
||
var postURLs, | ||
isFetchingPosts = false, | ||
shouldFetchPosts = true, | ||
postsToLoad = $(".post-list").children().length, | ||
loadNewPostsThreshold = 3000; | ||
|
||
// Load the JSON file containing all URLs | ||
$.getJSON('/all-posts.json', function(data) { | ||
postURLs = data["posts"]; | ||
console.log(postsToLoad); | ||
|
||
// If there aren't any more posts available to load than already visible, disable fetching | ||
if (postURLs.length <= postsToLoad) | ||
disableFetching(); | ||
}); | ||
|
||
// If there's no spinner, it's not a page where posts should be fetched | ||
if ($(".infinite-spinner").length < 1) | ||
shouldFetchPosts = false; | ||
|
||
// Are we close to the end of the page? If we are, load more posts | ||
$(window).scroll(function(e){ | ||
if (!shouldFetchPosts || isFetchingPosts) return; | ||
|
||
var windowHeight = $(window).height(), | ||
windowScrollPosition = $(window).scrollTop(), | ||
bottomScrollPosition = windowHeight + windowScrollPosition, | ||
documentHeight = $(document).height(); | ||
|
||
// If we've scrolled past the loadNewPostsThreshold, fetch posts | ||
if ((documentHeight - loadNewPostsThreshold) < bottomScrollPosition) { | ||
fetchPosts(); | ||
} | ||
}); | ||
|
||
// Fetch a chunk of posts | ||
function fetchPosts() { | ||
// Exit if postURLs haven't been loaded | ||
if (!postURLs) return; | ||
|
||
isFetchingPosts = true; | ||
|
||
// Load as many posts as there were present on the page when it loaded | ||
// After successfully loading a post, load the next one | ||
var loadedPosts = 0, | ||
postCount = $(".post-list").children().length, | ||
callback = function() { | ||
loadedPosts++; | ||
var postIndex = postCount + loadedPosts; | ||
|
||
if (postIndex > postURLs.length-1) { | ||
disableFetching(); | ||
return; | ||
} | ||
|
||
if (loadedPosts < postsToLoad) { | ||
fetchPostWithIndex(postIndex, callback); | ||
} else { | ||
isFetchingPosts = false; | ||
} | ||
}; | ||
|
||
fetchPostWithIndex(postCount + loadedPosts, callback); | ||
} | ||
|
||
function fetchPostWithIndex(index, callback) { | ||
var postURL = postURLs[index]; | ||
|
||
$.get(postURL, function(data) { | ||
$(data).find(".blog-post").appendTo(".post-list"); | ||
console.log(postURL); | ||
console.log($(".post-list").children().length); | ||
callback(); | ||
}); | ||
} | ||
|
||
function disableFetching() { | ||
shouldFetchPosts = false; | ||
isFetchingPosts = false; | ||
$(".infinite-spinner").fadeOut(); | ||
} | ||
|
||
}); |