Skip to main content
cngx-src documentation

CngxIntersectionObserver

DirectivePrimaryv0.1.0

projects/common/layout/observers/intersection-observer.directive.ts

Import#

import { CngxIntersectionObserver } from '@cngx/common/layout'

Description#

Observes whether the host element is visible in the viewport or a scroll container.

Wraps the browser's IntersectionObserver API and exposes its state as Angular signals. The observer is automatically recreated when root, rootMargin, or threshold inputs change, and disconnected on destroy.

Use cases: lazy loading images, infinite scroll sentinels, scroll-triggered animations, "back to top" buttons, analytics viewport tracking.

Lazy load sentinel

<div cngxIntersectionObserver #io="cngxIntersectionObserver"
     (entered)="loadNextPage()">
  @if (io.isIntersecting()) { Loading... }
</div>

Scroll-triggered animation

<section cngxIntersectionObserver #io="cngxIntersectionObserver"
         [threshold]="0.5"
         [class.visible]="io.isIntersecting()">
  Content fades in at 50% visibility
</section>

Custom scroll container

<div cngxIntersectionObserver [root]="'.scroll-container'" [rootMargin]="'100px'">
  Observed within a scrollable parent
</div>

https://cngxjs.github.io/cngx/examples/#/common/layout/intersection-observer/scroll-sentinel

Metadata#

Index#

Inputs#

root#string | null
input()

CSS selector for the scroll container root. null (default) uses the viewport.

default null
rootMargin#string
input()

Margin around the root, using CSS margin syntax (e.g. '100px 0px').

default '0px'
threshold#number | number[]
input()

Visibility ratio(s) at which the callback fires. 0 = any pixel visible, 1 = fully visible.

default 0

Outputs#

entered#void
output()

Emitted once when the element enters the observed area.

intersectionChange#IntersectionObserverEntry
output()

Emitted on every intersection change with the raw IntersectionObserverEntry.

left#void
output()

Emitted once when the element leaves the observed area.