A parallax-scroll component from Aceternity UI.
From Aceternity UI
1"use client";
2import { useScroll, useTransform } from "motion/react";
3import { useRef } from "react";
4import { motion } from "motion/react";
5
6import { cn } from "@/lib/utils";
7
8export const ParallaxScroll = ({
9 images,
10 className,
11}: {
12 images: string[];
13 className?: string;
14}) => {
15 const gridRef = useRef<any>(null);
16 const { scrollYProgress } = useScroll({
17 container: gridRef, // remove this if your container is not fixed height
18 offset: ["start start", "end start"], // remove this if your container is not fixed height
19 });
20
21 const translateFirst = useTransform(scrollYProgress, [0, 1], [0, -200]);
22 const translateSecond = useTransform(scrollYProgress, [0, 1], [0, 200]);
23 const translateThird = useTransform(scrollYProgress, [0, 1], [0, -200]);
24
25 const third = Math.ceil(images.length / 3);
26
27 const firstPart = images.slice(0, third);
28 const secondPart = images.slice(third, 2 * third);
29 const thirdPart = images.slice(2 * third);
30
31 return (
32 <div
33 className={cn("h-[40rem] items-start overflow-y-auto w-full", className)}
34 ref={gridRef}
35 >
36 <div
37 className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 items-start max-w-5xl mx-auto gap-10 py-40 px-10"
38 ref={gridRef}
39 >
40 <div className="grid gap-10">
41 {firstPart.map((el, idx) => (
42 <motion.div
43 style={{ y: translateFirst }} // Apply the translateY motion value here
44 key={"grid-1" + idx}
45 >
46 <img
47 src={el}
48 className="h-80 w-full object-cover object-left-top rounded-lg gap-10 !m-0 !p-0"
49 height="400"
50 width="400"
51 alt="thumbnail"
52 />
53 </motion.div>
54 ))}
55 </div>
56 <div className="grid gap-10">
57 {secondPart.map((el, idx) => (
58 <motion.div style={{ y: translateSecond }} key={"grid-2" + idx}>
59 <img
60 src={el}
61 className="h-80 w-full object-cover object-left-top rounded-lg gap-10 !m-0 !p-0"
62 height="400"
63 width="400"
64 alt="thumbnail"
65 />
66 </motion.div>
67 ))}
68 </div>
69 <div className="grid gap-10">
70 {thirdPart.map((el, idx) => (
71 <motion.div style={{ y: translateThird }} key={"grid-3" + idx}>
72 <img
73 src={el}
74 className="h-80 w-full object-cover object-left-top rounded-lg gap-10 !m-0 !p-0"
75 height="400"
76 width="400"
77 alt="thumbnail"
78 />
79 </motion.div>
80 ))}
81 </div>
82 </div>
83 </div>
84 );
85};
86