Parallax

Animate CSS properties depending on the scroll position of the document.


Usage

To apply this component, add the data-parallax attribute to a container element. Add an option with the desired animation target value for each CSS property you want to animate. Clique provides a number of options that you can add to the data-parallax attribute:

Option Description
x animate translateX in pixels
xp animate translateX in percent
y animate translateY in pixels
yp animate translateY in percent
rotate animate rotation clockwise in degree
scale animate scaling
color animate color (needs start and stop value)
background-color animate background-color (needs start and stop value)
border-color animate border color (needs start and stop value)

Example

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.

Markup

<div data-parallax="{y: -200, opacity: 0}">...</div>

Start and stop values

Properties are always animated from the current value to the target value which you set in the option. However, you can also define a start value yourself. This is done by passing a string to the option which contains two values separated by comma.

NOTE Some properties, like colors, require a start and a stop value!

Example

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.

Markup

<div data-parallax="{x: '-100,100', 'background-color': '#EBF7FD,#FFF1F0'}">...</div>

Background image

You can also animate a background image. Just add the bg option with a (start and) stop value to the data-parallax attribute. If you use the bgp option, the given values will be interpreted as percent.

Example

Markup

<div data-parallax="{bg: '50,-50'}">...</div>

Nested animation

Using different animations for nested elements is a simple task. Just create another container within the first parallax container and add your options to a new data-parallax attribute.

Example

Title

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Markup

<div data-parallax="{bg: '50,-50'}">
    <div data-parallax="{scale: 0}">...</div>
</div>

Target Option

Usually, the animation lasts as long as the element itself is in the viewport. To start and stop the animation based on the viewport visibility of another element, use the target option. This can be helpful when using nested animations.

Example

This box is animated as long as the headline "Target Option" is in the viewport.

Markup

<!-- Using any target -->
<div data-parallax="{target: '#target-id', y: -50}">...</div>

<!-- Nested animation -->
<div id="target-id" data-parallax="{bg: '-50'}">
    <div data-parallax="{target: '#target-id', y: -50}">...</div>
</div>

Velocity

To adjust the easing of the animation, add the velocity option.

Example

0.04
0.2
0.6
1
2
3
4

Markup

<div data-parallax="{velocity: 0.5}">
    ...
</div>

Viewport

Using the viewport option, the animation duration can be adjusted. With the value 1 or false the animation lasts as long as the element is in the viewport. Setting it to 0.5, for example, animates the property only in the first half of the viewport.

Example

This animation is finished when the element reaches the middle of the viewport.

Markup

<div data-parallax="{viewport: 0.5}">...</div>

JavaScript options

Option Possible value Default Description
velocity float 0.5 Animation velocity during scrolling
target mixed false Element dimension reference for animation duration.
viewport float (0 to 1) false Animation range depending on the viewport.

Init element manually

var parallax = Clique.parallax(element, { /* options */ });