🎉 Black Friday deal! 25% off your first year of CodyHouse Pro →

Projects

Progress value is 30%

Infinite Scroll

Infinite Scroll plugin to automatically add new content to the page.

Copy: View demo

Dependencies

All components are built upon the CodyHouse Framework.

This component includes variations. All variations share the same SCSS/JS code. They only differ in the HTML part.

How To

The Infinite Scroll plugin is used to add dynamic content without the need to load a new page. The loading can be trigger by the scroll of the page or the click on a Load More button (or a combination of the two).

Options #

The plugin comes with a list of options that you can use to customize the load behavior.

Load Button #

By default, the loading of new content is triggered by the scroll of the page (infinite scroll). If you want to use a button as trigger, add a data-load-btn='on' to the .js-infinite-scroll element:

<div class="js-infinite-scroll" data-load-btn="on">
  <!-- ... -->
</div>

If this option is on, the plugin will search for the .js-infinite-scroll__btn element and load new content when user clicks it.

Load Button Delay #

There may be cases where you want some of your content to be loaded on scroll, but then use a button to load the remaining part (user can decide to load more or move forward).

In this case, add a data-load-btn-delay="{n}", where {n} is the number of times you want new content to be loaded while scrolling. You will need to add a data-load-btn="on" for this option to work.

<!-- dynamic content will be loaded 2 times on scrolling, then the 'Load More' button will be used -->
<div class="js-infinite-scroll" data-load-btn="on" data-load-btn-delay="2">
  <!-- ... -->
</div>

Threshold #

This is the distance below the viewoport that will trigger the load of a new content (defalt is 200px). You change this value adding a data-threshold equal to the new value. You can use either px or %.

<!-- new threshold is 300px -->
<div class="js-infinite-scroll" data-threshold="300px">
  <!-- ... -->
</div>

<!-- new threshold is 50% of viewport -->
<div class="js-infinite-scroll" data-threshold="50%">
  <!-- ... -->
</div>

Update History #

If you want the url of your page to be updated when new content is loaded, add a data-history="on" (defaut is off).

<div class="js-infinite-scroll" data-history="on">
  <!-- ... -->
</div>

Path #

This is the url of the new page to be loaded.

It could be a comma-separated list of pages to load:

<div class="js-infinite-scroll" data-path="/projects.html, /about.html, /contact.html">
  <!-- ... -->
</div>

or a single string including '{n}' for the incremental page number:

<div class="js-infinite-scroll" data-path="/page-{n}.html">
  <!-- ... -->
</div>

The data-path attribute is false by default. In this case, the plugin will emit a custom event when new code needs to be loaded so that you can develop your custom load function. More in the Custom Load Function section.

Container #

The data-container attribute is used to specify the CSS selector of the container of the dynamic content. When not provided, the new content will be appended to the .js-infinite-scroll element.

Additionally, only the content of this element will be loaded from the dynamic page. When not provided, all content will be loaded.

<div class="js-infinite-scroll" data-container=".js-infinite-scroll__content">
  <!-- ... -->
</div>

Custom Load Function #

If you do not have a static page with your dynamic content (e.g., you need to make a call to a database to retrieve data), you can omit the data-path attribute.

When new content neeeds to be loaded, the plugin will emit the 'load-new' custom event. You can listen to this custom event and load new content. Here's an example:

var infiniteScrollElement = document.getElementsByClassName('js-infinite-scroll');
if(infiniteScrollElement.length > 0 ){
  infiniteScrollElement[0].addEventListener('load-new', function(event){
    // you need to load new content
    // event.detail.index is the index of the page to be loaded (e.g., 1 for the first dynamic load)
  });
}

Once you have loaded your new content, make sure to emit the 'loaded-new' custom event; the plugin will detect this event and reset the page for new loading. Here's how you can do that:

var event = new CustomEvent('loaded-new', {detail: {path: pathString, checkNext: bool}});
infiniteScrollElement[0].dispatchEvent(event);
  • path is the new url you want to use for the page (set to false if you do not want to update the browser history);
  • checkNext is a boolean variable: set to true if there's still content to be loaded; false otherwise.

Accessibility #

Even if data-load-btn is off, we do suggest you include the .js-infinite-scroll__btn element ('Load More' button - the plugin will hide it). This button will be announced to Screen Readers and will allow users using the keyboard to load new content (see demo).

When using a custom load function, you can use the moveFocus property of the event.detail to detect whether you need to move the focus to the first loaded element in the new content. This will improve keyboard accessibility.

By default, its value is false (no need to move focus). If the user is navigating using a keyboard, the value will be true: you should move the focus so the user can navigate the new content with their keyboard.

Bug Report & Feedback

To report a bug or get notified if we update this component, visit the GitHub repository.

If you'd like to suggest new components, visit our components board.

✅ Project duplicated

✅ Project created

There was an error while trying to export your project. Please try again or contact us