Skip to content

Commit ad9e478

Browse files
committed
parallax effect added
1 parent 1bf0d21 commit ad9e478

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

app/page.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Div100vh from "@/components/Div100vh";
66
import StorygramCards from "@/components/StorygramCards";
77
import fetchData from "@/lib/sanity/fetchData";
88
import fetchLogo from "@/lib/sanity/fetchLogo";
9+
import ParallaxImage from "@/components/ParallaxImage";
910

1011
export default async function Home() {
1112
const homeData = await fetchData("home");
@@ -76,9 +77,9 @@ export default async function Home() {
7677
<div className="grid w-full gap-8 md:grid-cols-3 md:gap-y-16">
7778
{featured.map((shot) => (
7879
<div key={shot._key} className="relative bg-darkest pb-6 pt-3">
79-
<RenderImage
80+
<ParallaxImage
8081
image={shot.image}
81-
sizes="(max-width: 768px) 100vw, 30vw"
82+
className="aspect-[4/3] w-full"
8283
/>
8384
<div className="absolute left-0 top-0 size-full opacity-0 duration-1000 hover:opacity-100">
8485
{shot.instaHandle && (

components/ParallaxImage.jsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use client";
2+
import { useRef } from "react";
3+
import { motion, useScroll, useTransform } from "framer-motion";
4+
import RenderImage from "./RenderImage";
5+
6+
export default function ParallaxImage({ image, className }) {
7+
const ref = useRef(null);
8+
const { scrollYProgress } = useScroll({
9+
target: ref,
10+
offset: ["start end", "end start"],
11+
});
12+
13+
const y = useTransform(scrollYProgress, [0, 1], ["-15%", "15%"]);
14+
15+
return (
16+
<div ref={ref} className={`relative overflow-hidden ${className}`}>
17+
<motion.div style={{ y, height: "120%", width: "100%" }} className="relative">
18+
<RenderImage
19+
image={image}
20+
fill
21+
style={{ objectFit: "cover" }}
22+
sizes="(max-width: 768px) 100vw, 50vw"
23+
/>
24+
</motion.div>
25+
</div>
26+
);
27+
}

0 commit comments

Comments
 (0)