Created
September 18, 2023 21:44
-
-
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
This file contains hidden or 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
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> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can add buttons to go back and forth