Skip to content

Instantly share code, notes, and snippets.

@iampato
Created September 18, 2023 21:44
Show Gist options
  • Save iampato/203c0bcd699c8f909b23329bea368bbe to your computer and use it in GitHub Desktop.
Save iampato/203c0bcd699c8f909b23329bea368bbe to your computer and use it in GitHub Desktop.
Unintentionally I have created my own carousel that can extended to meet your needs
const images = [
"https:/...",
"https:/...",
"https:/...",
];
const Carousel = () => {
const [index,setIndex] = useState(0);
const goToNextSlide = () => {
// check for last slide and reset to 0
setIndex((prevIndex) =>
prevIndex === images.length - 1 ? 0 : prevIndex + 1
);
};
const goToPrevSlide = () => {
// check for first slide and set it to last slide index
setIndex((prevIndex) =>
prevIndex === 0 ? images.length - 1 : prevIndex - 1
);
};
return (
<div className="overflow-x-hidden rounded-lg">
<div
className="flex w-full transition-transform duration-500 ease-in-out "
style={{
transform: `translateX(-${index * 100}%)`,
}}
>
{images.map((image, index) => (
<div
key={index}
className="w-full flex flex-shrink-0"
>
<img
src={image}
alt={`Image ${index + 1}`}
className="object-cover w-full h-full"
/>
</div>
))}
</div>
</div>
);
}
@iampato
Copy link
Author

iampato commented Sep 18, 2023

You can add buttons to go back and forth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment