New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

animate-when-visible

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

animate-when-visible

A tiny (2KB), no-dependency JavaScript module that adds classes to elements when they become visible, letting you control animations with CSS.

latest
Source
npmnpm
Version
1.0.8
Version published
Maintainers
1
Created
Source

Animate When Visible

npm downloads license

A tiny (2KB), no-dependency JavaScript module that adds a class to elements when they become visible, letting you control animations with CSS. Optionally, automatically add staggered timing between elements.

There are no default animations included, you define them yourself in CSS.

Example: https://animate-when-visible-example.netlify.app/

Features

  • Adds a class to elements when they enter the viewport
  • Supports staggered animations with automatic transition delays
  • Includes sorting logic so that elements fire based on their order in the DOM
  • Optionally handles dynamically added content via MutationObserver
  • Provides an optional callback function for each intersection
  • Provides lifecycle methods: destroy() and refresh()
  • Lightweight, no dependencies

Why another scroll animation module?

Because I couldn't find what I wanted in the existing options.

Most existing scroll animation libraries are either:

  • Heavy: They add a lot of extra JavaScript and CSS that aren’t necessary for simple visibility-triggered animations.
  • Outdated: Many are based on scroll listeners, which can be detrimental to peformance. This module uses IntersectionObserver and requestAnimationFrame to ensure light and performant effects.
  • Opinionated: They come bundled with animations that you would never use.

This module:

  • Provides the essential logic for visibility detection.
  • Leaves the animation as part of the CSS presentation layer.
  • Makes staggered animations dead simple with built-in delay handling.

Installation

npm install animate-when-visible

Usage

1. Import the module

import animateWhenVisible from 'animate-when-visible';

2. Initialize

animateWhenVisible();

By default, all elements with the .awv-animate class will have the configured animationClass added when they become visible.

3. Options

You can pass an options object:

const animator = animateWhenVisible({
  threshold: 0.1,
  staggerDelay: 100,
  staggerDelaySlow: 250,
  animationClass: 'awv-visible',
  staggerClass: 'awv-stagger',
  staggerSlowClass: 'awv-stagger-slow',
  targetSelector: '.awv-animate',
  staggerContainerSelector: '.awv-stagger-container',
  onVisible: null,
  observeMutations: false,
  animateOnScrollDownOnly: false,
});
OptionTypeDefaultDescription
thresholdNumber0.1IntersectionObserver threshold
staggerDelayNumber100Delay in ms between staggered elements
staggerDelaySlowNumber250Delay for slow-staggered elements
animationClassString'awv-visible'Class added when element is visible
staggerClassString'awv-stagger'Class marking elements for stagger
staggerSlowClassString'awv-stagger-slow'Class marking elements for slow stagger
targetSelectorString'.awv-animate'CSS selector for elements to animate
staggerContainerSelectorString'.awv-stagger-container'Selector for staggered element containers
onVisibleFunctionnullCallback called when element becomes visible
observeMutationsBooleanfalseObserve dynamically added elements
animateOnScrollDownOnlyBooleanfalseOnly animate when scrolling down

4. Destroy / Refresh

import { destroy, refresh } from 'animate-when-visible';
// Stop observers
destroy();

// Re-observe elements
refresh();

HTML Markup

Single element

<div class="awv-animate"></div>

Staggered elements

<div class="awv-stagger-container">
  <div class="awv-animate awv-stagger fade-in"></div>
  <div class="awv-animate awv-stagger fade-in"></div>
  <div class="awv-animate awv-stagger fade-in"></div>
</div>
  • .awv-stagger-container groups a set of staggered elements together. This prevents long delays when many elements appear on the screen at once.
  • .awv-stagger and .awv-stagger-slow control the stagger timing.

CSS

Add a transition or animation to your animationClass:

/* Fade in & slide up */
.fade-in {
  opacity: 0;
  transform: translateY(10px);
  transition: opacity 0.5s ease, transform 0.5s ease;
}

.fade-in.awv-visible {
  opacity: 1;
  transform: translateY(0);
}

/* Simple opacity fade */
.fade-in-opacity {
  opacity: 0;
  transition: opacity 1s ease;
}

.fade-in-opacity.awv-visible {
  opacity: 1;
}
  • Stagger delays are applied dynamically via JS as transition delays, so you don’t need to write CSS for each element.

Browser Support

  • Modern browsers with IntersectionObserver support.
  • Polyfills required for IE11 and older browsers.

License

MIT

Keywords

animate

FAQs

Package last updated on 23 Sep 2025

Did you know?

Socket

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