Convert Figma logo to code with AI

wagerfield logoparallax

Parallax Engine that reacts to the orientation of a smart device

16,455
2,135
16,455
11

Top Related Projects

A smooth 3D tilt javascript library.

7,064

Lightweight, vanilla javascript parallax library

Finally, a "back to top" button that behaves like a real elevator.

10,422

Simple & lightweight (<4kb gzipped) vanilla JavaScript library to create smooth & beautiful animations when you scroll.

9,901

Reveal CSS animation as you scroll down a page

49,575

JavaScript animation engine

Quick Overview

Parallax is a lightweight JavaScript library for creating interactive parallax effects on websites. It allows developers to easily add depth and movement to web pages by animating elements based on mouse movement or device orientation.

Pros

  • Easy to use with minimal setup required
  • Supports both mouse and device orientation-based parallax effects
  • Lightweight and performant, with no dependencies
  • Highly customizable with various configuration options

Cons

  • Limited to parallax effects only, not a full-featured animation library
  • May not work well on older browsers or devices
  • Can potentially cause motion sickness for some users if overused
  • Documentation could be more comprehensive

Code Examples

  1. Basic parallax setup:
var scene = document.getElementById('scene');
var parallaxInstance = new Parallax(scene);
  1. Configuring parallax options:
var scene = document.getElementById('scene');
var parallaxInstance = new Parallax(scene, {
  relativeInput: true,
  clipRelativeInput: false,
  hoverOnly: true
});
  1. Adding parallax to specific elements:
<div id="scene">
  <div data-depth="0.2">Layer 1</div>
  <div data-depth="0.6">Layer 2</div>
  <div data-depth="1.0">Layer 3</div>
</div>

Getting Started

  1. Include the Parallax.js library in your HTML:
<script src="https://cdnjs.cloudflare.com/ajax/libs/parallax/3.1.0/parallax.min.js"></script>
  1. Create a container element with child elements for parallax layers:
<div id="scene">
  <div data-depth="0.2">Layer 1</div>
  <div data-depth="0.6">Layer 2</div>
</div>
  1. Initialize Parallax in your JavaScript:
document.addEventListener('DOMContentLoaded', function() {
  var scene = document.getElementById('scene');
  var parallaxInstance = new Parallax(scene);
});

Competitor Comparisons

A smooth 3D tilt javascript library.

Pros of vanilla-tilt.js

  • Lightweight and dependency-free, making it easier to integrate into projects
  • Supports both mouse and gyroscope input for a wider range of devices
  • Offers more customization options and event callbacks

Cons of vanilla-tilt.js

  • Limited to tilt effects, whereas Parallax offers a broader range of parallax animations
  • May require more manual setup for complex scenes compared to Parallax's built-in features

Code Comparison

vanilla-tilt.js:

VanillaTilt.init(document.querySelector(".your-element"), {
    max: 25,
    speed: 400,
    glare: true,
    "max-glare": 0.5
});

Parallax:

var scene = document.getElementById('scene');
var parallaxInstance = new Parallax(scene, {
    relativeInput: true,
    clipRelativeInput: false
});

Both libraries offer simple initialization, but vanilla-tilt.js provides more options in a single call, while Parallax focuses on scene-based setup.

7,064

Lightweight, vanilla javascript parallax library

Pros of Rellax

  • Lightweight (1.1kb gzipped) compared to Parallax's larger file size
  • Simpler API and easier to implement for basic parallax effects
  • Supports horizontal parallax scrolling out of the box

Cons of Rellax

  • Less feature-rich than Parallax, with fewer customization options
  • Limited support for mobile devices and touch events
  • Lacks built-in debugging tools that Parallax offers

Code Comparison

Rellax implementation:

var rellax = new Rellax('.rellax', {
  speed: -2,
  center: false,
  wrapper: null,
  round: true,
  vertical: true,
  horizontal: false
});

Parallax implementation:

var scene = document.getElementById('scene');
var parallaxInstance = new Parallax(scene, {
  relativeInput: true,
  clipRelativeInput: false,
  hoverOnly: true,
  inputElement: document.getElementById('input-element')
});

Both libraries offer straightforward implementation, but Parallax provides more options for customization and input handling. Rellax's simpler API makes it easier to set up basic parallax effects quickly, while Parallax offers more control over the parallax behavior and supports a wider range of use cases.

Finally, a "back to top" button that behaves like a real elevator.

Pros of elevator.js

  • Lightweight and focused on a single, specific functionality (smooth scrolling to top)
  • Easy to implement with minimal configuration
  • Includes a playful audio feature for enhanced user experience

