Skip to content

budacode/ng2-scrollspy

 
 

Repository files navigation

npm version Build Status Codacy Badge Coverage Status devDependency Status

Github Releases

You can use this angular2 service to spy scroll events from window or any other scrollable element.

This library implements an service to collect observables from scroll spy directives. It can be used to create you own components or if you prefer use on of the following components that leverage this library functionality to accomplish different behaviors:

  • index: create and display and index from content inside and element.
  • affix: make element follow scroll inside its parent.
  • parallax: create very simple parallax effects based on scroll.
  • infinite: infinite scroll

Installation

First you need to install the npm module:

npm install ng2-scrollspy --save

If you use SystemJS to load your files, you might have to update your config with this if you don't use defaultJSExtensions: true:

System.config({
	packages: {
		"/ng2-scrollspy": {"defaultExtension": "js"}
	}
});

Finally, you can use ng2-scrollspy in your Angular 2 project. It is recommended to instantiate ScrollSpyService in the bootstrap of your application and to never add it to the "providers" property of your components, this way you will keep it as a singleton. If you add it to the "providers" property of a component it will instantiate a new instance of the service that won't be initialized.

import {ScrollSpyService} from 'ng2-scrollspy';

bootstrap(AppComponent, [
	ScrollSpyService
]);

Using

Spy window scroll

Use ScrollSpyDirective to spy on window.

import {Component, View, Injectable, AfterViewInit} from 'angular2/core';
import {ScrollSpyDirective, ScrollSpyService} from 'ng2-scrollspy';

@Injectable()
@Component({
	selector: 'app'
})
@View({
	template: `<div scrollSpy></div>`,
	directives: [ScrollSpyDirective]
})
export class AppComponent implements AfterViewInit {
	constructor(scrollSpyService: ScrollSpyService) {
		this.scrollSpyService = scrollSpyService;
	}

	ngAfterViewInit() {
		this.scrollSpyService.getObservable('window').subscribe((e: any) => {
			console.log('ScrollSpy::window: ', e);
		});
	}
}

Spy any element scroll

Use ScrollSpyElementDirective to spy on any element. You must git a unique id to each instance.

import {Component, View, Injectable, AfterViewInit} from 'angular2/core';
import {ScrollSpyElementDirective, ScrollSpyService} from 'ng2-scrollspy';

@Injectable()
@Component({
	selector: 'yourComponent'
})
@View({
	template: `
	<div scrollSpyElement="test" style="max-height: 100px; overflow: auto;">
		<div style="height: 500px;"></div>
	</div>`,
	directives: [ScrollSpyElementDirective]
})
export class YourComponent implements 
 {
	constructor(scrollSpyService: ScrollSpyService) {
		this.scrollSpyService = scrollSpyService;
	}

	ngAfterViewInit() {
		this.scrollSpyService.getObservable('test').subscribe((e: any) => {
			console.log('ScrollSpy::test: ', e);
		});
	}
}

Because ScrollSpyService is a singleton, you can get any ScrollSpy observable from anywhere withing your application.

TODO:

  • Documentation/examples webpage
  • Unit tests
  • Test all browsers
    • Chrome
    • Firefox
    • Safari
    • IE
    • edge
    • opera
    • Mobile

License

MIT

Packages

No packages published

Languages

  • TypeScript 82.3%
  • JavaScript 17.4%
  • Shell 0.3%