Convert Figma logo to code with AI

metafizzy logoinfinite-scroll

📜 Automatically add next page

7,406
1,743
7,406
62

Top Related Projects

A jQuery plugin that adds a bindable 'inview' event for detecting when an element is scrolled into view.

4,649

Get notified when a DOM element enters or exits the viewport. :eyes:

An awesome Infinite Scroll component in react.

Quick Overview

Infinite Scroll is a JavaScript plugin that automatically adds the next page of content to a webpage, eliminating the need for pagination. It detects when the user scrolls near the bottom of the page and loads more content, creating a seamless browsing experience.

Pros

  • Enhances user experience by providing continuous content without page reloads
  • Customizable and flexible, supporting various content types and layouts
  • Works with different backend setups (static, WordPress, custom CMS)
  • Lightweight and performant, with minimal impact on page load times

Cons

  • May interfere with traditional navigation and bookmarking of specific content
  • Can potentially lead to excessive data usage on mobile devices if not implemented carefully
  • Requires JavaScript to function, which may not be ideal for all use cases
  • Can be challenging to implement proper SEO for infinitely scrolled content

Code Examples

  1. Basic initialization:
let infScroll = new InfiniteScroll('.container', {
  path: '.pagination__next',
  append: '.post',
  history: false,
});

This code initializes Infinite Scroll on a container element, specifying the path to the next page and the elements to append.

  1. Using a custom loader:
let infScroll = new InfiniteScroll('.container', {
  path: '.pagination__next',
  append: '.post',
  status: '.page-load-status',
  scrollThreshold: 400,
});

This example adds a custom loader element and sets a scroll threshold for when to load more content.

  1. Implementing with Masonry layout:
let msnry = new Masonry('.grid');
let infScroll = new InfiniteScroll('.grid', {
  path: '.pagination__next',
  append: '.grid-item',
  outlayer: msnry,
});

This code demonstrates how to use Infinite Scroll with Masonry layout for a grid-based design.

Getting Started

  1. Install Infinite Scroll:

    npm install infinite-scroll
    
  2. Include the script in your HTML:

    <script src="path/to/infinite-scroll.pkgd.min.js"></script>
    
  3. Initialize Infinite Scroll in your JavaScript:

    let infScroll = new InfiniteScroll('.container', {
      path: '.pagination__next',
      append: '.post',
    });
    
  4. Ensure your HTML structure includes the necessary elements (container, items to append, and next page links).

Competitor Comparisons

A jQuery plugin that adds a bindable 'inview' event for detecting when an element is scrolled into view.

Pros of jquery.inview

  • Lightweight and focused on detecting element visibility
  • Simple to implement for basic in-view detection scenarios
  • Works well with jQuery, which is still widely used in many projects

Cons of jquery.inview

  • Limited functionality compared to infinite-scroll's comprehensive feature set
  • Requires jQuery as a dependency, which may not be ideal for modern projects
  • Less actively maintained, with fewer recent updates and contributions

Code Comparison

infinite-scroll:

let infScroll = new InfiniteScroll('.container', {
  path: '.pagination__next',
  append: '.post',
  history: false,
});

jquery.inview:

$('.element').on('inview', function(event, isInView) {
  if (isInView) {
    // Element is now visible in the viewport
  }
});

Summary

infinite-scroll offers a more comprehensive solution for implementing infinite scrolling functionality, including content loading and appending. It's actively maintained and doesn't require jQuery. jquery.inview, on the other hand, is a simpler plugin focused solely on detecting when elements enter the viewport. It's lightweight but requires jQuery and has limited features compared to infinite-scroll. The choice between the two depends on the specific requirements of your project and whether you need a full infinite scrolling solution or just viewport detection.

4,649

Get notified when a DOM element enters or exits the viewport. :eyes:

Pros of in-view

  • Lightweight and focused on element visibility detection
  • Simple API with customizable thresholds and offsets
  • No dependencies, making it easier to integrate

Cons of in-view

  • Limited to visibility detection, lacking built-in infinite scrolling functionality
  • Requires additional implementation for content loading and appending
  • Less actively maintained compared to infinite-scroll

Code Comparison

in-view:

inView('.selector')
  .on('enter', el => {
    // Element enters viewport
  })
  .on('exit', el => {
    // Element exits viewport
  });

infinite-scroll:

let infScroll = new InfiniteScroll('.container', {
  path: '.pagination__next',
  append: '.post',
  history: false,
});

Key Differences

  • in-view focuses on detecting when elements enter or exit the viewport
  • infinite-scroll provides a complete solution for implementing infinite scrolling
  • in-view requires more custom code to achieve infinite scrolling functionality
  • infinite-scroll offers built-in features like history management and content appending

Use Cases

  • Choose in-view for simple visibility detection or custom scroll-based interactions
  • Opt for infinite-scroll when implementing full-featured infinite scrolling with minimal effort

Both libraries serve different purposes, with in-view being more flexible for general visibility detection and infinite-scroll offering a comprehensive infinite scrolling solution.

