Socket
Socket
Sign inDemoInstall

tiny-parallax

Package Overview
Dependencies
3
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    tiny-parallax

Creating scroll-based animations often involves complex calculations and event handling. The `tiny-parallax` library simplifies this process by computing a parallax rate as elements move within the viewport. This rate can be utilized to dynamically adjust


Version published
Maintainers
1
Install size
18.3 kB
Created

Readme

Source

tiny-parallax GitHub license cov storybook npm PRs Welcome

Creating scroll-based animations often involves complex calculations and event handling. The tiny-parallax library simplifies this process by computing a parallax rate as elements move within the viewport. This rate can be utilized to dynamically adjust styles, enabling fluid and responsive transitions that are easily managed through CSS.

Live Demo

Experience the capabilities of tiny-parallax firsthand with our live demo:

View Demo

Getting Started

To incorporate tiny-parallax into your project, install the package via npm:

npm install tiny-parallax --save

How to Use

In a React Application

CSS-Based Animation
import { Frame, Layer } from "tiny-parallax";

const App = () => (
  <Frame>
    <Layer>
      <h1 style={{ opacity: "var(--parallax-rate)" }}>Hello, World!</h1>
    </Layer>
  </Frame>
);

This example demonstrates how to animate an element using CSS variables that are dynamically updated by tiny-parallax.

Imperative Animation
import { useRef } from "react";
import { Frame, Layer, useParallax } from "tiny-parallax";

const Video = () => {
  const video = useRef();

  useParallax((rate) => {
    if (video.current.duration)
      video.current.currentTime = rate * video.current.duration;
  });

  return (
    <video
      ref={video}
      src="https://assets.allsamplefiles.com/mp4/ns/60s/sample-file-sd.mp4"
    />
  );
};

const App = () => (
  <Frame>
    <Video />
  </Frame>
);

This example showcases an imperative approach to animation, where the current time of a video is controlled by the parallax rate.

Ranges

To specify the range within the viewport in which the frame should animate, utilize the range property. For instance, the setup below initiates the animation when the element enters the bottom of the screen and concludes it when the center of the frame aligns with the viewport's center:

import { Frame, Layer } from "tiny-parallax";

const App = () => (
  <Frame>
    <Layer range={(viewport, element) => [0, viewport / 2 + element / 2]}>
      <h1 style={{ opacity: "var(--parallax-rate)" }}>Hello, World!</h1>
    </Layer>
  </Frame>
);

For added convenience, the library offers a selection of predefined range functions that simplify the process of establishing animation boundaries. To delve deeper into these functions, examine the source code found at src/core/ranges.ts.

Explore the Ranges storybook for a visual guide and a better grasp of the predefined ranges.

Caveats

1. Why is my animation flickering?

This issue may be caused by the animation causing changes in the size of the element, which can affect the rate calculation. To prevent this, it is recommended to use the CSS property transform for animations. If it is necessary to change other properties, make sure they do not affect the element size during the animation.

2. Why is my animation not running smoothly?

This issue may be caused by a large number of elements or complex animations. To improve performance, it is recommended to optimize your animation by reducing the number of elements or simplifying the animation. Keep in mind that adding Frames can be costly as their dimensions need to be continuously calculated. On the other hand, using Layers or useParallax with a parent Frame is more efficient.

FAQs

Last updated on 07 Feb 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc