Intersection observer for Angular
npm install ngx-intersection-observerObserve visiblity of elements and get notified when they visit/leave the window viewport.
## Features
- Observe elements via IntersectionObserver API or fallback to scroll listener to detect if the element enters/leaves the viewport.
- Apply or remove classes based on element visiblity
- Specify threshold, how many precentage of the element need to be visible to trigger intersection.
- Specify intersection method (IntersectionObserver API or scroll listener)
- Event binding for intersecting elements.
## Dependencies
---
ngx-intersection-observer | Angular
--- | ---
1.0.13 | >=13.x
## Install
---
Install the package via NPM
``shell`
$ npm install --save ngx-intersection-observer
`
Import the module
Import the module to your angular applicationtypescript
//...
import { IntersectionObserverModule } from 'ngx-intersection-observer';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
IntersectionObserverModule.forRoot({
debounce: 50,
threshold: 30,
autoRemove: true
} as IntersectionObserverConfig)
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
`
---
Name | Type | Description | Optional | Default
--- | --- | --- | --- | ---
visitClass | String | List of classes to apply, when the element visits the windows viewport. | YesString
leaveClass | | List of classes to apply, when the element leaves the window viewport. | YesString
removeVisitClass | | List of classes to remove, when the element visits the window viewport.String
removeLeaveClass | | List of classes to remove, when the element leaves the windows viewport. | YesBoolean
autoRemove | | true / false If true classes will be removed automatically when the element leaves the viewport, otherwhise use removeVisitClass property. | Yes | trueBoolean
useScroll | | true / false If true, use scoll listener otherwhise use IntersectionObserver. By default IntersectionBehavior is used, fallback to scroll listener. | Yes | trueNumber
threshold | | Specifies how many precentage of the element need to be visible in the viewport to treat it as intersection. Specify a value between 0% and 100% | Yes | 30%Event
intersection | | Function which is called when the element enters/leaves the viewport. | Yes
Example
`html
[visitClass]="'bg-blue border'"
[autoRemove]="true"
[threshold]="30"
[useScroll]="false"
(intersection)="intersect($event)"
class="bg-red"
id="my-div">
`css
#my-div {
width: 300px;
height: 300px;
margin-top: 2000px;
}.bg-red {
background-color: red;
}
.bg-blue{
background-color: blue;
}
.border{
border: 5px solid black;
`Use with animate.css
---
Animate.css is a library of ready-to-use, cross-browser animations for use in your web projects. Great for emphasis, home pages, sliders, and attention-guiding hints.
If you want to apply animations from animate.style, use those two classes to prevent flickering.
`css
.hide-on-init {
visibility: hidden;
}.animate__animated {
visibility: visible !important;
}
`
Example
`html
[visitClass]="'bg-blue border'"
[autoRemove]="true"
[threshold]="30"
[useScroll]="false"
(intersection)="intersect($event)"
class="bg-red hide-on-init"
id="my-div">---
MIT
---
> GitHub @tobiasschiebel | Twitter @SchiebelTobias