Floating.js is a lightweight library for creating animated floating elements on your webpage. Perfect for festive effects like snowflakes, sparkles, or any other decorative floating items. 🚀
- Customizable elements: Use text, emojis, or even HTML elements.
- Control animation frequency: Adjust the number of floating elements and the interval between appearances.
- Lightweight and modular: No external dependencies.
- Easy-to-use API: Start with minimal setup.
You can include Floating.js in your project via npm:
npm install --save @janmarkuslanger/floatingjs
Here’s how to get started with Floating.js:
import { Floating } from '@janmarkuslanger/floatingjs';
const floating = new Floating({
elements: ['❄️', '✨', '🎉', '⭐️'], // Elements to float (strings or custom HTML elements)
maxElements: 20, // Maximum number of floating elements (default: 10)
interval: 1000, // Interval in milliseconds between new elements (default: 2000)
});
// Start the animation
floating.start();
// Stop the animation
floating.stop();
Option | Type | Default | Description |
---|---|---|---|
elements |
string[] or Function[] |
[] |
Elements to float. Strings (e.g., '⭐️' ) or functions returning HTML elements. |
maxElements |
number |
10 |
Maximum number of floating elements visible at any time. |
interval |
number (ms) |
2000 |
Interval between the creation of new floating elements (in milliseconds). |
root |
HTMLElement |
document.body |
The container where floating elements will be appended. |
import { Floating } from '@janmarkuslanger/floatingjs';
const snowflakes = new Floating({
elements: ['❄️', '❅', '❆'], // Snowflake shapes
maxElements: 30,
interval: 500,
});
// Start the animation
snowflakes.start();
const customFloating = new Floating({
elements: [
'✨',
() => {
const customElement = document.createElement('div');
customElement.textContent = 'Custom!';
customElement.style.color = 'gold';
return customElement;
},
],
maxElements: 15,
interval: 800,
});
customFloating.start();
We welcome contributions! If you have suggestions, feature requests, or find a bug, please open an issue or submit a pull request.
This project is licensed under the MIT License.