• Stars
    star
    1,196
  • Rank 38,944 (Top 0.8 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Infinite Scroll Directive for Angular

Build Status Backers on Open Collective Sponsors on Open Collective npm version npm version npm downloads a month npm downloads a week

Consider Becoming a sponsor

Angular Infinite Scroll

versions now follow Angular's version to easily reflect compatibility.
Meaning, for Angular 10, use ngx-infinite-scroll @ ^10.0.0

Angular - Older Versions Support

Starting Angular 6 and Above - ngx-infinite-scroll@THE_VERSION.0.0
For Angular 4 and Angular = ^5.5.6 - use version [email protected]
For Angular 5.x with rxjs =<5.5.2 - use version [email protected]
For Angular version <= 2.3.1, you can use npm i angular2-infinite-scroll (latest version is 0.3.42) - please notice the angular2-infinite-scroll package is deprecated

Used By

and much more.

These analytics are made available via the awesome Scarf package analytics library

Opt-Out Of Scarf

Scarf can be disabled by following these directions

Front End Consulting Services

I'm a Senior Front End Engineer & Consultant at Orizens. My services include:

  • Angular/React/Javascript Consulting
  • Front End Architecture Consulting
  • Project Code Review
  • Project Development

Contact Here

Webpack and Angular

Installation

npm install ngx-infinite-scroll --save

Supported API

Properties

@Input() Type Required Default Description
infiniteScrollDistance number optional 2 the bottom percentage point of the scroll nob relatively to the infinite-scroll container (i.e, 2 (2 * 10 = 20%) is event is triggered when 80% (100% - 20%) has been scrolled). if container.height is 900px, when the container is scrolled to or past the 720px, it will fire the scrolled event.
infiniteScrollUpDistance number optional 1.5 should get a number
infiniteScrollThrottle number optional 150 should get a number of milliseconds for throttle. The event will be triggered this many milliseconds after the user stops scrolling.
scrollWindow boolean optional true listens to the window scroll instead of the actual element scroll. this allows to invoke a callback function in the scope of the element while listenning to the window scroll.
immediateCheck boolean optional false invokes the handler immediately to check if a scroll event has been already triggred when the page has been loaded (i.e. - when you refresh a page that has been scrolled)
infiniteScrollDisabled boolean optional false doesn't invoke the handler if set to true
horizontal boolean optional false sets the scroll to listen for horizontal events
alwaysCallback boolean optional false instructs the scroller to always trigger events
infiniteScrollContainer string / HTMLElement optional null should get a html element or css selector for a scrollable element; window or current element will be used if this attribute is empty.
fromRoot boolean optional false if infiniteScrollContainer is set, this instructs the scroller to query the container selector from the root of the document object.

Events

@Output() Type Event Type Required Description
scrolled EventEmitter IInfiniteScrollEvent optional this will callback if the distance threshold has been reached on a scroll down.
scrolledUp EventEmitter IInfiniteScrollEvent optional this will callback if the distance threshold has been reached on a scroll up.

Behavior

By default, the directive listens to the window scroll event and invoked the callback.
To trigger the callback when the actual element is scrolled, these settings should be configured:

  • [scrollWindow]="false"
  • set an explict css "height" value to the element

DEMO

Try the Demo in StackBlitz

Usage

First, import the InfiniteScrollModule to your module:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { InfiniteScrollModule } from 'ngx-infinite-scroll';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppComponent } from './app';

@NgModule({
  imports: [BrowserModule, InfiniteScrollModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent],
})
export class AppModule {}

platformBrowserDynamic().bootstrapModule(AppModule);

In this example, the onScroll callback will be invoked when the window is scrolled down:

import { Component } from '@angular/core';

@Component({
  selector: 'app',
  template: `
    <div
      class="search-results"
      infiniteScroll
      [infiniteScrollDistance]="2"
      [infiniteScrollThrottle]="50"
      (scrolled)="onScroll()"
    ></div>
  `,
})
export class AppComponent {
  onScroll() {
    console.log('scrolled!!');
  }
}

in this example, whenever the "search-results" is scrolled, the callback will be invoked:

import { Component } from '@angular/core';

@Component({
  selector: 'app',
  styles: [
    `
      .search-results {
        height: 20rem;
        overflow: scroll;
      }
    `,
  ],
  template: `
    <div
      class="search-results"
      infiniteScroll
      [infiniteScrollDistance]="2"
      [infiniteScrollThrottle]="50"
      (scrolled)="onScroll()"
      [scrollWindow]="false"
    ></div>
  `,
})
export class AppComponent {
  onScroll() {
    console.log('scrolled!!');
  }
}

