The InfiniteScroll constructor accepts an options object to customize behavior.
Required Options:
path: Determines the URL for the next page. Can be a selector string (e.g., '.pagination__next'), a string with {{#}} as a placeholder for the page number (e.g., '/blog/page/{{#}}'), or a function that returns the URL.append: A selector string that specifies which elements from the loaded page should be appended to the container.
Key Configuration Options:
checkLastPage: (Boolean or String) Checks if the page has a path selector element. If path is a selector string, set this to the selector of the 'next' link.prefill: (Boolean) If true, loads and appends pages on initialization until the scroll requirement is met.responseBody: (String) Sets the method used on the response. Set to 'json' to load JSON.domParseResponse: (Boolean) Enables parsing the response body into a DOM. Disable to load flat text.fetchOptions: (Object or Function) Custom settings for the fetch() request (headers, CORS, POST method, etc.).outlayer: (Boolean) Integrates with Masonry, Isotope, or Packery so appended items are added to the layout.scrollThreshold: (Number) Distance between the viewport and scroll area to trigger the scrollThreshold event.elementScroll: (Boolean) Sets scroller to an element for overflow element scrolling.loadOnScroll: (Boolean) Loads next page when scroll crosses scrollThreshold.history: (String) Changes browser history and URL. Options: 'replace' (default) or 'push'.historyTitle: (Boolean) Updates the window title. Requires history to be enabled.hideNav: (Selector) Hides a navigation element.status: (Selector) Displays status elements indicating state: .infinite-scroll-request, .infinite-scroll-load, .infinite-scroll-error.button: (Selector) Enables a button to load pages on click.onInit: (Function) Called on initialization. Useful for binding events.debug: (Boolean) Logs events and state changes to the console.
let infScroll = new InfiniteScroll( '.container', {
path: '.pagination__next',
append: '.post',
checkLastPage: true,
prefill: false,
responseBody: 'text',
domParseResponse: true,
fetchOptions: undefined,
outlayer: false,
scrollThreshold: 400,
elementScroll: false,
loadOnScroll: true,
history: 'replace',
historyTitle: true,
hideNav: undefined,
status: undefined,
button: undefined,
onInit: undefined,
debug: false,
});