Skip to content

Commit

Permalink
Working prototype of infinite scroll.
Browse files Browse the repository at this point in the history
  • Loading branch information
trvrb committed Oct 18, 2015
1 parent 1e92533 commit 94d279d
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 85 deletions.
4 changes: 0 additions & 4 deletions _config.yml
Expand Up @@ -14,10 +14,6 @@ redcarpet:
- superscript
- strikethrough

paginate: 6
paginate_path: "blog/page:num"
paginate_category: "blog"

exclude:
- "*.branches"
- "*.kml"
Expand Down
3 changes: 3 additions & 0 deletions _layouts/default.html
Expand Up @@ -37,6 +37,9 @@
ga('send', 'pageview');

</script>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="/js/infinite-jekyll.js"></script>

</head>

Expand Down
100 changes: 51 additions & 49 deletions _layouts/post.html
Expand Up @@ -7,61 +7,63 @@
{% endif %}
{% endfor %}

<div class="row">
<div class="col-md-12">
<div class="media">
<div class="media-body">
<div class="title media-heading">
{{ page.title }}
</div>
<p>
<div class="smallhead">
{{ page.date | date_to_string }}
{% if author %}
by <a class="off" href="{{ author.url }}">{{ author.handle }}</a>
{% endif %}
<div class="blog-post">
<div class="row">
<div class="col-md-12">
<div class="media">
<div class="media-body">
<div class="title media-heading">
{{ page.title }}
</div>
<p>
<div class="smallhead">
{{ page.date | date_to_string }}
{% if author %}
by <a class="off" href="{{ author.url }}">{{ author.handle }}</a>
{% endif %}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

<div class="bigspacer"></div>
<div class="spacer"></div>
<div class="bigspacer"></div>
<div class="spacer"></div>

<div class="row">
<div class="col-md-3">
{% if page.link %}
<div class="smallhead">
Link
</div>
<div class="pad-left note">
<div class="smallspacer"></div>
<i class="fa fa-external-link fa-fw"></i>
<a class="off" href="{{ page.link }}">{{ page.link | remove: 'http://' | remove: 'https://' | replace: '/', '/<wbr>' | replace: '%', '%<wbr>' }}</a>
</div>
<div class="bigspacer"></div>
{% endif %}
<div class="smallhead">
Share
</div>
<div class="pad-left note">
<div class="smallspacer"></div>
{% tweet {{page.url}} {{page.title}} %}
{% trackback {{page.url}} %}
<div class="row">
<div class="col-md-3">
{% if page.link %}
<div class="smallhead">
Link
</div>
<div class="pad-left note">
<div class="smallspacer"></div>
<i class="fa fa-external-link fa-fw"></i>
<a class="off" href="{{ page.link }}">{{ page.link | remove: 'http://' | remove: 'https://' | replace: '/', '/<wbr>' | replace: '%', '%<wbr>' }}</a>
</div>
<div class="bigspacer"></div>
{% endif %}
<div class="smallhead">
Share
</div>
<div class="pad-left note">
<div class="smallspacer"></div>
{% tweet {{page.url}} {{page.title}} %}
{% trackback {{page.url}} %}
</div>
<div class="bigspacer"></div>
</div>
<div class="bigspacer"></div>
<div class="col-md-8">
{% if page.image %}
<img class="offset" src="{{ page.image }}">
{% endif %}
<div class="post">
{{ content }}
</div>
</div>
<div class="col-md-1"></div>
</div>
<div class="col-md-8">
{% if page.image %}
<img class="offset" src="{{ page.image }}">
{% endif %}
<div class="post">
{{ content }}
</div>
</div>
<div class="col-md-1"></div>
</div>
</div>



Expand Down
9 changes: 9 additions & 0 deletions all-posts.json
@@ -0,0 +1,9 @@
---
---
{
"posts" : [
{% for post in site.categories.blog %}
"{{ post.url }}"{% unless forloop.last %},{% endunless %}
{% endfor %}
]
}
39 changes: 8 additions & 31 deletions blog/index.html
Expand Up @@ -6,14 +6,17 @@
category: blog
---

{% for post in paginator.posts %}
<div class="post-list">

{% for post in site.categories.blog limit: 10 %}

{% for member in site.categories.team %}
{% if member.title == post.author %}
{% assign author = member %}
{% endif %}
{% endfor %}


<div class="blog-post">
<div class="row">
<div class="col-md-3">
<div class="head">
Expand All @@ -37,37 +40,11 @@
</div>
<div class="col-md-1"></div>
</div>
<div class="bigspacer"></div>
<div class="bigspacer"></div>
</div>

{% endfor %}

<div class="pager">

{% if paginator.previous_page %}
{% if paginator.previous_page == 1 %}
<a class="prev" href="/{{ page.category }}/">
<i class="fa fa-chevron-left fa-lg"></i>
</a>
{% else %}
<a class="prev" href="/{{ page.category }}/page{{ paginator.previous_page }}">
<i class="fa fa-chevron-left fa-lg"></i>
</a>
{% endif %}
{% endif %}

{% if paginator.next_page %}
<a class="next" href="/{{ page.category }}/page{{ paginator.next_page }}">
<i class="fa fa-chevron-right fa-lg"></i>
</a>
{% endif %}

</div>

<div class="row">
<div class="col-md-12 text-center">
<a class="off" href="/{{ page.category }}/archive/">
<div class="smallhead">Archive</div>
<i class="text-gray fa fa-chevron-circle-down fa-2x"></i>
</a>
</div>
</div>
<div class="infinite-spinner"></div>
35 changes: 34 additions & 1 deletion css/style.less
Expand Up @@ -345,4 +345,37 @@ body.dark .pager a:hover .icon.next { background-position:-100px -90px; }
body.dark .pager .icon.prev,
.pager a:hover .icon.prev { background-position:-80px -70px; }
body.dark .pager .icon.next,
.pager a:hover .icon.next { background-position:-100px -70px; }
.pager a:hover .icon.next { background-position:-100px -70px; }

/* Infinite scroll */

.infinite-spinner {
display: block;
width: 40px;
height: 40px;
border-radius: 40px;
background-color: #333;
margin: 60px auto;

-webkit-animation: scaleout 1.0s infinite ease-in-out;
animation: scaleout 1.0s infinite ease-in-out;
}

@-webkit-keyframes scaleout {
0% { -webkit-transform: scale(0.0) }
100% {
-webkit-transform: scale(1.0);
opacity: 0;
}
}

@keyframes scaleout {
0% {
transform: scale(0.0);
-webkit-transform: scale(0.0);
} 100% {
transform: scale(1.0);
-webkit-transform: scale(1.0);
opacity: 0;
}
}
85 changes: 85 additions & 0 deletions js/infinite-jekyll.js
@@ -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();
}

});

0 comments on commit 94d279d

Please sign in to comment.