Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
Framer-Motion Introduction and Installation
Next article icon

Framer-Motion Introduction and Installation

Last Updated : 21 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Framer-motion is an open-source, production-ready animation and gesture library for React. It provides a high-level API that simplifies adding animations and gestures while keeping the code minimal and readable.

Unlike CSS animations or other animation libraries, Framer Motion integrates seamlessly with React and offers features like gesture handling, layout animations, and drag animations. This makes it a preferred choice for building fluid UI interactions, dynamic animations, and responsive user experiences.

Key Features of Framer Motion

  • Declarative Animations: Motion uses a simple, declarative API, allowing developers to define animations directly in their JSX code, making it intuitive and easy to work with.
  • Smooth Transitions: The library provides built-in support for smooth transitions between different states of a component (like opacity, position, scale, and rotation), ensuring a seamless user experience.
  • Gesture-based Animations: Framer Motion allows you to trigger animations based on user gestures, such as hover, tap, drag, or while the user is scrolling. This creates more interactive and dynamic UI elements.
  • Page Transitions: The library supports smooth page transitions, which can be especially useful in single-page applications (SPAs) to enhance the user experience when navigating between views or routes.
  • Customizable and Flexible: Framer Motion offers extensive control over animations with properties like timing, easing functions, and variants, allowing you to create complex and customized animations that fit your needs.

Components of Framer Motion

  • Motion Components: Motion components (e.g., <motion.div/>, <motion.circle/>) are the core of the Motion API, allowing you to animate any HTML or SVG element with additional props to handle gestures and animations easily.
  • Animate Presence: This component is used for unmounting animations. It animates elements when they are removed from the React tree, providing smooth transitions during component removal.
  • Layout Group: LayoutGroup groups motion components that should perform layout animations together, allowing for smooth transitions and animations when components change their layout within the parent container.
  • Lazy Motion: LazyMotion helps reduce the bundle size by loading motion component features synchronously or asynchronously, optimizing performance by only loading the required features.
  • Reorder: The Reorder component enables drag-to-reorder functionality, perfect for creating reorderable lists or items like to-do lists or tabs with simple drag-and-drop behaviour.

Steps to Install and Implement Framer Motion

Step 1: Creating React Application

Create a React application using the following command.

npx create-react-app demo
cd demo

Step 2: Install Framer Motion

First, ensure that you have the Framer Motion library installed. Open your terminal and run the following command:

npm install framer-motion

Step 3: Import motion from Framer Motion

In your App.js file, you need to import the motion component from the framer-motion library to animate the elements.

import { motion } from "framer-motion";

Step 4: Create a Motion Component

You can now replace your standard JSX tags with their animated versions provided by Framer Motion.

In this example, we are using <motion.div> instead of <div>. This allows us to apply animations to the <div> element.

JavaScript
<motion.div style={{     color: 'green',     fontSize: 20,     width: '300px',     height: '30px',     textAlign: 'center',     border: '2px solid green',     margin: '40px' }}>     GeeksforGeeks </motion.div> 

Step 4: Add Hover Effect

Framer Motion makes it easy to add interactive animations.

In this example, we're adding a whileHover animation, which will be triggered when the user hovers over the element. Here, we are applying the scale property inside whileHover to scale the element down when the mouse hovers over it:

JavaScript
<motion.div     style={{         color: 'green',         fontSize: 20,         width: '300px',         height: '30px',         textAlign: 'center',         border: '2px solid green',         margin: '40px'     }}     whileHover={{ scale: 0.5 }}  >     GeeksforGeeks </motion.div> 

Final App.js file

JavaScript
//App.js  import React from "react"; import { motion } from "framer-motion";  function App() {     return (         <motion.div style={{             color: 'green',             fontSize: 20,             width: '300px',             height: '30px',             textAlign: 'center',             border: '2px solid green',             margin: '40px'         }}              whileHover={{ scale: 0.5 }}         >             GeeksforGeeks         </motion.div>     ); }  export default App; 
  • Motion Component: The motion.div is a Framer Motion component that adds animation capabilities to the regular div element. It allows you to animate its properties such as scale, opacity, and position.
  • Hover Animation: The whileHover prop is used to animate the div when the user hovers over it. In this case, the scale property is set to 0.5, causing the element to shrink on hover.
  • Inline Styling: The div has inline styles applied to it, including color, font size, width, height, border, and margin, making it visually distinct and positioned in the center of the page.

