Logo

dev-resources.site

for different kinds of informations.

How to build 3 simple animation with framer motion

Published at
2/9/2023
Categories
react
webdev
tutorial
framermotion
Author
rardooba
Author
8 person written this
rardooba
open
How to build 3 simple animation with framer motion

Animations are an essential part of any user interface, and Framer Motion is a powerful library for creating animations in React. It provides a simple and performant way to add animations to your components. In this article, we will show you how to build three simple animations using Framer Motion for React.

1. Fading in and out

The first animation we will create is a simple fade in and out animation. We will use the animate property and the opacity property to create this animation. First, we need to import the motion.div component from Framer Motion. We will use this component to wrap our div that we want to animate. Then, we can use the animate property to define the animation and the opacity property to define the initial and final states of the animation.

import { motion } from 'framer-motion';

function FadeInOut() {
  return (
    <motion.div
      animate={{ opacity: [0, 1, 0] }}
      transition={{ duration: 2, loop: Infinity }}
    >
      <p>Fading in and out</p>
    </motion.div>
  );
}
Enter fullscreen mode Exit fullscreen mode

In the example above, we are telling Framer Motion to animate the opacity of the wrapped div from 0 to 1 and back to 0. We also define a transition with a duration of 2 seconds and a loop that repeats infinitely.

2. Move along a path

The second animation we will create is a simple animation that moves an element along a path. We will use the motion.path component to define the path, and the animate property to move the element along the path.

import { motion } from 'framer-motion';

function MoveAlongPath() {
  return (
    <motion.path
      d="M10,10 L90,90"
      animate={{ pathLength: [0, 1] }}
      transition={{ duration: 2 }}
    >
      {(path) => (
        <motion.div
          animate={{ pathLength: [0, 1] }}
          style={{ path, position: 'absolute' }}
        >
          <p>Moving along a path</p>
        </motion.div>
      )}
    </motion.path>
  );
}
Enter fullscreen mode Exit fullscreen mode

In this example, we defined a path using the d attribute and set the pathLength property to [0,1], which makes the element move along the path from start to end. We also defined a transition with a duration of 2 seconds. The component wrapped inside the path component uses the path prop and position absolute to move along the path

3. Rotating

The third animation we will create is a simple rotation animation. We will use the animate property and the rotate property to create this animation.

import { motion } from 'framer-motion';

function Rotating() {
  return (
    <motion.div
      animate={{ rotate: [0, 360] }}
      transition={{ duration: 2, loop: Infinity }}
    >
      <p>Rotating</p>
    </motion.div>
  );
}
Enter fullscreen mode Exit fullscreen mode

In this example, we are telling Framer Motion to animate the rotation of the div from 0 to 360 degrees, and we also defined a transition with a duration of 2 seconds and a loop that repeats.

That's it ! See U next level !

framermotion Article's
30 articles in total
Favicon
Building a food simulation game with Next.js and generative design
Favicon
How to Create a Flipping Card Animation Using Framer Motion
Favicon
State-of-the-Art Web Design with Remix, Tailwind, and Framer Motion
Favicon
How I Created a Stunning Portfolio with Next.js, Tailwind CSS, and Framer Motion
Favicon
Creating the iMessage Card Stack Animation: The Interactive Layer
Favicon
Creating the iMessage Card Stack Animation: The TimelineΒ Design
Favicon
How to build awesome website with stunning animation?
Favicon
Adding motion to 3D models with Framer Motion and Three.js
Favicon
Creating a Smooth Animated Menu with React and Framer Motion
Favicon
My-Portfolio
Favicon
Watch Out For Broken Links, 404 Page With Framer Motion, TailwindCSS and NextJs
Favicon
Could not find a declaration file for module framer-motion Error in Next.js 14
Favicon
How to add Animations and Transitions in React
Favicon
Creating a text writing animation in React using Framer-Motion
Favicon
Unveiling My Portfolio Website: A Fusion of Tech and Creativity πŸš€
Favicon
React developer portfolio template use framer motion to animate components
Favicon
Building a mini casual game with Next.js
Favicon
Creating a Multi-Level Sidebar Animation with Tailwind and Framer Motion in React
Favicon
Easy Animated Tabs in ReactJS with Framer Motion
Favicon
A Tinder-like card game with Framer-Motion
Favicon
Elevating Web Design with Framer Motion: Bringing Your Website to Life
Favicon
Animating the Web: Route Transitions in Next.js with Framer Motion
Favicon
Enhancing Form Usability with Framer Motion: A Guide to Animated, Chunked Form Transitions
Favicon
How to Build a fully accessible Accordion with HeadlessUI, Framer Motion, and TailwindCSS
Favicon
Build Portfolio Theme in Tailwind CSS & Next.js
Favicon
How to create Scroll-Linked Animations with React and Framer Motion πŸ’ƒπŸ»πŸ•ΊπŸ»
Favicon
How to build 3 simple animation with framer motion
Favicon
How to create an awesome navigation menu using chakra-UI and framer-motion.
Favicon
Make a slide open envelope
Favicon
Animate everything with Framer Motion

Featured ones: