Here's an example of an infinity multi-item image carousel with touch using plain vanilla JS and CSS.
No dependencies, no frameworks.
Here's an example of an infinity multi-item image carousel with touch using plain vanilla JS and CSS.
No dependencies, no frameworks.
In a recent project, I implemented an image carousel with a focus on efficiency and responsiveness, and I'd like to share the rationale behind my design and coding decisions. Let's break it down:
HTML Structure
The carousel is structured with a
<div>containing all the carousel images. Notably, I've used "eager" loading for the first and last three images. This is because these images are likely to be in or near the viewport when the carousel is initially loaded or when users navigate to the end. Eager loading ensures these images are loaded as soon as possible, enhancing user experience.CSS Design
I've used CSS custom properties (variables) for easier maintenance and better scalability. The responsive design is achieved using media queries. Below 576px width, the carousel displays one image at a time (100% width), and above that, it shows three images at a time (33.33% width). The
display: nonefor navigation buttons on smaller screens enhances mobile user experience by providing more space.JavaScript Functionality
The JavaScript class
Carouselhandles all carousel functionalities. It initializes with the current index set to the first real image, considering the duplicated (fake) images for the loop effect. Theslidefunction updates the index and uses CSS transitions for smooth movement. Special attention is given to touch navigation, a must-have for modern web interfaces, allowing users to navigate the carousel with swipes.Key Considerations
This implementation aligns with modern web development standards focusing on performance, responsiveness, and user experience.
CodePen