Thursday, June 2, 2022

How To Build a Simple Carousel With Vanilla JavaScript (14 Lines of Code!)

How To Build a Simple Carousel With Vanilla JavaScript (14 Lines of Code!)

The humble carousel (or “slider”) is a common feature on websites but it’s not always the most straightforward thing to build.

In this tutorial, we’ll build a very simple carousel using vanilla JavaScript. So simple, in fact, that it requires less than 15 lines of JavaScript code.

This sort of implementation is perfect for a basic website and it’s also a highly performant way of displaying content in a presentable manner. Once you’ve built these slides, you can add whatever content you want to them; text, images, video, you name it.

Here’s a look at the finished carousel we’ll be building–4 simple colored slides:

Let’s get started!

1. Place the Content With HTML

Let’s define the structure of our slider using HTML. We’ll be placing some slides in a container and some buttons to control the slider so the layout will look like this:

We define our slides-container and slides with the ul and li tag respectively for accessibility reasons.

2. Style the Carousel With CSS

Now we’ll style our slider. To keep this demo as simple as possible, I’ll be styling the slides with background colors only but, as mentioned previously, any element can be placed in the carousel (images, text, videos etc.).

The first thing we’ll do is style our slider-wrapper, slides-container and slide classes.

The slides container will have overflow:scroll and display:flex properties to display all the slides on a single row while its parent container, the slider wrapper, will have an overflow-hidden property. This allows us to scroll through the slides in the container while still staying in the width of the page.

One benefit of using the overflow:scroll property is that it allows the user to manually scroll through the slides so it’s “draggable”.

We’ll also need to override the default styling for the ul tag. And then we’ll set the slides to the full width and height of the slides container. 

We’ve set the slides-container height as calc(100vh-2rem) to offset the 1rem margin on the top and bottom from the slider-wrapper.

Allow for Smooth Scrolling

Another important property to note is the scroll-behaviour on the slides container class. This is the property that allows the container to scroll smoothly to the next slide instead of it moving instantly. 

This is what our carousel would look like without scroll-behaviour: smooth

Scrollbar, or No Scrollbar?

Optionally, we can choose to hide the scroll bar on our slidesContainer. The scrollbar is present in the demo but we could remove it by adding:

Style the Carousel Buttons

Finally, we’ll style our carousel buttons. We’ll set an opacity property on the buttons so they’re slightly transparent and solid on hover or focus:

3. Add Slider Functionality With JavaScript

Time to put the fun in functional! There are two parts of logic involved in this slider:

  1. Displaying the next slide when the forward arrow is clicked
  2. Displaying the previous slide when the backward arrow is clicked

First, get all the elements we need for the slider:

Now that’s done, we’ll handle moving the slider. We can decide how the slider container moves by using the scrollLeft property.

“TheElement.scrollLeft property gets or sets the number of pixels that an element’s content is scrolled from its left edge.”- MDN

Since we want to display the next slide when the forward arrow is clicked, that means we’ll need to scroll the slidesContainer to the left by the width of one slide. 

Breaking down what’s happening in this code:

  • We add the click event listener to the next button
  • When the button is clicked, we get the width value of one slide
  • We increase the scrollLeft property of the slidesContainer by the slideWidth.

We can apply this same logic to the backward arrow button but with one small tweak:

With this method, instead of adding to the scrollLeft property, we subtract from it by the width of a slide. This allows us move backwards when the back arrow is pressed.

All the Code

Putting the entire JavaScript code together gives us, as promised, a functional slider with less than 15 lines of JavaScript.

Conclusion

And that’s about it for the carousel.

Of course, it’s possible to extend this implementation to include several features like pagination or snap-to-slide but for the purpose of this tutorial, we’ll stop here. If you’re looking for slightly more complicated implementations, check out this article:


No comments:

Post a Comment