Cons of elevator.js

  • Limited in scope compared to Parallax's versatile effects
  • Less customizable and fewer animation options
  • May not be suitable for complex scrolling interactions or multi-directional scrolling

Code Comparison

elevator.js:

var elevator = new Elevator({
  element: document.querySelector('.elevator-button'),
  targetElement: document.querySelector('#elevator-target'),
  duration: 1000
});

Parallax:

var scene = document.getElementById('scene');
var parallaxInstance = new Parallax(scene, {
  relativeInput: true,
  clipRelativeInput: false,
  hoverOnly: true
});

Summary

elevator.js is a simple, focused library for smooth scrolling to the top of a page, while Parallax offers a more comprehensive solution for creating parallax effects and interactive backgrounds. elevator.js is easier to implement but less versatile, whereas Parallax provides more customization options and complex animations at the cost of a steeper learning curve.

Both libraries serve different purposes and can be chosen based on the specific needs of a project. elevator.js is ideal for quick, simple scrolling functionality, while Parallax is better suited for creating immersive, interactive web experiences with depth and movement.

10,422

Simple & lightweight (<4kb gzipped) vanilla JavaScript library to create smooth & beautiful animations when you scroll.

Pros of lax.js

  • Smaller file size and lighter weight (3.5KB vs 14KB for Parallax)
  • More flexible and customizable with a wider range of animation options
  • Easier to implement complex animations without extensive JavaScript knowledge

Cons of lax.js

  • Less established community and fewer resources compared to Parallax
  • May require more initial setup and configuration for basic parallax effects
  • Documentation could be more comprehensive for advanced use cases

Code Comparison

lax.js:

lax.addDriver('scrollY', function () {
  return window.scrollY
})

lax.addElements('.selector', {
  scrollY: {
    translateY: [
      ["elInY", "elOutY"],
      [0, 100],
    ]
  }
})

Parallax:

var scene = document.getElementById('scene');
var parallaxInstance = new Parallax(scene, {
  relativeInput: true,
  clipRelativeInput: false,
  hoverOnly: true
});

Both libraries offer ways to create parallax effects, but lax.js provides more granular control over animations, while Parallax focuses on simpler, out-of-the-box parallax implementations. lax.js requires more specific configuration, whereas Parallax can be set up with minimal code for basic effects.

9,901

Reveal CSS animation as you scroll down a page

Pros of WOW

  • Lightweight and easy to implement, focusing on scroll-triggered animations
  • Supports a wide range of CSS animations out of the box
  • Customizable animation settings and offset options

Cons of WOW

  • Limited to scroll-based animations, lacking the depth and variety of Parallax
  • Requires CSS knowledge for creating custom animations
  • Less active development and community support compared to Parallax

Code Comparison

WOW:

new WOW().init();

<div class="wow fadeInUp" data-wow-duration="2s" data-wow-delay="1s">
  Animated content
</div>

Parallax:

var scene = document.getElementById('scene');
var parallaxInstance = new Parallax(scene, {
  relativeInput: true
});

Key Differences

  • WOW focuses on scroll-triggered animations, while Parallax offers more complex depth and motion effects
  • Parallax provides a richer set of features for creating immersive, layered experiences
  • WOW is generally easier to implement for simple scroll animations, whereas Parallax requires more setup but offers greater flexibility

Use Cases

  • Choose WOW for simple, scroll-based reveal animations and lightweight implementations
  • Opt for Parallax when creating more complex, interactive, and depth-oriented visual experiences
49,575

JavaScript animation engine

Pros of Anime

  • More versatile animation library, capable of animating CSS properties, SVG, DOM attributes, and JavaScript objects
  • Smaller file size (16.8KB vs 27.9KB minified)
  • Better documentation and examples

Cons of Anime

  • Steeper learning curve due to more complex API
  • Less focused on parallax effects specifically
  • May require more setup for simple parallax animations

Code Comparison

Parallax:

var scene = document.getElementById('scene');
var parallax = new Parallax(scene);

Anime:

anime({
  targets: '.element',
  translateY: '100px',
  duration: 2000,
  easing: 'easeInOutQuad'
});

Summary

Parallax is a specialized library for creating parallax scrolling effects, while Anime is a more general-purpose animation library. Parallax is easier to use for quick parallax implementations, but Anime offers greater flexibility for various animation types. Anime has a smaller file size and better documentation, but may require more setup for simple parallax effects. The choice between the two depends on the specific project requirements and the desired level of animation complexity.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Parallax.js

CDNJS

Parallax Engine that reacts to the orientation of a smart device. Where no gyroscope or motion detection hardware is available, the position of the cursor is used instead.

Check out the demo to see it in action!

Table of Contents

1. Getting started

