Explorar o código

W3.9: simple infinite scroll.

Frederic G. MARAND %!s(int64=9) %!d(string=hai) anos
pai
achega
cda39392be
Modificáronse 1 ficheiros con 25 adicións e 1 borrados
  1. 25 1
      image_share.js

+ 25 - 1
image_share.js

@@ -2,6 +2,27 @@
 Images = new Mongo.Collection("images");
 
 if (Meteor.isClient) {
+  Session.set("imageLimit", 8);
+
+  let lastScrollTop = 0;
+
+  $(window).scroll(function (event) {
+    let $window = $(window);
+
+    // Current position in the page.
+    let scrollTop = $window.scrollTop();
+
+    // If we are near the bottom of the window.
+    if (scrollTop + $window.height() > $(document).height() - 100) {
+      // If we are heading down.
+      if (scrollTop > lastScrollTop) {
+        Session.set("imageLimit", Session.get("imageLimit") + 4);
+        console.log('Increased image count');
+      }
+      lastScrollTop = scrollTop;
+    }
+  });
+
   Accounts.ui.config({
     passwordSignupFields: "USERNAME_AND_EMAIL"
   });
@@ -15,7 +36,10 @@ if (Meteor.isClient) {
     images: function () {
       const createdBy = Session.get("userFilter");
       const selector = createdBy ? { createdBy } : {};
-      return Images.find(selector, { sort: { createdOn: -1, rating: -1 }});
+      return Images.find(selector, {
+        sort: { createdOn: -1, rating: -1 },
+        limit: Session.get("imageLimit")
+      });
     },
     filtering_images: function () {
       const userFilter = Session.get("userFilter");