floating card
A perspective-based floating card animation that responds to mouse movement
> COMPONENTS:
> motion.div
> useMotionValue
> useSpring
> useTransform
> KEYWORDS:
perspective3dmouse trackingspring physics
> SOURCE CODE:
"use client"
import { motion, useMotionValue, useSpring, useTransform } from "framer-motion"
import { MouseEvent } from "react"
const FloatingCard = () => {
const x = useMotionValue(0)
const y = useMotionValue(0)
const mouseXSpring = useSpring(x)
const mouseYSpring = useSpring(y)
const rotateX = useTransform(mouseYSpring, [-0.5, 0.5], ["17.5deg", "-17.5deg"])
const rotateY = useTransform(mouseXSpring, [-0.5, 0.5], ["-17.5deg", "17.5deg"])
const handleMouseMove = (e: MouseEvent<HTMLDivElement>) => {
const rect = e.currentTarget.getBoundingClientRect()
const width = rect.width
const height = rect.height
const mouseX = e.clientX - rect.left
const mouseY = e.clientY - rect.top
const xPct = mouseX / width - 0.5
const yPct = mouseY / height - 0.5
x.set(xPct)
y.set(yPct)
}
const handleMouseLeave = () => {
x.set(0)
y.set(0)
}
return (
<motion.div
className="w-40 h-40"
onMouseMove={handleMouseMove}
onMouseLeave={handleMouseLeave}
style={{
rotateY,
rotateX,
transformStyle: "preserve-3d",
}}
>
<div
style={{
transform: "translateZ(75px)",
transformStyle: "preserve-3d",
}}
className="relative h-full w-full rounded-xl bg-black/50 shadow-2xl"
/>
</motion.div>
)
}
export default FloatingCard