Skip to main content
Jarallax provides a comprehensive API for controlling parallax instances through events and methods.

Methods

Methods allow you to programmatically control Jarallax instances after initialization.

destroy

Completely removes the Jarallax effect and restores the element to its original state.
From the source code, the destroy method:
  1. Removes the parallax observer
  2. Restores original styles from data-jarallax-original-styles attribute
  3. Moves the image element back to its original position (if using <img> tag)
  4. Removes the parallax container element from the DOM
  5. Calls the onDestroy callback if defined
  6. Deletes the jarallax instance from the element
After calling destroy, you’ll need to reinitialize Jarallax if you want to use it again on the same elements.

isVisible

Checks if the parallax element is currently visible in the viewport.
Returns: boolean
This method returns true if the element is currently in the viewport, false otherwise.

onResize

Manually triggers the resize handler to recalculate and refit the parallax image.
Call onResize when:
  • The container element size changes dynamically
  • Content is loaded that affects layout
  • Window is resized programmatically
  • Elements are shown/hidden
  • Orientation changes on mobile devices
From the source code:

onScroll

Manually triggers the scroll handler to recalculate the parallax image position.
Jarallax automatically calls this method on window scroll. You only need to call it manually if you’re programmatically scrolling or need to force a position update.

Method Examples

Conditional Destroy

Dynamic Content Loading

Visibility-Based Actions

Events

Events allow you to hook into different stages of the parallax lifecycle.

onInit

Called after Jarallax initialization completes.
Inside event callbacks, this refers to the Jarallax instance with access to:
  • this.$item - The parallax container element
  • this.options - Current configuration options
  • this.image - Image data object
  • this.image.$container - The parallax image container
  • this.image.$item - The image element
  • this.$video - Video element (if using video extension)

onDestroy

Called after the Jarallax instance is destroyed.

onCoverImage

Called after the parallax image is covered/fitted to the container.
This event is triggered during initialization and whenever the container is resized.

onScroll

Called continuously during scrolling with detailed calculation data.
The onScroll event fires frequently during scrolling. Avoid heavy computations in this callback to maintain smooth performance.

onScroll Event Calculations

The onScroll event provides detailed information about the parallax element’s position and visibility.

Calculation Object

Output structure:

Calculation Properties Explained

Represents how much of the element is visible in the viewport.
  • 1.0: Fully visible
  • 0.5: Half visible
  • 0.0: Not visible
Position relative to the viewport center, ranging from -1 to 1.
  • -1: Top of element is at viewport center
  • 0: Center of element is at viewport center
  • 1: Bottom of element is at viewport center
The element’s DOMRect object containing position and dimensions:

Practical onScroll Examples

Video Extension Events

When using the video extension, additional events are available.

onVideoInsert

Called when the video element is inserted into the DOM.
Access the video element via this.$video in the callback.

onVideoWorkerInit

Called when the VideoWorker instance is initialized.
The VideoWorker instance provides these events:
  • ready - Video is loaded and ready to play
  • started - Video has started playing
  • play - Video play event
  • pause - Video pause event
  • ended - Video has ended
  • error - Video loading or playback error
These are handled internally by Jarallax, but you can add additional listeners in onVideoWorkerInit.

Complete API Example

Here’s a comprehensive example using methods and events:

TypeScript Support

Jarallax includes TypeScript definitions. Here’s how to use them:

Best Practices

1

Use events for initialization tasks

Perform setup work in onInit to ensure Jarallax is fully ready:
2

Clean up in onDestroy

Remove event listeners and custom elements in onDestroy:
3

Throttle onScroll callbacks

Limit heavy operations in onScroll to maintain performance:

Next Steps

Basic Usage

Learn the fundamentals of Jarallax

Advanced Options

Explore advanced configuration options