Skip to content

Commit

Permalink
fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
srilekha279 committed Aug 10, 2024
1 parent bc56f0a commit c8f0489
Showing 1 changed file with 45 additions and 17 deletions.
62 changes: 45 additions & 17 deletions src/Components/Testimonials/Testimonials.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,60 @@ function Testimonials() {
const cardTheme = useSelector((state) => state.theme.value) ? "cardTheme-dark" : "light";
const carouselRef = useRef(null);

const scrollCarousel = (direction) => {
useEffect(() => {
const carousel = carouselRef.current;
const cardWidth = carousel.querySelector(".card").offsetWidth;
const items = Array.from(carousel.children);

const scrollAmount = direction === "left" ? -cardWidth : cardWidth;
// Clone items for infinite scroll
items.forEach(item => {
const clone = item.cloneNode(true);
carousel.appendChild(clone);
});

if (
direction === "right" &&
carousel.scrollLeft + carousel.offsetWidth >= carousel.scrollWidth - cardWidth
) {
carousel.scrollLeft = 0;
} else if (direction === "left" && carousel.scrollLeft <= 0) {
carousel.scrollLeft = carousel.scrollWidth - carousel.offsetWidth;
} else {
carousel.scrollLeft += scrollAmount;
}
};
// Adjust scroll position
const adjustScroll = () => {
carousel.scrollLeft = carousel.scrollWidth / 2;
};

adjustScroll();

useEffect(() => {
const autoSlideInterval = setInterval(() => {
scrollCarousel("right");
const cardWidth = carousel.querySelector(".card").offsetWidth;
const maxScrollLeft = carousel.scrollWidth - carousel.clientWidth;

if (carousel.scrollLeft + carousel.clientWidth >= maxScrollLeft) {
carousel.scrollLeft = carousel.scrollWidth / 2 - cardWidth; // Smooth transition
} else {
carousel.scrollLeft += cardWidth;
}
}, 3000);

return () => clearInterval(autoSlideInterval);
const handleScroll = () => {
const cardWidth = carousel.querySelector(".card").offsetWidth;
const maxScrollLeft = carousel.scrollWidth / 2 - cardWidth;

if (carousel.scrollLeft <= 0) {
carousel.scrollLeft = carousel.scrollWidth / 2;
} else if (carousel.scrollLeft >= maxScrollLeft) {
carousel.scrollLeft = 0;
}
};

carousel.addEventListener('scroll', handleScroll);
return () => {
clearInterval(autoSlideInterval);
carousel.removeEventListener('scroll', handleScroll);
};
}, []);

const scrollCarousel = (direction) => {
const carousel = carouselRef.current;
const cardWidth = carousel.querySelector(".card").offsetWidth;
const scrollAmount = direction === "left" ? -cardWidth : cardWidth;

carousel.scrollLeft += scrollAmount;
};

return (
<>
<div className={`wrapper ${theme}`} id="testimonials">
Expand Down

0 comments on commit c8f0489

Please sign in to comment.