Output

Example of framer motion

Framer Motion Events

Framer Motion provides a set of event handlers that can be used to control and respond to animations. These events give you more control over when and how animations happen. They can be used for triggering specific actions or behaviors during the lifecycle of an animation.

Here are some key Framer Motion events:

1. onAnimationComplete

The onAnimationComplete event is triggered once the animation finishes. This is helpful when you want to perform an action or trigger something after an animation has completed, such as navigating to a new page, showing a success message, or triggering another animation.

JavaScript
<motion.div     initial={{ opacity: 0 }}     animate={{ opacity: 1 }}     onAnimationComplete={() => console.log("Animation completed!")} >     Content fades in </motion.div> 

2. onUpdate

The onUpdate event is triggered continuously during the animation’s lifecycle. We can use this event to track the progress of an animation or trigger real-time updates based on animation values, such as dynamically updating a progress bar.

JavaScript
<motion.div     animate={{ x: 100 }}     transition={{ duration: 2 }}     onUpdate={(latest) => console.log(latest)} >     Moving div </motion.div> 

3. onHoverStart and onHoverEnd

These events are triggered when the user interacts with an element by hovering over it (onHoverStart) and when the user stops hovering (onHoverEnd).

JavaScript
<motion.div     onHoverStart={() => console.log("Hovered!")}     onHoverEnd={() => console.log("Hover ended!")}     whileHover={{ scale: 1.2 }} >     Hover over me! </motion.div> 

4. onTapStart, onTap, and onTapEnd

These events are triggered during tap gestures on mobile devices, but they can also be used for clicks on any device.

JavaScript
<motion.button     onTapStart={() => console.log("Tap started")}     onTap={() => console.log("Tapped")}     onTapEnd={() => console.log("Tap ended")} >     Tap me! </motion.button> 

5. onDragStart, onDrag, and onDragEnd

These events are used with draggable elements. They allow you to handle the start, movement, and end of dragging actions.

JavaScript
<motion.div     drag     dragConstraints={{ left: 0, right: 200 }}     onDragStart={() => console.log("Drag started")}     onDrag={(e, info) => console.log("Dragging", info.point)}     onDragEnd={() => console.log("Drag ended")} >     Drag me around </motion.div> 

Conclusion

Framer Motion is a powerful and user-friendly animation library for React that simplifies the creation of smooth, interactive animations. Its declarative API, combined with features like Optimized for Performance, layout transitions, and Gestures and Interactions, makes it an ideal choice for building dynamic user interfaces. With easy installation and seamless integration into React projects, Framer Motion enhances the user interface and user experience of web applications while keeping the codebase simple and maintainable.


Next Article
Framer-Motion Introduction and Installation

T

tejaswaniagrawal23
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • Framer-motion

Similar Reads

    React-Motion Introduction & Installation
    The react-motion package is a JavaScript animation library for the React applications which can be used for creating smooth and interactive animations. This uses the spring-based physics model, through which we can define animations with natural and dynamic movements. This library is mainly suited f
    4 min read
    Foundation CSS Motion UI Installation
    Foundation CSS is a front-end framework that provides a consistent and customizable style guide for web development. It includes a wide range of features such as a responsive grid system, typography styles, form styling, and more. Motion UI is a Sass library for creating CSS transitions and animatio
    5 min read
    Foundation CSS Motion UI Animation
    A Foundation is an open-source and responsive front-end framework created by ZURB in September 2011 that makes it simple to create stunning responsive websites, apps, and emails that operate on any device. Many companies, like Facebook, eBay, Mozilla, Adobe, and even Disney, use it. The framework is
    5 min read
    Intorduction to React Motion
    Animation plays a vital role in modern web development, enhancing user experience and adding a layer of interactivity to web interfaces. React Motion is a popular animation library specifically designed for React applications. This article will explore React Motion in detail, covering its key featur
    4 min read
    Foundation CSS Orbit Using Animation
    Foundation CSS is an open-source front-end framework that makes it simple and quick to create an appealing responsive website, email, or app. ZURB released it in September 2011. Numerous businesses, like Facebook, eBay, Mozilla, Adobe, and even Disney, use it. This platform, which resembles SaaS, is
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences