I'm using react-responsive-carousel in my React Project. I want to control the number of items shown on screen, (3 items in desktop view and 1 in mobile view). At first, I tried to add a prop named "slidesToShow" as suggested by Chatgpt (although I couldn't find this prop in official documentation, anyways, tried to give it a try), but it didn't work. Then, I did this through css by adding the width property in "slide" class, that actually worked, but it shows weird behavior while scrolling through the images. It starts from third image and shows some frames blank. Looks like we need to add some logic like itemsPerPage, itemOffset, etc but not getting how to add here, just like react-paginate gives us the freedom to write our own logics for scrolling. Here is how my .js code looks like:
import "react-responsive-carousel/lib/styles/carousel.min.css"; // requires a loader
import { Carousel } from "react-responsive-carousel";
import "./CustomIndicator.css";
export default function App() {
return (
<Carousel infiniteLoop slidesToShow={3}>
<div>
<p>01</p>
<img src={"1.jpeg"} className="w-full h-full" />
</div>
<div>
<p>02</p>
<img src={"2.jpeg"} className="w-full h-full" />
</div>
<div>
<p>03</p>
<img src={"3.jpeg"} className="w-full h-full" />
</div>
<div>
<p>04</p>
<img src={"1.jpeg"} className="w-full h-full" />
</div>
<div>
<p>05</p>
<img src={"2.jpeg"} className="w-full h-full" />
</div>
<div>
<p>06</p>
<img src={"3.jpeg"} className="w-full h-full" />
</div>
</Carousel>
);
}
And this is the styles file:
.carousel .control-dots .dot {
border-radius: 0% !important;
width: 20px !important;
height: 2px !important;
}
.carousel .control-dots .dot:active {
border-radius: 0% !important;
width: 20px !important;
height: 5px !important;
}
@media only screen and (max-width: 600px) {
.slide {
min-width: 50% !important;
}
}
/* For all other screen sizes, display 3 items */
.slide {
min-width: 50% !important;
}
And following is the link of codesandbox sample for better demonstration. https://codesandbox.io/s/how-to-control-carousel-items-gl6pov?file=/src/CustomIndicator.css