DirectiveAdded in v16.7Optional

wrInView

Emit when an element enters or leaves the viewport.

Installation

Import the class. This entry point has no stylesheet.

import { Component } from '@angular/core';
import { WrInView } from 'ngwr/directives';

@Component({ imports: [WrInView] })
export class MyComponent {}

Usage

(wrInView) carries true on entering and false on leaving. An element that starts out of view emits nothing until it shows up.

<section (wrInView)="seen.set($event)" [wrInViewThreshold]="0.5">…</section>

<img wrInViewOnce (wrInView)="track()" src="hero.webp" alt="" />

Scroll down

Scroll down

Out of view

Scroll up

Scroll up

One observer

Every instance with the same threshold shares one IntersectionObserver. It is created with the first element and disconnected with the last. Nothing is observed on the server.

API

NameDescriptionTypeDefault
(wrInView)true when the host enters the viewport, false when it leaves. Fires on a change only.boolean—
[wrInViewThreshold]Share of the host, from 0 to 1, that must be visible. Read once, on first render.number0
[wrInViewOnce]Stop watching after the first entry.booleanfalse

See also