1.1 Installation

1.1 a) Using the CDN

  1. Add <script src="https://cdnjs.cloudflare.com/ajax/libs/parallax/3.1.0/parallax.min.js"></script> to your markup
  2. Done!

Many thanks to the fine folks over at cdnjs for hosting our library.

1.1 b) Beginners

  1. Head over to the releases Section
  2. Download compiled.zip from the latest release
  3. Extract the ZIP archive and locate the parallax.js and parallax.min.js files
    • Use parallax.js if you want to snoop around in the code
    • Use parallax.min.js for deployment, because it has a smaller file size
  4. Copy the file of your choice into your project directory
  5. So far, so good!

1.1 c) Professionals

npm i -s parallax-js

You will then find the source code in node_modules/parallax-js/src/parallax.js and the browserified, babelified, uglified, production-ready version in node_modules/parallax-js/dist/parallax.min.js

1.2 Preparations

Include the Script

If you use the compiled version, either downloaded from the releases page, or copied from the dist folder, include the script like any other Javascript library:
<script src="path/to/parallax.js"></script>

Of course, when you've installed via npm, and use browserify/babel, you can also simply do:
import Parallax from 'parallax-js' or
const Parallax = require('parallax-js')

Create your HTML elements

Each Parallax.js instance needs a container element, the scene. You're free to identify it by any means you want, but for now, let's use an ID:

<div id="scene">
</div>

Per default, all direct child elements of the scene will become moving objects, the layers. You can change this to a custom query selector, but again, we're going with the easiest approach for now:

<div id="scene">
  <div>My first Layer!</div>
  <div>My second Layer!</div>
</div>

While all other options and parameters are optional, with sane defaults, and can be set programatically, each layer needs a data-depth attribute. The movement applied to each layer will be multiplied by its depth attribute.

<div id="scene">
  <div data-depth="0.2">My first Layer!</div>
  <div data-depth="0.6">My second Layer!</div>
</div>

1.3 Run Parallax

As soon as your DOM is ready and loaded, you can create a new Parallax.js instance, providing your scene element as first parameter.

var scene = document.getElementById('scene');
var parallaxInstance = new Parallax(scene);

That's it, you're running Parallax.js now!

2. Configuration

2.1 Programmatic vs Declarative

Most configuration settings can be declared either as data-value attribute of the scene element, or property of the configuration object. The programmatic approach will take priority over the data-value attributes set in the HTML.
Some options can also be set at run-time via instance methods.

Declarative:

<div data-relative-input="true" id="scene">
  <div data-depth="0.2">My first Layer!</div>
  <div data-depth="0.6">My second Layer!</div>
</div>

Programmatic:

var scene = document.getElementById('scene');
var parallaxInstance = new Parallax(scene, {
  relativeInput: true
});

Using Methods at Runtime:

parallaxInstance.friction(0.2, 0.2);

2.2 Configuration Options

relativeInput

Property: relativeInput
Attribute: data-relative-input

Value: boolean
Default: false

Makes mouse input relative to the position of the scene element.
No effect when gyroscope is used.

clipRelativeInput

Property: clipRelativeInput
Attribute: data-clip-relative-input

Value: boolean
Default: false

Clips mouse input to the bounds of the scene. This means the movement stops as soon as the edge of the scene element is reached by the cursor.
No effect when gyroscope is used, or hoverOnly is active.

hoverOnly

Property: hoverOnly
Attribute: data-hover-only

Value: boolean
Default: false

Parallax will only be in effect while the cursor is over the scene element, otherwise all layers move back to their initial position. Works best in combination with relativeInput.
No effect when gyroscope is used.

inputElement

Property: inputElement
Attribute: data-input-element
Method: setInputElement(HTMLElement)

Value: null or HTMLElement / String
Default: null

Allows usage of a different element for cursor input.
The configuration property expects an HTMLElement, the data value attribute a query selector string.
Will only work in combination with relativeInput, setting hoverOnly might make sense too.
No effect when gyroscope is used.

calibrateX & calibrateY

Property: calibrateX & calibrateY
Attribute: data-calibrate-x & data-calibrate-y
Method: calibrate(x, y)

Value: boolean
Default: false for X, true for Y

Caches the initial X/Y axis value on initialization and calculates motion relative to this.
No effect when cursor is used.

invertX & invertY

Property: invertX & invertY
Attribute: data-invert-x & data-invert-y
Method: invert(x, y)

Value: boolean
Default: true

Inverts the movement of the layers relative to the input. Setting both of these values to false will cause the layers to move with the device motion or cursor.

limitX & limitY

Property: limitX & limitY
Attribute: data-limit-x & data-limit-y
Method: limit(x, y)

