0.0.16 • Published 8 years ago

budacode-ng2-scrollspy v0.0.16

Weekly downloads
4
License
MIT
Repository
github
Last release
8 years ago

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 - x Chrome - Firefox - Safari - IE - edge - opera - Mobile

License

MIT

0.0.16

8 years ago

0.0.15

8 years ago

0.0.14

8 years ago

0.0.13

8 years ago

0.0.12

8 years ago

0.0.11

8 years ago

0.0.10

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago