A lightweight library for Svelte that adds viewport-triggered transitions to your elements. Perfect for creating engaging scroll animations and micro-interactions.
- π― Triggers animations when elements enter the viewport
- π¨ Includes 6 customizable transitions
- π Supports all Svelte easing functions
- π¦ Zero dependencies (besides Svelte)
- π TypeScript support with full type inference
- πΌοΈ Prevents layout shifts with placeholder elements
npm install svelte-microanimations
# or
pnpm add svelte-microanimations
# or
yarn add svelte-microanimations<script>
import { microanimation } from 'svelte-microanimations';
import { fadeSlide } from 'svelte-microanimations/transitions';
import { cubicOut } from 'svelte/easing';
</script>
<div
use:microanimation={{
transition: fadeSlide,
params: { duration: 800, easing: cubicOut }
}}
>
This will animate when it enters the viewport
</div>Each transition accepts common parameters plus its own specific parameters:
{
duration?: number; // Duration in milliseconds
delay?: number; // Delay before animation starts
easing?: Function; // Svelte easing function
}Combines fade with directional movement.
type DirectionalParams = {
y?: number; // Vertical slide distance in pixels
x?: number; // Horizontal slide distance in pixels
}
// Usage
<div use:microanimation={{
transition: fadeSlide,
params: {
duration: 800,
easing: cubicOut,
y: 50, // Slides up 50px
x: 0 // No horizontal movement
}
}} />Scales and fades element into view.
type ScaleParams = {
start?: number; // Initial scale value (0 to 1)
opacity?: number; // Initial opacity value (0 to 1)
}
// Usage
<div use:microanimation={{
transition: popScale,
params: {
duration: 700,
easing: elasticOut,
start: 0.95, // Starts slightly smaller
opacity: 0 // Starts fully transparent
}
}} />Rotates and fades element into view.
type RotateParams = {
degrees?: number; // Rotation amount in degrees
y?: number; // Vertical offset in pixels
}
// Usage
<div use:microanimation={{
transition: spinIn,
params: {
duration: 600,
easing: cubicOut,
degrees: 180, // Half rotation
y: 20 // Small upward movement
}
}} />Reveals content with a sliding mask effect.
// Only uses common parameters
// Usage
<div use:microanimation={{
transition: revealSlide,
params: {
duration: 800,
easing: cubicInOut
}
}} />Scales in with a bouncy effect.
// Only uses common parameters
// Usage
<div use:microanimation={{
transition: bounceScale,
params: {
duration: 800,
easing: elasticOut // Best with elastic or bounce easing
}
}} />Flips element in 3D space.
// Only uses common parameters
// Usage
<div use:microanimation={{
transition: flip3D,
params: {
duration: 800,
easing: cubicOut
}
}} />Different transitions work best with specific easing functions:
fadeSlide:cubicOut,quartOutpopScale:elasticOut,bounceOutspinIn:cubicOut,quartOutrevealSlide:cubicInOut,quartInOutbounceScale:elasticOut,bounceOutflip3D:cubicOut,quartOut
Import easing functions from Svelte:
import { cubicOut, cubicInOut, quartOut, quartInOut, elasticOut, bounceOut } from 'svelte/easing';The microanimation action accepts the following options:
type MicroanimationParameters = {
transition: TransitionFunction; // The transition to apply
params?: TransitionParams; // Parameters for the transition
threshold?: number; // Viewport intersection threshold (0 to 1)
once?: boolean; // Whether to trigger only once
};duration: Animation duration in milliseconds (default varies by transition)delay: Delay before animation starts in milliseconds (default: 0)easing: Svelte easing function (default varies by transition)
The library includes full TypeScript support with type inference for transition parameters:
import type { TransitionParamsMap } from 'svelte-microanimations';
// TypeScript will show available parameters for spinIn
use:microanimation<'spinIn'>{{
transition: spinIn,
params: {
duration: 800,
easing: bounceOut,
degrees: 180, // TypeScript knows this is available
y: 20 // And this too
}
}}<script>
import { microanimation } from 'svelte-microanimations';
import { fadeSlide } from 'svelte-microanimations/transitions';
import { cubicOut } from 'svelte/easing';
let items = ['Item 1', 'Item 2', 'Item 3'];
</script>
{#each items as item, i}
<div
use:microanimation={{
transition: fadeSlide,
params: {
duration: 600,
delay: i * 100,
y: 50,
easing: cubicOut
}
}}
>
{item}
</div>
{/each}<script>
import { microanimation } from 'svelte-microanimations';
import { fade } from 'svelte/transition';
import { elasticOut } from 'svelte/easing';
const customTransition = (node, params) => ({
...fade(node, params),
easing: elasticOut
});
</script>
<div
use:microanimation={{
transition: customTransition,
params: { duration: 1000 }
}}
>
Custom transition
</div>- Requires browsers that support the Intersection Observer API and Web Animations API
- Modern browsers (Chrome, Firefox, Safari, Edge)
MIT
Contributions are welcome! Please feel free to submit a Pull Request.