An awesome Infinite Scroll component in react.

Pros of react-infinite-scroll-component

  • Specifically designed for React applications, providing seamless integration
  • Offers built-in loading and "end message" components for better user experience
  • Supports both window and scrollable containers out of the box

Cons of react-infinite-scroll-component

  • Less flexible for non-React projects or complex customizations
  • May have a steeper learning curve for developers not familiar with React
  • Limited documentation compared to infinite-scroll

Code Comparison

react-infinite-scroll-component:

<InfiniteScroll
  dataLength={items.length}
  next={fetchMoreData}
  hasMore={hasMore}
  loader={<h4>Loading...</h4>}
>
  {items.map((item, index) => (
    <div key={index}>{item}</div>
  ))}
</InfiniteScroll>

infinite-scroll:

let infScroll = new InfiniteScroll('.container', {
  path: '.pagination__next',
  append: '.post',
  history: false,
});

The react-infinite-scroll-component is more declarative and React-specific, while infinite-scroll offers a more flexible, vanilla JavaScript approach that can be used in various environments. The choice between the two depends on the project's requirements, the development stack, and the level of customization needed.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Infinite Scroll

Automatically add next page

See infinite-scroll.com for complete docs and demos.

Install

Download

CDN

Link directly to Infinite Scroll files on unpkg.

<script src="https://unpkg.com/infinite-scroll@4/dist/infinite-scroll.pkgd.min.js"></script>
<!-- or -->
<script src="https://unpkg.com/infinite-scroll@4/dist/infinite-scroll.pkgd.js"></script>

Package managers

npm: npm install infinite-scroll

Yarn: yarn add infinite-scroll

License

Commercial license

If you want to use Infinite Scroll to develop commercial sites, themes, projects, and applications, the Commercial license is the appropriate license. With this option, your source code is kept proprietary. Purchase an Infinite Scroll Commercial License at infinite-scroll.com

Open source license

If you are creating an open source application under a license compatible with the GNU GPL license v3, you may use Infinite Scroll under the terms of the GPLv3.

Read more about Infinite Scroll's license.

Usage

Infinite Scroll works on a container element with its child item elements

<div class="container">
  <article class="post">...</article>
  <article class="post">...</article>
  <article class="post">...</article>
  ...
</div>

Options

let infScroll = new InfiniteScroll( '.container', {
  // defaults listed

  path: undefined,
  // REQUIRED. Determines the URL for the next page
  // Set to selector string to use the href of the next page's link
  // path: '.pagination__next'
  // Or set with {{#}} in place of the page number in the url
  // path: '/blog/page/{{#}}'
  // or set with function
  // path: function() {
  //   return return '/articles/P' + ( ( this.loadCount + 1 ) * 10 );
  // }

  append: undefined,
  // REQUIRED for appending content
  // Appends selected elements from loaded page to the container

  checkLastPage: true,
  // Checks if page has path selector element
  // Set to string if path is not set as selector string:
  //   checkLastPage: '.pagination__next'

  prefill: false,
  // Loads and appends pages on intialization until scroll requirement is met.

  responseBody: 'text',
  // Sets the method used on the response.
  // Set to 'json' to load JSON.

  domParseResponse: true,
  // enables parsing response body into a DOM
  // disable to load flat text

  fetchOptions: undefined,
  // sets custom settings for the fetch() request
  // for setting headers, cors, or POST method
  // can be set to an object, or a function that returns an object

  outlayer: false,
  // Integrates Masonry, Isotope or Packery
  // Appended items will be added to the layout

  scrollThreshold: 400,
  // Sets the distance between the viewport to scroll area
  // for scrollThreshold event to be triggered.

  elementScroll: false,
  // Sets scroller to an element for overflow element scrolling

  loadOnScroll: true,
  // Loads next page when scroll crosses over scrollThreshold

  history: 'replace',
  // Changes the browser history and URL.
  // Set to 'push' to use history.pushState()
  //    to create new history entries for each page change.

  historyTitle: true,
  // Updates the window title. Requires history enabled.

  hideNav: undefined,
  // Hides navigation element

  status: undefined,
  // Displays status elements indicating state of page loading:
  // .infinite-scroll-request, .infinite-scroll-load, .infinite-scroll-error
  // status: '.page-load-status'

  button: undefined,
  // Enables a button to load pages on click
  // button: '.load-next-button'

  onInit: undefined,
  // called on initialization
  // useful for binding events on init
  // onInit: function() {
  //   this.on( 'append', function() {...})
  // }

  debug: false,
  // Logs events and state changes to the console.
})

Browser support

Infinite Scroll v4 supports Chrome 60+, Edge 79+, Firefox 55+, Safari 11+.

For IE10 and Android 4 support, try Infinite Scroll v3.

Development

This package is developed with Node.js v14 and npm v6. Manage Node and npm version with nvm.

nvm use

Install dependencies

npm install

Lint

npm run lint

Run tests

npm test

By Metafizzy 🌈🐻

NPM DownloadsLast 30 Days