What is @motionone/dom?
@motionone/dom is a powerful and flexible library for creating animations and transitions in web applications. It provides a simple API to animate DOM elements with various effects, including transforms, opacity changes, and more. The library is designed to be performant and easy to use, making it a great choice for adding dynamic visual effects to your web projects.
What are @motionone/dom's main functionalities?
Basic Animation
This feature allows you to animate basic properties of DOM elements. In this example, the element with the class 'box' is animated to move 100 pixels to the right over a duration of 1 second.
import { animate } from '@motionone/dom';
animate('.box', { transform: 'translateX(100px)' }, { duration: 1 });
Keyframe Animations
This feature allows you to create more complex animations using keyframes. In this example, the element with the class 'box' moves to the right and then back to its original position over a duration of 2 seconds.
import { animate } from '@motionone/dom';
animate('.box', [
{ transform: 'translateX(0px)' },
{ transform: 'translateX(100px)' },
{ transform: 'translateX(0px)' }
], { duration: 2 });
Staggered Animations
This feature allows you to create staggered animations for multiple elements. In this example, elements with the class 'box' will fade in one after another with a delay of 0.1 seconds between each.
import { animate, stagger } from '@motionone/dom';
animate('.box', { opacity: [0, 1] }, { delay: stagger(0.1) });
Scroll-based Animations
This feature allows you to trigger animations based on the scroll position. In this example, elements with the class 'box' will fade in when they come into view.
import { animate, inView } from '@motionone/dom';
inView('.box', () => {
animate('.box', { opacity: [0, 1] });
});
Other packages similar to @motionone/dom
animejs
Anime.js is a lightweight JavaScript animation library with a simple, yet powerful API. It supports various types of animations, including CSS properties, SVG, DOM attributes, and JavaScript objects. Compared to @motionone/dom, Anime.js offers a broader range of animation capabilities but may have a steeper learning curve.
gsap
GSAP (GreenSock Animation Platform) is a robust and highly performant animation library. It provides a wide range of features for animating DOM elements, including complex timelines, easing functions, and more. GSAP is known for its performance and flexibility, making it a popular choice for professional-grade animations. Compared to @motionone/dom, GSAP offers more advanced features and greater control over animations.
framer-motion
Framer Motion is a popular animation library for React applications. It provides a declarative API for creating animations and transitions, making it easy to integrate with React components. Framer Motion is designed to work seamlessly with React, offering features like layout animations and gesture-based interactions. Compared to @motionone/dom, Framer Motion is more specialized for React and offers a more integrated experience for React developers.