Was macht Framer Motion Skill?
Produktionsreife Animationen für React-Anwendungen.
Schnellstart
Installation
npm install framer-motion
pnpm add framer-motion
Grundlegende Verwendung
import { motion } from "framer-motion";
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 0.5 }}
>
Inhalt
</motion.div>
Kernkonzepte
Beispiele
Templates
Kurzreferenz
Basis-Animation
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -20 }}
transition={{ duration: 0.3 }}
>
Inhalt
</motion.div>
Hover & Tap
<motion.button
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
transition={{ type: "spring", stiffness: 400, damping: 17 }}
>
Klick mich
</motion.button>
Varianten
const container = {
hidden: { opacity: 0 },
show: {
opacity: 1,
transition: { staggerChildren: 0.1 }
}
};
const item = {
hidden: { opacity: 0, y: 20 },
show: { opacity: 1, y: 0 }
};
<motion.ul variants={container} initial="hidden" animate="show">
{items.map(i => (
<motion.li key={i} variants={item}>{i}</motion.li>
))}
</motion.ul>
AnimatePresence (Exit-Animationen)
import { AnimatePresence, motion } from "framer-motion";
<AnimatePresence mode="wait">
{isVisible && (
<motion.div
key="modal"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
>
Modal-Inhalt
</motion.div>
)}
</AnimatePresence>
Scroll-Trigger
<motion.div
initial={{ opacity: 0, y: 50 }}
whileInView={{ opacity: 1, y: 0 }}
viewport={{ once: true, margin: "-100px" }}
transition={{ duration: 0.5 }}
>
Animiert, wenn in den Viewport gescrollt wird
</motion.div>
Drag (Ziehen)
<motion.div
drag
dragConstraints={{ left: -100, right: 100, top: -100, bottom: 100 }}
dragElastic={0.1}
>
Zieh mich
</motion.div>
Layout-Animation
<motion.div layout layoutId="shared-element">
Inhalt, der bei Layoutänderungen animiert
</motion.div>
Transition-Typen
transition={{ duration: 0.3, ease: "easeOut" }}
transition={{ type: "spring", stiffness: 300, damping: 20 }}
transition={{ type: "spring", bounce: 0.25 }}
transition={{ type: "inertia", velocity: 50 }}
Easing-Funktionen
ease: "linear"
ease: "easeIn"
ease: "easeOut"
ease: "easeInOut"
ease: "circIn"
ease: "circOut"
ease: "circInOut"
ease: "backIn"
ease: "backOut"
ease: "backInOut"
ease: [0.17, 0.67, 0.83, 0.67]
Reduzierte Bewegung
Respektieren Sie immer die Benutzereinstellungen:
import { motion, useReducedMotion } from "framer-motion";
function Component() {
const prefersReducedMotion = useReducedMotion();
return (
<motion.div
initial={{ opacity: 0, y: prefersReducedMotion ? 0 : 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: prefersReducedMotion ? 0 : 0.3 }}
>
Respektiert Bewegungseinstellungen
</motion.div>
);
}
const variants = {
initial: { opacity: 0 },
animate: { opacity: 1 },
};
<motion.div
variants={variants}
initial="initial"
animate="animate"
className="motion-reduce:transition-none"
>
Häufige Muster
Einblenden nach oben
const fadeInUp = {
initial: { opacity: 0, y: 20 },
animate: { opacity: 1, y: 0 },
transition: { duration: 0.4 }
};
<motion.div {...fadeInUp}>Inhalt</motion.div>
Gestaffelte Liste
const container = {
hidden: { opacity: 0 },
show: {
opacity: 1,
transition: { staggerChildren: 0.1, delayChildren: 0.2 }
}
};
const item = {
hidden: { opacity: 0, x: -20 },
show: { opacity: 1, x: 0 }
};
Modales Fenster
<AnimatePresence>
{isOpen && (
<>
{/* Hintergrund */}
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className="fixed inset-0 bg-black/50"
onClick={onClose}
/>
{/* Modal */}
<motion.div
initial={{ opacity: 0, scale: 0.95 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.95 }}
className="fixed inset-x-4 top-1/2 -translate-y-1/2 ..."
>
Modal-Inhalt
</motion.div>
</>
)}
</AnimatePresence>
Akkordeon
<motion.div
initial={false}
animate={{ height: isOpen ? "auto" : 0 }}
transition={{ duration: 0.3, ease: "easeInOut" }}
className="overflow-hidden"
>
<div className="p-4">Akkordeon-Inhalt</div>
</motion.div>
Best Practices
- Varianten verwenden: Saubererer Code, einfachere Orchestrierung
- Reduzierte Bewegung respektieren: Immer
useReducedMotion prüfen
layout sparsam einsetzen: Kann teuer sein, nur bei Bedarf verwenden
- Exit-Animationen: Mit
AnimatePresence umschließen
- Spring für Interaktionen: Natürlicheres Gefühl bei Hover/Tap
- Tween für Seitenübergänge: Vorhersehbareres Timing
- GPU-beschleunigte Properties:
opacity, scale, x, y gegenüber width, height bevorzugen