DEV Community

Cover image for Found Simplicity & Elegance – Building a Simple Carousel with JavaScript
Tom J.
Tom J.

Posted on

Found Simplicity & Elegance – Building a Simple Carousel with JavaScript

Hey everyone!

I recently worked on a simple carousel for a client project. I'm absolutely enjoying this project—I didn't need to work on it myself, but I chose to dive in anyway because it's just so thrilling. I finally managed to find the best way to manage a project for its speedy and quality growth.

Alongside that, I also finally discovered the value in simplicity that accomplishes the goal perfectly without projecting a dev's (aka past me) need to prove themselves.

I've uploaded the code to GitHub Gist, and in this article, I'll walk you through how it works. You can also watch the uploaded live stream on YouTube.

The Goal

Create a responsive, smooth-scrolling carousel that:

  • Allows horizontal scrolling through items.
  • Has left and right arrow controls.
  • Hides the arrows appropriately when scrolling reaches the start or end.
  • Is SIMPLE, LIGHTWEIGHT, and EASY to integrate.

Embracing Simplicity

In the world of development in general, it's easy to get caught up in complex solutions, over-engineering features to showcase our skills. But usually, the simplest solutions are the way to go. This carousel is a testament to that philosophy.

I could download a library or use margins, or sometimes I even saw absolute positioning—but I didn't; all those solutions are too heavy without providing any meaningful gain.

I focused on using vanilla JavaScript and CSS to achieve the desired functionality. The result is a lightweight carousel that's easy to understand, maintain, and integrate into any project (yeah, I actually put it in place—or rather, all 6 places with specific design from Figma—in 30 minutes).

How It Works

The carousel's HTML is dead simple. I'll just mention one thing—classes are for UI devs, data- attributes are for JS devs—this separation is awesome for team development.

CSS takes care of all the animations for performance and ease of use and later changes.

JS is the real worker here; it performs super simple logic to achieve a fantastic (not just a “good enough”) job in making the simple move and calculation. It’s simple code as it is, and it even has performance optimization included.

Why This Approach Works

  • Performance: Less code and no external dependencies mean faster load times and better performance.
  • Maintainability: Simplicity makes it easier to understand and modify the code in the future.
  • Customization: It's easy to tweak and style according to different projects' needs.

Watch the Live Stream

If you're interested in seeing how I built this carousel step by step, check out the live stream on YouTube:

Watch the Live Stream

Final Thoughts – Let's Keep Learning

First, I used some 250kb carousel jQuery library. It had so many different transition effects and implementations, and it was awesome!... It wasn't... Every time a client asked for something, this thing just couldn't do it.

Next, I created our own using the jQuery framework. It had 500 lines plus CSS, and it was universally used for any project and design. It was awesome!... It wasn't... Any kind of bending for any project took literally 2 hours, and any project needed to be bent to it.

Then, I made a carousel for each project separately based on the same logic—margins and some effects on top and changing behavior on several responsive steps. It also wasn't awesome.

Finally—this thing now takes me 10 minutes and is perfectly suited for a given project and perfectly performant.

Of course, this requires collaboration with the graphics designer—if they want it simple, I can provide simple. But also, every other solution was unnecessarily bloated and large and just horrible, and even in the craziest carousels anyone can think of, the solution and code would be quite simple.

Do you have your own experiences seeing yourself change over the years?

Top comments (0)