Created
July 20, 2024 18:46
-
-
Save koohz/3140be45ca98d498b3a36714351b6b03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { motion, motionValue, transform, useScroll, useTransform } from "framer-motion"; | |
import { useRef } from "react"; | |
const items = [ | |
// ... | |
] | |
const Component = () => { | |
const ref = useRef(null); | |
const { scrollYProgress } = useScroll({ | |
target: ref, | |
offset: ["start", "end"], | |
}); | |
// [0, 7] -> 7 is (total elements - 1) | |
const transformer = transform([0, 7], [-1, 1]); | |
return ( | |
<section ref={ref} className="h-[300vh] relative"> | |
<div className="h-screen flex items-center justify-center sticky top-0 bg-black"> | |
{items.map((item, idx) => { | |
const x = useTransform( scrollYProgress, [0, 1 ], [ 0, 500 * ( idx >= 8 ? -1 : 1) ]) | |
const y = useTransform( scrollYProgress, [0, 1 ], [ 0, 500 * transformer(idx % 8)]) | |
const zIndex = 10+idx%8; | |
const rotate = useTransform( scrollYProgress, [0, 1 ], [ -10 * idx, 10 * idx % 7 * idx % 4]); | |
const scale = useTransform( scrollYProgress, [0, 1 ], [1, .6]); | |
const style = { x, y, zIndex, scale, rotate } | |
return (<motion.div key={idx} className="h-[15rem] w-[15rem] absolute bg-red-500 shadow-large rounded-medium border" style={style}> | |
</motion.div>)} | |
)} | |
</div> | |
</section> | |
); | |
}; | |
export default Component; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment