@smoovy/parallax
Predictable parallax effects
Installation
yarn add @smoovy/parallax
or
npm install --save @smoovy/parallax
Usage
A parallax item is simply a point that moves by the mangament of the state inside a context.
So if a state is updated all parallax items assgined to the context will be updated as well.
Creating a parallax item
import { parallax } from '@smoovy/parallax';
const item = parallax({
speed: { y: 0.3 },
context: 'custom-optional-context',
element: document.querySelector('[data-parallax]'),
onUpdate: (state, progress) => {
console.log('current progress', progress.y * 100);
console.log('current shift', state.shiftY);
}
});
Updating the state
States are always assigned to a context. The default context is named default
.
This ensure that you can have different groups of items and calculations with
the same API.
import { Parallax } from '@smoovy/parallax';
window.addEventListener('scroll', () => {
Parallax.update({
scrollX: window.scrollX,
scrollY: window.scrollY,
viewWidth: window.innerWidth,
viewHeight: window.innerHeight,
maxWidth: document.body.offsetWidth,
maxHeight: document.body.offsetHeight
}, 'custom-optional-context' );
});
You should consider all of these calls (e.g. with @smoovy/observer)
More options
parallax({
context?: string;
culling?: boolean;
masking?: boolean;
normalize?: boolean;
speed?: Partial<Coordinate>;
element?: HTMLElement | {
target: HTMLElement;
transform?: boolean;
};
onUpdate?: (state: ParallaxState, progress: Coordinate) => void;
})
Choosing the perfect speed value
The speed value defines how fast the point will move relative to it's initial position:
0
means static. No movement
1
means sticky. It moves with the scroll position
-1
means "reverse sticky". It moves against the scroll position.
everything in between is a fraction of the scroll position (negative or positive).
License
See the LICENSE file for license rights and limitations (MIT).