Giter Site home page Giter Site logo

crystalline / ngx-infinite-scroll Goto Github PK

View Code? Open in Web Editor NEW

This project forked from orizens/ngx-infinite-scroll

0.0 1.0 0.0 250 KB

Infinite Scroll Directive for Angular (>= 4)

Home Page: https://www.npmjs.com/package/ngx-infinite-scroll

License: MIT License

JavaScript 18.17% HTML 2.11% TypeScript 77.86% CSS 1.86%

ngx-infinite-scroll's Introduction

Build Status

Angular Infinite Scroll

Inspired by ng-infinite-scroll directive for angular.

Angular Support

Supports Angular > 4
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

Angular Consulting Services

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

  • consulting to companies and startups on how to approach code in their projects and keep it maintainable.
  • I provide project bootstrapping and development - while afterwards, I integrate it on site and guide the team on it.

Contact Me Here

Installation

npm install ngx-infinite-scroll --save

Supported API

Currently supported attributes:

  • infiniteScrollDistance<number> - (optional, default: 2) - should get a number, the number of viewport lengths from the bottom of the page at which the event will be triggered.
  • infiniteScrollUpDistance<number> - (optional, default: 1.5) - should get a number
  • infiniteScrollThrottle<number> - (optional, default: 300) - should get a number of milliseconds for throttle. The event will be triggered this many milliseconds after the user stops scrolling.
  • infiniteScrollContainer<string|HTMLElement> - (optional, default: 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.
  • scrolled<function> - this will callback if the distance threshold has been reached on a scroll down.
  • scrolledUp<function> - (event: InfiniteScrollEvent) - this will callback if the distance threshold has been reached on a scroll up.
  • scrollWindow<boolean> - (optional, default: 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, default: 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, default: false) - doesn't invoke the handler if set to true
  • horizontal<boolean> - (optional, default: false) - sets the scroll to listen for horizontal events
  • alwaysCallback<boolean> - (optional, default: false) - instructs the scroller to always trigger events
  • fromRoot<boolean> - (optional, default: false) - if infiniteScrollContainer is set, this instructs the scroller to query the container selector from the root of the document object.

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

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

ngx-infinite-scroll's People

Contributors

andrewmnlv avatar be-ndee avatar bernardopacheco avatar borowskt avatar dylan-smith avatar emeyekayee avatar hongbo-miao avatar kylethen avatar nerumo avatar orizens avatar robisim74 avatar teleaziz avatar wandersonalves avatar ymatus avatar zoehneto avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.