ProductPromotion
Logo

Angular.JS

made by https://0x3d.site

GitHub - fidian/ngx-visibility: Angular module that detects when elements are visible. Uses IntersectionObserver.
Angular module that detects when elements are visible. Uses IntersectionObserver. - fidian/ngx-visibility
Visit Site

GitHub - fidian/ngx-visibility: Angular module that detects when elements are visible. Uses IntersectionObserver.

GitHub - fidian/ngx-visibility: Angular module that detects when elements are visible. Uses IntersectionObserver.

NgxVisibility

Angular 18.x library to monitor when elements are visible in the DOM. When you have a huge list of components, this is more performant than other libraries because it keeps the number of observers to a minimum. It uses IntersectionObserver to do the work.

If you only care about when elements are resized, including resize events due to browser window size changing, look at ngx-resize-observer.

If you want to be notified when DOM elements change properties, ngx-mutation-observer would be a good pick.

Demonstration

There's a live demo over at CodeSandbox.io.

Installation

Install like other Angular libraries. First run a command to download the module.

npm install ngx-visibility

Next, add the module to your project.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';

// Import the module
import { NgxVisibilityModule } from 'ngx-visibility';

import { AppComponent } from './app.component';

@NgModule({
    declarations: [AppComponent, ItemComponent, NgxVisibilityDirective],

    // Include the module.
    imports: [BrowserModule, FormsModule, NgxVisibilityModule],
    providers: [NgxVisibilityService],
    bootstrap: [AppComponent]
})
export class AppModule {}

Finally, you leverage the service directly or use some directives for common uses.

NgxVisibilityLazyLoadDirective

Load an image or a background image when the element is visible.

<img src="myImage.png" ngxVisibilityLazyLoad />

<img srcset="myImage.png 1x, betterImage.png 2x" ngxVisibilityLazyLoad />

<div backgroundImage="myImage.png" style="height: 100px; width: 82px" ngxVisibilityLazyLoad></div>

Supports src and srcset for images, backgroundImage for all HTML elements. It's highly recommended that you set at least a placeholder size (possibly with min-height and min-width) for elements. Once the item is flagged visible, the observer is removed and the item stays visible.

Configuration is allowed through ngxVisibilityAnchorDirective, ngxVisibilityMargin and ngxVisibilityThreshold, which are discussed below.

NgxVisibilityDirective

Emit a boolean when an item becomes visible or is hidden from view.

<my-component (ngxVisibility)="updateVisibility($event)"></my-component>

Listener is automatically added and removed by the directive.

Configuration is allowed through ngxVisibilityAnchorDirective, ngxVisibilityMargin and ngxVisibilityThreshold, which are discussed below.

NgxVisibilityService

This service maintains the list of observers and calls callbacks when items become visible or are invisible.

import { Component, ElementRef, OnDestroy, OnInit } from '@angular/core';
import { NgxVisibilityService } from 'ngx-visibility';

@Component({
    selector: 'my-component'
})
export class MyComponent implements OnDestroy, OnInit {
    constructor(
        private readonly ngxVisibilityService: NgxVisibilityService,
        private readonly elementRef: ElementRef
    ) {}

    ngOnInit() {
        this.ngxVisibilityService.observe(
            // The native element reference
            this.elementRef.nativeElement,

            // A callback that is called whenever the element crosses
            // a threshold. When you use thresholds, tracking how many
            // have been crossed is up to you.
            isVisible => {
                console.log('I am now', isVisible ? 'visible' : 'hidden');
            },

            // The configuration object is optional. Look at
            // IntersectionObserver for what these values mean.
            // The config is of type IntersectionObserverInit.
            {
                // The viewport native element to use as our window.
                // Defaults to `window`.
                root: null,

                // How far outside the viewport to extend. Useful for
                // loading items that are almost going to be seen.
                // Negative margins will not load items until they
                // progress that far into the view window.
                // Must be specified in pixels or percent and use
                // the typical CSS margin formats.
                rootMargin: '40px',

                // Thresholds. Default is [ 0 ]. Can be either a number or
                // an array of thresholds. Values are from 0 (not visible)
                // to 1 (completely visible).
                threshold: [ 0, .33, .66 ]
            }
        );
    }

    ngOnDestroy() {
        this.ngxVisibiltyService.unobserve(this.elementRef.nativeElement);
    }
}

NgxVisibilityAnchorDirective

Used to flag a viewport instead of using the whole window. When using this directive, the component must also use the ngxVisibility or ngxVisibilityLazyLoad directive, otherwise this directive has no effect. Really, that's not too bad but it is a little wasteful because resources will be loading before you want them.

<div ngxVisibilityAnchorDirective style="overflow: scroll; height: 40px; width: 40px">
    <div *ngFor="let item in itemList" ngxVisibility="setItemVisibility(item, $event)">
        {{ item.name }} is visible? {{ item.visibility }}
    </div>
</div>

ngxVisibilityMargin

This attribute is used with ngxVisibility or ngxVisibilityLazyLoad in order to set the margin when observing that element. Margins are specified as per the CSS properties and must be measured in pixels or percent.

<!-- Load when it is within 100px of being seen. -->
<img src="cool-image.png" ngxVisibilityLazyLoad ngxVisibilityMargin="100px" />

<!-- Load if the element is within half of a screen away from being seen. -->
<div (ngxVisibility)="setVisibility($event)" ngxVisibilityMargin="50%" />

ngxVisibilityThreshold

Sets up one or more thresholds when combined with ngxVisibility or ngxVisibilityLazyLoad. It accepts an array, a number, or a string that will be converted to a number. Numbers need to be within the range of 0 to 1.

<div ngxVisibility="setVisibility($event)" ngxVisibilityThreshold="1">...content...</div>

<div ngxVisibility="setVisibility($event)" [ngxVisibilityThreshold]="[ 0, .5 ]">...content...</div>

License

This project is licensed under an MIT license.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory