Logo

dev-resources.site

for different kinds of informations.

How to Create a Flipping Card Animation Using Framer Motion

Published at
12/22/2024
Categories
flippingcard
framermotion
animation
react
Author
graciesharma
Author
12 person written this
graciesharma
open
How to Create a Flipping Card Animation Using Framer Motion

Link video : Flipping Card

Framer Motion is a popular library for creating smooth and customizable animations in React. In this blog, weโ€™ll walk through the steps to create a visually appealing flipping card animation. This card will flip between its front and back sides at regular intervals, using Framer Motion's powerful features.

Prerequisites
Before diving into the code, ensure you have the following:

Basic knowledge of React.
A React project set up (using tools like Create React App or Next.js).
Framer Motion installed in your project.
You can install it with:
npm install framer-motion

"use client";

import React, { useState, useEffect } from "react";
import { motion } from "framer-motion";
import CardFront from "./card-front";
import CardBack from "./card-back";

const FlippingCard = () => {
  const [isFlipped, setIsFlipped] = useState(false);

  useEffect(() => {
    const interval = setInterval(() => {
      setIsFlipped((prev) => !prev);
    }, 2000);

    return () => clearInterval(interval);
  }, []);

  return (
    <motion.div
      className="card-container"
      style={{
        width: "454px",
        height: "271px",
        perspective: "1000px", // Adds depth for 3D animation
      }}
    >
      <motion.div
        className="card"
        animate={{ rotateY: isFlipped ? 180 : 0 }} // Animates the flip
        transition={{ duration: 1 }} // Controls the flip speed
        style={{
          width: "100%",
          height: "100%",
          position: "relative",
          transformStyle: "preserve-3d", // Enables 3D effect
        }}
      >
        {/* Front Side */}
        <motion.div
          className="card-front"
          style={{
            position: "absolute",
            backfaceVisibility: "hidden", // Ensures only one side is visible
            width: "100%",
            height: "100%",
            display: "flex",
            justifyContent: "center",
            alignItems: "center",
          }}
        >
          <CardFront />
        </motion.div>

        {/* Back Side */}
        <motion.div
          className="card-back"
          style={{
            position: "absolute",
            backfaceVisibility: "hidden",
            transform: "rotateY(180deg)", // Flips the back face
            width: "100%",
            height: "100%",
            display: "flex",
            justifyContent: "center",
            alignItems: "center",
          }}
        >
          <CardBack />
        </motion.div>
      </motion.div>
    </motion.div>
  );
};

export default FlippingCard;

Enter fullscreen mode Exit fullscreen mode

Key Features of This Component

3D Effect with Perspective:The perspective property in the outer container ensures the flipping motion feels realistic by adding depth.

Dynamic Rotation: The rotateY property toggles between 0 (front) and 180 (back) degrees to create the flip animation.

Smooth Animation: The transition property in Framer Motion controls the flip speed and smoothness.

Automatic Flip: A setInterval hook toggles the isFlipped state every 2 seconds for continuous flipping. You can replace this with user-triggered events like onClick or onHover.

Backface Visibility: backfaceVisibility: hidden ensures that only the visible side is displayed while flipping.

How to Customize the Flipping Card

Content: Replace CardFront and CardBack components with custom designs or dynamic content.

Dimensions: Modify width and height in the styles to fit your design needs.

Transition Speed: Adjust the duration in the transition property for faster or slower flips.

Flip Trigger: Replace the setInterval with an event handler.

animation Article's
30 articles in total
Favicon
[Boost]
Favicon
What is the scope of multimedia and animation?
Favicon
[Translations] Implementing Animations with Throttle in JavaScript
Favicon
Top Aviation Courses in Bangalore: Best Aviation Training Institutes for Career Growth in 2025
Favicon
Using Forced Reflows, the Event Loop, and the Repaint Cycle to Slide Open a Box
Favicon
Houdini animations USED IN APPS
Favicon
Getting Started with CSS Animations: A Beginner's Guide ๐ŸŽจโœจ
Favicon
Some platforms to post your animations on the web
Favicon
Sci-fi Text Animation
Favicon
Counter - A React library for animating numeric transitions
Favicon
Terminal Animations with Node.js
Favicon
Enjoy editing your videos with fun animations, premium effects, and pro features unlocked by downloading the Alight Motion Mod APK 2024 for free. https://alightmotionproapks.com/
Favicon
Learn how to create an animated avatar stack widget with Tailwind CSS
Favicon
Framer Motion Animated Hero with Reactjs
Favicon
Learn how to create an animated expanding search bar with Tailwind CSS
Favicon
ใ€ŒMac็•…็Žฉ้ธฟ่’™ไธŽ็กฌไปถ19ใ€้ธฟ่’™UI็ป„ไปถ็ฏ‡9 - ่‡ชๅฎšไน‰ๅŠจ็”ปๅฎž็Žฐ
Favicon
[Boost]
Favicon
Learn how to create an animated profile card with Tailwind CSS
Favicon
Learn how to create a cool animated envelope with Tailwind CSS
Favicon
How to Create a Flipping Card Animation Using Framer Motion
Favicon
Learn how to create an animated input field with Tailwind CSS
Favicon
Dynamic Box Shadow on Mouse Move
Favicon
Interactive Snowfall Cursor Effect with CSS and JavaScript
Favicon
How to Create Scroll Animation for Text in Your Website
Favicon
Add Crazy Cursor Click Effect on your website..
Favicon
Create Eye-Catching Button Effect with Rotating Glow Animation
Favicon
Discover the Ultimate Resource: Dog Breed Hub
Favicon
A Cozy Thanksgiving Feast with Hello Kitty and the Turkey ๐Ÿฆƒ๐Ÿ‚
Favicon
Build A Stunning Animated Travel Image Slider Using Html, Css, And JavaScript!
Favicon
testimonials with slide effects

Featured ones: