From d3a9af0666aa987be19ce44367d2cc8ea18dac53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=AA=E7=A3=8A?= Date: Tue, 27 Jun 2017 19:03:51 +0800 Subject: [PATCH] Remove infinitescroll in single page (#319) * Remove infinitescroll in single page * Update infinitescroll.js --- assets/js/infinitescroll.js | 37 ++++++++++++++++++++++++------------- default.hbs | 2 ++ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/assets/js/infinitescroll.js b/assets/js/infinitescroll.js index b737b7d..6da63cf 100644 --- a/assets/js/infinitescroll.js +++ b/assets/js/infinitescroll.js @@ -1,18 +1,29 @@ // Code snippet inspired by https://github.com/douglasrodrigues5/ghost-blog-infinite-scroll -$().ready(function () { - var page = 2, - blogUrl = window.location, +$(function ($) { + var currentPage = 1, + pathname = window.location.pathname, + $window = $(window), + $document = $(document), $result = $('.post-feed'); - $(window).scroll(function () { - if ($(window).scrollTop() + $(window).height() == $(document).height()) { - if (page <= maxPages) { - $.get((blogUrl + '/page/' + page), - function (content) { - $result.append($(content).find('.post').hide().fadeIn(100)); - page = page + 1; - }); - } + function handleScroll () { + // return if not scroll to the bottom + if ($window.scrollTop() + $window.height() !== $document.height()) { + return; } - }); + + if (currentPage >= maxPages) { + return $window.off('scroll', handleScroll); + } + + // next page + currentPage++; + + // Load more + $.get((pathname + 'page/' + currentPage + '/'), function (content) { + $result.append($(content).find('.post').hide().fadeIn(100)); + }); + } + + $window.on('scroll', handleScroll).trigger('scroll'); }); diff --git a/default.hbs b/default.hbs index b3d7a63..955751d 100644 --- a/default.hbs +++ b/default.hbs @@ -63,10 +63,12 @@ + {{#if pagination.pages}} + {{/if}} {{!-- The #block helper will pull in data from the #contentFor other template files. In this case, there's some JavaScript which we only want to use in post.hbs, but it needs to be included down here, after jQuery has already loaded. --}} {{{block "scripts"}}}