

is going to have the basic styling for our carousel.That’s what data-carousel and the others are for. That achieves the same result.īut my preference is to use data- attributes. , which are exclusively for JavaScript use. This means, that I do not use classes to select elements with JavaScript.Īn exception to this is if you use classes with a prefix such as js. I want them to feel safe when working with the CSS. I don’t want them to be paranoid that if they change the CSS they may break the JavaScript, or the automated tests, or the asynchronous content insertions, or anything else. They may also change some CSS modifier classes or something. When someone tries to change the styling of the carousel in the future, they may replace the class name for a more descriptive one. For example, classes are standard to use for CSS. I personally prefer using data- attributes for JavaScript because I want to separate concerns. The content inside each slide is irrelevant to us, it can be anything.Īs for the data- attributes, those are what we’ll use as selectors in JavaScript. Inside that, we have a div for each slide. we have two buttons, one for "previous slide" and one for "next slide".we have an outer div for the entire carousel.In the head, we are linking font awesome and also our custom styles CSS file. The rest is standard stuff.Īs for the actual carousel, here is some HTML we can use. We won’t focus very much on the HTML head or anything other than the carousel. That’s the basic HTML, CSS and JavaScript. We’ll start with the basic functionality. It goes into much more detail than this post. If you prefer a video version, here it is. It won’t be an in-place carousel where a slide fades out while another one fades-in. Slides will move in from left to right, or right to left, with a transition. The carousel will be a "moving carousel". We will use good code practices, keep accessibility in mind and also consider how we can test the carousel.
JAVASCRIPT IMAGE CAROUSEL HOW TO
In this post we’ll look at how to make a simple carousel with HTML, CSS and JavaScript.
