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.

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: