Docs
Horizontal Scroll

Horizontal Scroll

Scroll content horizontally

https://images.unsplash.com/photo-1501854140801-50d01698950b?q=80&w=1950
https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05?q=80&w=1948
https://images.unsplash.com/photo-1475924156734-496f6cac6ec1?q=80&w=2070
https://images.unsplash.com/photo-1505765050516-f72dcac9c60e?q=80&w=2070
https://images.unsplash.com/photo-1431794062232-2a99a5431c6c?q=80&w=2070
https://images.unsplash.com/photo-1446329813274-7c9036bd9a1f?q=80&w=2070
https://images.unsplash.com/photo-1572099606223-6e29045d7de3?q=80&w=2070

Installation

Copy and paste the following code into your project.

components/edil-ozi/horizontal-scroll.tsx
import { FC, useRef } from "react";
import { motion, useTransform, useScroll } from "framer-motion";
import Image from "next/image";
 
//optional hook for smooth scrolling
import useLenis from "@/hooks/useLenis";
 
interface Props {
  images: string[];
}
 
const HorizontalScrollCarousel: FC<Props> = ({ images }) => {
  const targetRef = useRef(null);
  const { scrollYProgress } = useScroll({
    target: targetRef,
  });
 
  const x = useTransform(scrollYProgress, [0, 1], ["1%", "-90%"]);
 
  useLenis();
 
  return (
    <section
      ref={targetRef}
      className="relative h-[300vh] w-full"
    >
      <div className="sticky top-0 flex h-screen items-center overflow-hidden">
        <motion.div
          style={{ x }}
          className="flex gap-4"
        >
          {images.map((src) => (
            <Card
              src={src}
              key={src}
            />
          ))}
        </motion.div>
      </div>
    </section>
  );
};
 
const Card: FC<{ src: string }> = ({ src }) => {
  return (
    <div
      key={src}
      className="group relative h-[450px] w-[450px] overflow-hidden rounded-lg border border-gray-400"
    >
      <Image
        src={src}
        fill
        objectFit="cover"
        alt={src}
      />
    </div>
  );
};
 
export default HorizontalScrollCarousel;

Props

Prop nameTypeDefaultDescription
imagesstring[]-Array of urls