To create a seamless infinite carousel animation for your image slider, you need to ensure that the transition between the end and the beginning of the slider is smooth without any visible jumps or gaps. Here's a step-by-step guide to help you achieve this:
1. Understand the Current Setup
You have two .image-slider divs inside .galerie-main, each containing the same set of images. This duplication is a common technique to create an infinite scrolling effect. However, the current animation translates the slider by -100vw, which might not align correctly with the duplicated content, causing the non-seamless transition.
2. Adjust the HTML Structure
Ensure that your .image-slider elements are structured correctly for seamless looping. Here's an optimized version of your HTML structure:
<div className="galerie">
<div className="galerie-item galerie-main">
<div className="image-slider">
{/* First set of images */}
{['Rectangle 887.png', 'images (4).png', 'images (5).png', 'images (6).png', 'Rectangle 890.png'].map((src, index) => (
<div className="galerie__image-item" key={`slider1-${index}`}>
<img className='galerie-image' src={`/${src}`} alt={`Gallery ${index + 1}`} loading="lazy" />
</div>
))}
</div>
<div className="image-slider">
{/* Duplicate set of images for seamless looping */}
{['Rectangle 887.png', 'images (4).png', 'images (5).png', 'images (6).png', 'Rectangle 890.png'].map((src, index) => (
<div className="galerie__image-item" key={`slider2-${index}`}>
<img className='galerie-image' src={`/${src}`} alt={`Gallery Duplicate ${index + 1}`} loading="lazy" />
</div>
))}
</div>
</div>
<!-- Other galerie items -->
</div>
3. Update the CSS for Seamless Animation
To achieve a seamless loop, you'll need to adjust the animation to translate both .image-slider elements smoothly. Here's how you can modify your CSS:
.galerie {
display: flex;
flex-direction: column;
align-items: center;
}
.galerie-item.galerie-main {
display: flex;
width: 200%; /* Since we have two image-slider divs */
animation: slide 20s linear infinite;
}
.image-slider {
display: flex;
width: 50%; /* Each slider takes up half the total width */
}
.galerie__image-item {
flex: 0 0 auto;
width: 20%; /* Adjust as needed based on the number of images */
}
.galerie-image {
width: 100%;
height: auto;
display: block;
}
@keyframes slide {
0% {
transform: translateX(0);
}
100% {
transform: translateX(-50%);
}
}
4. Explanation of CSS Adjustments
-
.galerie-item.galerie-main: Set the width to 200% because it contains two .image-slider divs. This allows both sliders to be side by side.
-
.image-slider: Each slider takes up 50% of the .galerie-main container, ensuring that both sliders combined fill the entire width.
-
.galerie__image-item: Set flex: 0 0 auto to prevent the items from stretching. Adjust the width based on the number of images to ensure they fit perfectly within the slider.
-
@keyframes slide:
- Start (
0%): The slider is at its initial position.
- End (
100%): Translates the slider by -50% of its width, which moves the first slider completely out of view, revealing the second slider seamlessly.
-
Animation Duration: Adjust the duration (20s in this example) to control the speed of the sliding effect.
5. Additional Enhancements
-
Responsive Design: Ensure that the carousel is responsive across different screen sizes. You might need to adjust the widths and flex properties based on breakpoints.
-
Pause on Hover: To improve user experience, consider pausing the animation when the user hovers over the carousel.
.galerie-item.galerie-main:hover {
animation-play-state: paused;
}
-
Smooth Transition: Ensure that the images are loaded properly to avoid any flickering during the transition.
6. Final Thoughts
By duplicating the image slider and adjusting the animation to translate only half the width (-50%), you ensure that as one slider moves out of view, the duplicate slides in seamlessly. This technique eliminates the visible jump or gap, creating a smooth infinite carousel effect.
7. Complete Updated Code
Experiences.css
.experiences {
margin-top: 100px;
overflow: hidden;
width: 100%;
}
.experiences__video-slider {
display: flex;
justify-content: center;
align-items: center;
flex-direction: row;
gap: 3rem;
height: 500px;
}
.experiences__video-slider .video-item:nth-child(2) {
transform: scale(1.1);
}
.image-slider {
display: flex;
flex: 0 0 auto;
}
.galerie {
display: flex;
flex-direction: column;
align-items: center;
}
.galerie-item.galerie-main {
display: flex;
width: 200%; /* Two sliders side by side */
animation: slide 20s linear infinite;
}
.galerie-item.galerie-second {
/* Existing styles */
}
.galerie__image-item {
flex: 0 0 20%; /* Adjust based on number of images */
display: flex;
justify-content: center;
align-items: center;
}
.galerie-image {
width: 100%;
height: auto;
display: block;
}
@keyframes slide {
0% {
transform: translateX(0);
}
100% {
transform: translateX(-50%);
}
}
.galerie-item.galerie-main:hover {
animation-play-state: paused;
}
Experiences.jsx
import React from 'react';
import { IoPlayOutline, IoPauseOutline } from "@react-icons/all-files/io5/index";
import './Experiences.css';
const Experiences = () => {
const videoItems = [
{ id: 1, img: '/Rectangle 881.png', title: 'Lorem ipsum', description: 'Lorem ipsum dolor', icon: <IoPlayOutline /> },
{ id: 2, img: '/Rectangle 882.png', title: 'Lorem ipsum', description: 'Lorem ipsum dolor', icon: <IoPauseOutline /> },
{ id: 3, img: '/Rectangle 883.png', title: 'Lorem ipsum', description: 'Lorem ipsum dolor', icon: <IoPlayOutline /> },
];
const galleryImages = [
'Rectangle 887.png',
'images (4).png',
'images (5).png',
'images (6).png',
'Rectangle 890.png',
];
return (
<section className='experiences'>
<div className="title__container">
<div className="welcome__header">
<h1 className="welcome__title">Nos patients témoignent</h1>
</div>
</div>
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center' }}>
<div className='welcome__details'>
Votre expert en ostéopathie, périnéologie, kinésithérapie sportive, performance sportive, neutral trainer, dryneedling
et Tecarthérapie.
</div>
</div>
<div className="experiences__video-slider">
{videoItems.map(item => (
<div className="video-item" key={item.id} style={{ '--i': `url('${item.img}')` }}>
<div className="video__item-details">
<div className="player-ui">
<div className="video__item-title">{item.title}</div>
<div className="video__item-description">{item.description}</div>
</div>
<button className="play-button">{item.icon}</button>
</div>
</div>
))}
</div>
<div className="galerie">
<div className="galerie-item galerie-main">
<div className="image-slider">
{galleryImages.map((src, index) => (
<div className="galerie__image-item" key={`slider1-${index}`}>
<img className='galerie-image' src={`/${src}`} alt={`Gallery ${index + 1}`} loading="lazy" />
</div>
))}
</div>
<div className="image-slider">
{galleryImages.map((src, index) => (
<div className="galerie__image-item" key={`slider2-${index}`}>
<img className='galerie-image' src={`/${src}`} alt={`Gallery Duplicate ${index + 1}`} loading="lazy" />
</div>
))}
</div>
</div>
<div className="galerie-item galerie-second">
{['images (9).png', 'images (1).png', 'images (2).webp', 'images (3).png'].map((src, index) => (
<div className="galerie__image-item" key={`second-${index}`}>
<img className='galerie-image' src={`/${src}`} alt={`Gallery ${index + 6}`} loading="lazy" />
</div>
))}
</div>
</div>
</section>
);
};
export default Experiences;
8. Conclusion
By restructuring the HTML and adjusting the CSS animation to translate only half the width of the .galerie-main container, you create a continuous loop that seamlessly transitions from the end back to the beginning. This approach eliminates the visible jump and ensures a smooth infinite carousel animation.
If you continue to experience issues, consider using established carousel libraries like Swiper.js or Slick Carousel, which offer robust solutions for infinite looping and seamless animations with minimal setup.