Infinite Scroll - 图1

Infinite Scroll

Infinite Scroll allows to load additional content or do any other required action when page scroll is near to the bottom.

Infinite Scroll Layout

To enable Infinite Scroll you need to add additional infinite-scroll-content class to any scrollable container, particularly to our page scrolling area - <div class="page-content">:

  1. <div class="page">
  2. <div class="page-content infinite-scroll-content infinite-scroll-top">
  3. ...
  4. <div class="preloader infinite-scroll-preloader"></div>
  5. </div>
  6. </div>

Where:

  • div class="page-content infinite-scroll-content" our infinite scroll container

  • data-infinite-distance attribute allows to configure distance from the bottom of page (in px) to trigger infinite scroll event. By default, if not specified, it is 50 (px)

  • div class="preloader infinite-scroll-preloader" preloader with few additional styles to be used with infinite scroll

Infinite Scroll On Top

If you need to use infinite scroll on top of the page (when it scrolled to top), you need to add additional infinite-scroll-top class to “page-content”:

  1. <div class="page">
  2. <div class="page-content infinite-scroll-content infinite-scroll-top">
  3. <div class="preloader infinite-scroll-preloader"></div>
  4. ...
  5. </div>
  6. </div>

Infinite Scroll App Methods

There are two App’s methods that can be used with infinite scroll container:

app.infiniteScroll.create(el)- add infinite scroll event listener to the specified HTML element

  • el - HTMLElement or string (with CSS selector) - infinite scroll container. Required.

Use this methods only in case you have added infinite scroll content after page init or if you want to enable it later. Otherwise if there is “infinite-scroll-content” element on page init it will be created automatically

app.infiniteScroll.destroy(el)- remove infinite scroll event listener from the specified HTML container

  • el - HTMLElement or string (with CSS selector) - infinite scroll container. Required.

Infinite Scroll Events

Infinite Scroll will fire the following DOM events on app instance:

Dom Events

EventTargetDescription
infiniteInfinite Scroll container<div class=”page-content infinite-scroll-content”>Event will be triggered when page scroll reaches specified (in data-distance attribute) distance to the bottom.

App Events

Infinite scroll component emits events on app instance as well as on DOM element.

EventTargetArgumentsDescription
infiniteapp(el, event)Event will be triggered when page scroll reaches specified (in data-distance attribute) distance to the bottom.

Example

  1. <div class="page" data-page="home">
  2. ...
  3. <div class="page-content infinite-scroll-content">
  4. <div class="list simple-list">
  5. <ul>
  6. <li>Item 1</li>
  7. <li>Item 2</li>
  8. <li>Item 3</li>
  9. <li>Item 4</li>
  10. <li>Item 5</li>
  11. <li>Item 6</li>
  12. <li>Item 7</li>
  13. <li>Item 8</li>
  14. <li>Item 9</li>
  15. <li>Item 10</li>
  16. <li>Item 11</li>
  17. <li>Item 12</li>
  18. <li>Item 13</li>
  19. <li>Item 14</li>
  20. <li>Item 15</li>
  21. <li>Item 16</li>
  22. <li>Item 17</li>
  23. <li>Item 18</li>
  24. <li>Item 19</li>
  25. <li>Item 20</li>
  26. </ul>
  27. </div>
  28. <div class="preloader infinite-scroll-preloader"></div>
  29. </div>
  30. </div>
  1. var app = new Framework7();
  2. var $$ = Dom7;
  3. // Loading flag
  4. var allowInfinite = true;
  5. // Last loaded index
  6. var lastItemIndex = $$('.list li').length;
  7. // Max items to load
  8. var maxItems = 200;
  9. // Append items per load
  10. var itemsPerLoad = 20;
  11. // Attach 'infinite' event handler
  12. $$('.infinite-scroll-content').on('infinite', function () {
  13. // Exit, if loading in progress
  14. if (!allowInfinite) return;
  15. // Set loading flag
  16. allowInfinite = false;
  17. // Emulate 1s loading
  18. setTimeout(function () {
  19. // Reset loading flag
  20. allowInfinite = true;
  21. if (lastItemIndex >= maxItems) {
  22. // Nothing more to load, detach infinite scroll events to prevent unnecessary loadings
  23. app.infiniteScroll.destroy('.infinite-scroll-content');
  24. // Remove preloader
  25. $$('.infinite-scroll-preloader').remove();
  26. return;
  27. }
  28. // Generate new items HTML
  29. var html = '';
  30. for (var i = lastItemIndex + 1; i <= lastItemIndex + itemsPerLoad; i++) {
  31. html += '<li>Item ' + i + '</li>';
  32. }
  33. // Append new items
  34. $$('.list ul').append(html);
  35. // Update last loaded index
  36. lastItemIndex = $$('.list li').length;
  37. }, 1000);
  38. });

← Icons

Inputs / Form Inputs →