In this example, the onScrollDown callback will be invoked when the window is scrolled down and the onScrollUp callback will be invoked when the window is scrolled up:

import { Component } from '@angular/core';
import { InfiniteScroll } from 'ngx-infinite-scroll';

@Component({
  selector: 'app',
  directives: [InfiniteScroll],
  template: `
    <div
      class="search-results"
      infiniteScroll
      [infiniteScrollDistance]="2"
      [infiniteScrollUpDistance]="1.5"
      [infiniteScrollThrottle]="50"
      (scrolled)="onScrollDown()"
      (scrolledUp)="onScrollUp()"
    ></div>
  `,
})
export class AppComponent {
  onScrollDown() {
    console.log('scrolled down!!');
  }

  onScrollUp() {
    console.log('scrolled up!!');
  }
}

In this example, the infiniteScrollContainer attribute is used to point directive to the scrollable container using a css selector. fromRoot is used to determine whether the scroll container has to be searched within the whole document ([fromRoot]="true") or just inside the infiniteScroll directive ([fromRoot]="false", default option).

import { Component } from '@angular/core';

@Component({
  selector: 'app',
  styles: [
    `
      .main-panel {
        height: 100px;
        overflow-y: scroll;
      }
    `,
  ],
  template: `
    <div class="main-panel">
      <div
        infiniteScroll
        [infiniteScrollDistance]="2"
        [infiniteScrollThrottle]="50"
        [infiniteScrollContainer]="selector"
        [fromRoot]="true"
        (scrolled)="onScroll()"
      ></div>
    </div>
  `,
})
export class AppComponent {
  selector: string = '.main-panel';

  onScroll() {
    console.log('scrolled!!');
  }
}

It is also possible to use infiniteScrollContainer without additional variable by using single quotes inside double quotes:

[infiniteScrollContainer]="'.main-panel'"

Showcase Examples

Contributors

This project exists thanks to all the people who contribute. [Contribute].

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

More Repositories

1

echoes-player

Echoes Player: the missing Media Player experience for Youtube - Built with Angular (9), ngrx (9), Angular CLI, Boostrap (SASS), Youtube api
TypeScript
834
star
2

echoes

Echoes is a media player based on youtube which provides a great user experience (no ads!). Overall, it's a web application that is based on angular.js & bootstrap.
JavaScript
334
star
3

angular-es6-styleguide

Angular (v 1.5) with ES6 / ES2015 Style Guide: A starting point for Angular development teams to provide consistency through good practices. http://orizens.com
204
star
4

ngrx-styleguide

a collection of best practices / commonly used patterns with ngrx extensions - store, effects, router and others
140
star
5

ngx-youtube-player

(ngx) A youtube component wrapped with Angular (typescript)
TypeScript
91
star
6

supernova-angular-1.5.x-es6-starter

starter kit for angular > 1.5.x, > es6, karma and jasmine
JavaScript
71
star
7

ngx-typeahead

A simple but yet powerful typeahead component for Angular (css framework agnostic)
TypeScript
60
star
8

sublime-angular2-snippets

John Papa's snippets for sublime text
51
star
9

Backbone.Safe

a plugin for backbone models to save & load data using localstorage as well
JavaScript
32
star
10

affinity

affinity is a case study: an attempt to create a simple component based library
TypeScript
18
star
11

angular2-infinite-scroll

Infinite Scroll Directive For Angular (version 2 up to 2.3.1)
TypeScript
15
star
12

gulp-dogen

a fast & easy scaffold cli for any development (based on gulp)
JavaScript
14
star
13

ngrx-action-creator-factory

An Action Creator Factory for ngrx
JavaScript
13
star
14

Backbone.Beamer

An Extension Manager & Loader for Backbone.js
JavaScript
10
star
15

html-extended

HTML syntax Sublime Text definition which supports Web Components with "-" in tag name
8
star
16

backbone-switcher

A view manager that switch views. built on top of backbone.beamer extension manager
JavaScript
8
star
17

angular2to1

An angular 2 to angular 1 Shim - Write angular 2 components with angular 1
JavaScript
7
star
18

Timber

An application framework for starting projects with Backbone.js & Requirejs DEPRECATED
JavaScript
5
star
19

echoes-comp

A Component Base version for Echoes Player
JavaScript
3
star
20

use-recorder

A Mic recorder hook for React
TypeScript
3
star
21

backbone-transition

A view manager that inserts transition to before and after render method. built on top of backbone.beamer extension manager
JavaScript
2
star
22

echoes-mobile

echoes player wrapped in ionic for Android
JavaScript
1
star
23

backbone.collection-view

A collection view support on top of backbone.beamer extension manager
JavaScript
1
star