Skip to content

Instantly share code, notes, and snippets.

@GraemeFulton
Created June 24, 2021 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save GraemeFulton/6b6e5885bf69682a720da0bd0970248b to your computer and use it in GitHub Desktop.
Save GraemeFulton/6b6e5885bf69682a720da0bd0970248b to your computer and use it in GitHub Desktop.
Framer Motion Image LazyLoad
import "./styles.css";
import { useState } from "react";
import { motion } from "framer-motion";
export default function App() {
const [imageLoading, setImageLoading] = useState(true);
const [pulsing, setPulsing] = useState(true);
const imageLoaded = () => {
setImageLoading(false);
setTimeout(() => setPulsing(false), 600);
};
return (
<div className="App">
<div
className={`${pulsing ? "pulse" : ""} loadable`}
style={{ width: "600px", background: "#ccc" }}
>
<motion.img
initial={{ height: "16rem", opacity: 0 }}
// style={{ height: imageLoading ? "6rem" : "auto" }}
animate={{
height: imageLoading ? "16rem" : "auto",
opacity: imageLoading ? 0 : 1
}}
transition={
({ height: { delay: 0, duration: 0.4 } },
{ opacity: { delay: 0.5, duration: 0.4 } })
}
onLoad={imageLoaded}
width="100%"
src="https://source.unsplash.com/random"
/>
</div>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment