Search.../

onViewportEnter( )

Adds an event handler that runs when an element is displayed in the viewable part of the current window.

Description

An element enters the viewport when the page is scrolled to show any part of the element. An element also enters the viewport if it was hidden or collapsed and is then shown or expanded in the viewable part of the current window. onViewportEnter() is not fired for hidden or collapsed elements even if they are scrolled into view.

Deprecation note: The $w parameter of event handlers is being deprecated. To get a scoped selector for working with elements in repeater items, use the $w.at() function and pass it the context property of the event parameter: $item = $w.at(event.context). To learn more, see here.

Syntax

function onViewportEnter(handler: EventHandler): Element
handler: function EventHandler(event: Event, $w: $w): void

onViewportEnter Parameters

NAME
TYPE
DESCRIPTION
handler

EventHandler

The name of the function or the function expression to run when the element enters the viewport.

Returns

The element on which the event is now registered.

Return Type:

EventHandler Parameters

NAME
TYPE
DESCRIPTION
event

Event

The event that occurred.

$w

$w

Deprecated: A selector function. The $w function was used to enable event handlers to work with elements in repeaters. Now, to get a scoped selector for working with repeater items, use the $w.at() function and pass it the context property of the event parameter: $item = $w.at(event.context).

Returns

This function does not return anything.

Return Type:

void
Mixed in from:$w.FormElement

Related Content:

Was this helpful?

Get the ID of the element that has entered the viewport

Code Example

Copy Code
1$w("#myElement").onViewportEnter( (event) => {
2 let targetId = event.target.id; // "myElement"
3});