Value: false or integer
Default: false

Limits the movement of layers on the respective axis. Leaving this value at false gives complete freedom to the movement.

scalarX & scalarY

Property: scalarX & scalarY
Attribute: data-scalar-x & data-scalar-y
Method: scalar(x, y)

Value: float
Default: 10.0

Multiplies the input motion by this value, increasing or decreasing the movement speed and range.

frictionX & frictionY

Property: frictionX & frictionY
Attribute: data-friction-x & data-friction-y
Method: friction(x, y)

Value: float between 0 and 1
Default: 0.1

Amount of friction applied to the layers. At 1 the layers will instantly go to their new positions, everything below 1 adds some easing.
The default value of 0.1 adds some sensible easing. Try 0.15 or 0.075 for some difference.

originX & originY

Property: originX & originY
Attribute: data-origin-x & data-origin-y
Method: origin(x, y)

Value: float between 0 and 1
Default: 0.5

X and Y origin of the mouse input. The default of 0.5 refers to the center of the screen or element, 0 is the left (X axis) or top (Y axis) border, 1 the right or bottom.
No effect when gyroscope is used.

precision

Property: precision
Attribute: data-precision

Value: integer
Default: 1

Decimals the element positions will be rounded to. 1 is a sensible default which you should not need to change in the next few years, unless you have a very interesting and unique setup.

selector

Property: selector
Attribute: data-selector

Value: null or string
Default: null

String that will be fed to querySelectorAll on the scene element to select the layer elements. When null, will simply select all direct child elements.
Use .layer for legacy behaviour, selecting only child elements having the class name layer.

pointerEvents

Property: pointerEvents
Attribute: data-pointer-events

Value: boolean
Default: false

Set to true to enable interactions with the scene and layer elements. When set to the default of false, the CSS attribute pointer-events: none will be applied for performance reasons.
Setting this to true alone will not be enough to fully interact with all layers, since they will be overlapping. You have to either set position: absolute on all layer child elements, or keep pointerEvents at false and set pointer-events: all for the interactable elements only.

onReady

Property: onReady

Value: null or function
Default: null

Callback function that will be called as soon as the Parallax instance has finished its setup. This might currently take up to 1000ms (calibrationDelay * 2).

3. Methods

In addition to the configuration methods outlined in the section above, there are a few more publicly accessible methods:

enable()

Enables a disabled Parallax instance.

disable()

Disables a running Parallax instance.

destroy()

Completely destroys a Parallax instance, allowing it to be garbage collected.

version()

Returns the version number of the Parallax library.

4. Development

4.1 Running the Project

  1. Clone the Repository git clone git@github.com:wagerfield/parallax.git
  2. Open the working directory cd parallax
  3. Install dependencies npm install
  4. Run development server gulp watch
  5. Open http://localhost:9000/ in browser

4.2 Opening an Issue

If you need help relating the direct usage of this library in a project of yours, provide us with a working, running example of your work. This can be a GitHub repository, a ZIP file containing your work, a project on CodePen or JSFiddle, you name it.
Do not complain about something not working without giving us some way to help you. Thank you!

4.3 Known Issues

SVG-Bug in MS Edge

It seems MS Edge does not support the children or querySelectorAll methods for SVG elements.

Animation running really slow

Depending on your site, the GPU might have a bit too much to do. You can try adding the CSS definition will-change: transform to the layer elements to speed things up. Use sparingly!

Gyroscope not working on Android

Android will only allow access to the gyroscope o secure origins (that is, with https protocol).

Gyroscope not working on iOS

Because gyroscope data had been abused to track users, it's disabled on iDevices by default and needs to be enabled by the users. You can try asking for permission via DeviceOrientationEvent.requestPermission.

Do something like:

DeviceOrientationEvent
  .requestPermission()
  .then(() => {
    new Parallax(scene)
  })

Unable to manually set position of layers

Since this often lead to issues, this library forces the positioning of the layers to be absolute. If you need to override this, add !important to your CSS positioning.

5. FAQ

How can I use this Library with jQuery?

jQuery will not prevent you from using this library in any way. If you want to use jQuery for selecting your Parallax scene element, you can do so too.

var scene = $('#scene').get(0);
var parallaxInstance = new Parallax(scene);

How can I interact with my layers?

Check out the section on the configuration option pointerEvents above.

How do I get the demo files to work?

Either download compiled_with_examples.zip from the GitHub Releases section, or follow section 4.1

6. Information

6.1 License

This project is licensed under the terms of the MIT License. Enjoy!

6.2 Authors

Matthew Wagerfield: @wagerfield
René Roth: Website

NPM DownloadsLast 30 Days