How to enable auto image slide in carousel ? #1488
-
How to enable auto slide functionality in carousel component? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
You would need to write some JS to do that. |
Beta Was this translation helpful? Give feedback.
-
I have created an automatic slide carousel with a feature that prevents scrolling to the specified ID. You can use it as a reference. import classNames from "classnames";
import { useEffect, useState } from "react";
import { twMerge } from "tailwind-merge";
import type { IPresetIProps, Iimg } from "~/interfaces/common";
type IProps = {
imgs: Iimg[];
carouselId: string;
classNameCarousel?: string;
classNameForImage?: string;
isAutoPlay?: boolean;
autoPlayMilliseconds?: number;
} & IPresetIProps;
const goToOtherImage = (href: string, carouselId: string) => {
const carousel = document.getElementById(carouselId);
if (carousel) {
const target = document.querySelector<HTMLDivElement>(href)!;
const left = target.offsetLeft;
carousel.scrollTo({ left: left });
}
};
export default function DaisyUICarousel({
imgs,
carouselId,
classNameCarousel,
classNameForImage,
isAutoPlay = true,
autoPlayMilliseconds = 5000,
}: IProps) {
const [activeIndex, setActiveIndex] = useState<number>(0);
const handleClickBtn = (
event: React.MouseEvent<HTMLAnchorElement, MouseEvent>,
i: number
) => {
event.preventDefault();
const btn = event.currentTarget;
const href = btn.getAttribute("href")!;
goToOtherImage(href, carouselId);
setActiveIndex(i);
};
useEffect(() => {
if (isAutoPlay) {
const intervalId = setInterval(() => {
const newActiveIndex =
activeIndex + 1 === imgs.length ? 0 : activeIndex + 1;
goToOtherImage(`#DaisyUICarousel_img_${newActiveIndex}`, carouselId);
setActiveIndex(newActiveIndex);
}, autoPlayMilliseconds);
return () => clearInterval(intervalId);
}
}, [activeIndex, autoPlayMilliseconds, carouselId, imgs.length, isAutoPlay]);
return (
<div className="relative">
<div
id={carouselId}
className={classNames("carousel", classNameCarousel)}
>
{imgs.map((img, i) => {
return (
<div
key={`DaisyUICarousel_img_${i}`}
id={`DaisyUICarousel_img_${i}`}
className={twMerge(
"carousel-item w-full bg-center bg-cover bg-no-repeat",
classNameForImage
)}
style={{
backgroundImage: `url(${img.src})`,
}}
></div>
);
})}
</div>
<div className="flex justify-center w-full py-2 gap-2 absolute bottom-3">
{imgs.map((img, i) => {
return (
<a
onClick={(e) => handleClickBtn(e, i)}
key={`DaisyUICarousel_img_point_${i}`}
href={`#DaisyUICarousel_img_${i}`}
className={classNames(
activeIndex !== i && " opacity-30",
"btn btn-xs btn-circle"
)}
></a>
);
})}
</div>
</div>
);
} |
Beta Was this translation helpful? Give feedback.
-
To enable automatic image sliding in a carousel, incorporate JavaScript to create a timed interval for transitioning between images. Use the setInterval function to trigger the slide change at regular intervals. Within the function, update the active image index and manage the transition seamlessly. Ensure synchronization with any user-initiated slides to maintain a smooth and cohesive experience. This dynamic approach enhances the carousel's usability, offering both manual control and an automatic slideshow feature. Adjust the interval duration to suit the desired pace of image transitions for an engaging and user-friendly carousel experience. Check out our blog : Tailwind Carousel: Create Responsive Carousels Easily - Purecode |
Beta Was this translation helpful? Give feedback.
You would need to write some JS to do that.
daisyUI's carousel is a simple CSS-only solution. It really can't have as much functionality as a JS carousel can deliver.
I would suggest to use a JS carousel plugin if you need more features. It's perfectly fine to use daisyUI components on some sections and use JS plugins when needed.