Skip to content

Latest commit

 

History

History
333 lines (285 loc) · 12.2 KB

javascript-less-carousel.md

File metadata and controls

333 lines (285 loc) · 12.2 KB
layout lesson
title JavaScript-less carousel
desc Learn to implement a functional and reusable carousel without JavaScript.
markbot_submit true
hide_show_for_marks true
extra_tutorials
title url
Advanced CSS interactions
advanced-css-interactions
title url
Forms cheat sheet
forms-cheat-sheet
goal
before notes
We’re going to create a fairly basic carousel using only CSS.
label text
Type it, type it real good
Remember the purpose of this lesson is to type the code out yourself—build up that muscle memory in your fingers!
important
title text
Simple but not accessible
The example that we’re completing here is simple to set up and requires no JavaScript. But it is not the most accessible, feature-rich, or even reusable piece of code. To make a truly good & accessible carousel we should add JavaScript and a bunch more ARIA attributes & roles to fully realize the functionality.
fork
url
steps
title before folders after notes
Set up project
After forking & cloning the repository we have a few starter files, but not everything we need…
label type
javascript-less-carousel
folder
label indent
index.html
1
label type indent
css
folder
1
label indent
main.css
2
label indent
modules.css
2
label indent
type.css
2
label type fade indent
images
folder
true
1
1. Make an `index.html` & add the boilerplate code. 2. Make a `modules.css` in your `css` folder—[get a new version from Modulifier](https://modulifier.web-dev.tools/). **Make sure to press “Select All”.** 3. Make a `type.css` in your `css` folder—[get a new version from Typografier](https://typografier.web-dev.tools/). 4. Make a `main.css` in your `css` folder—it can remain empty.
label text
Naming conventions
Don’t forget to follow the [naming conventions](/topics/naming-paths-cheat-sheet/#naming-conventions).
label text
HTML snippets
Create the boilerplate with `html5`, `viewport`, `css`
title before code_lang code_file code lines after
Create the HTML images
Let’s start the HTML by adding a bunch of wrapper `<div>` tags and the HTML necessary to display the four images. Here’s the names of the images for quick copying-and-pasting: ``` arsinoitherium elasmotherium glyptodon megatherium ``` *All the different wrapper tags are needed for grouping the controls and making sure the correct `relative` containers are used.*
html
index.html
⋮ </head> <body> <section class="max-length"> <div class="carousel"> <div class="carousel-inner relative"> <div class="carousel-items"> <div class="carousel-item embed embed-golden" id="arsinoitherium"> <img class="embed-item" src="images/arsinoitherium.jpg" alt=""> </div> <div class="carousel-item embed embed-golden" id="elasmotherium"> <img class="embed-item" src="images/elasmotherium.jpg" alt=""> </div> <div class="carousel-item embed embed-golden" id="glyptodon"> <img class="embed-item" src="images/glyptodon.jpg" alt=""> </div> <div class="carousel-item embed embed-golden" id="megatherium"> <img class="embed-item" src="images/megatherium.jpg" alt=""> </div> </div> </div> </div> </section> ⋮
num fade
2-3
true
Have a quick refresh in the browser and you should see this (though the images will be much bigger): ![](image-html.jpg)
title before code_lang code_file code lines
Add the controls
Next up we’re going to add all the controls. We’re going to use radio buttons in a hacky way: instead of representing collected information they’ll represent the state of our application, aka which slide is currently visible. *This is definitely not the most accessible approach to using radio buttons, but it’s a fairly common pattern and is good for getting a quick demo going.*
html
index.html
⋮ </head> <body> <section class="max-length"> <div class="carousel"> <input class="carousel-input" type="radio" name="mammals" id="arsinoitherium-control"> <input class="carousel-input" type="radio" name="mammals" id="elasmotherium-control"> <input class="carousel-input" type="radio" name="mammals" id="glyptodon-control"> <input class="carousel-input" type="radio" name="mammals" id="megatherium-control"> <div class="carousel-inner relative"> <div class="carousel-items"> <div class="carousel-item embed embed-golden" id="arsinoitherium"> <img class="embed-item" src="images/arsinoitherium.jpg" alt=""> </div> <div class="carousel-item embed embed-golden" id="elasmotherium"> <img class="embed-item" src="images/elasmotherium.jpg" alt=""> </div> <div class="carousel-item embed embed-golden" id="glyptodon"> <img class="embed-item" src="images/glyptodon.jpg" alt=""> </div> <div class="carousel-item embed embed-golden" id="megatherium"> <img class="embed-item" src="images/megatherium.jpg" alt=""> </div> </div> <ul class="carousel-controls"> <li> <label class="carousel-btn" for="arsinoitherium-control">Arsinoitherium</label> </li> <li> <label class="carousel-btn" for="elasmotherium-control">Elasmotherium</label> </li> <li> <label class="carousel-btn" for="glyptodon-control">Glyptodon</label> </li> <li> <label class="carousel-btn" for="megatherium-control">Megatherium</label> </li> </ul> </div> </div> </section> ⋮
num fade
2-7
true
num text
8-11
These are the radio buttons here. They’ll actually be hidden but whichever one is currently checked will denote which slide is currently visible.
num fade
13-30
true
num text
32-45
This list and `<label>` tags will represent the little buttons at the bottom of the carousel for us to hide/show.
num fade
46-50
true
title before code_lang code_file code after
Style the images
To get the images styled we need only a little bit of CSS!
css
css/main.css
.carousel-items > div { display: none; }
If you now refresh in your browser you’ll see nothing… just the names of the four mammals from the `<label>` tags.
title before multi_code after
Make it functional
The next thing I think we should do is add the CSS necessary to make the carousel functional. This requires some complex CSS selectors. We need to select each radio button that’s checked, then go into the `<div>` beside and select the correct slide.
code_lang code_file code
css
css/main.css
⋮ #arsinoitherium-control:checked ~ .carousel-inner #arsinoitherium, #elasmotherium-control:checked ~ .carousel-inner #elasmotherium, #glyptodon-control:checked ~ .carousel-inner #glyptodon, #megatherium-control:checked ~ .carousel-inner #megatherium { display: block; }
code_before code_lang code_file code lines
If you ever want to add another slide to your carousel you have to come into the CSS and add the appropriate new selectors. *Another benefit of using JavaScript is that the JS code would help maintain all this complex CSS.* Now, we’d like to make the first slide visible on the page. That requires a simple tweak to our HTML.
html
index.html
⋮ <section class="max-length"> <div class="carousel"> <input class="carousel-input" type="radio" name="mammals" id="arsinoitherium-control" checked> <input class="carousel-input" type="radio" name="mammals" id="elasmotherium-control"> <input class="carousel-input" type="radio" name="mammals" id="glyptodon-control"> <input class="carousel-input" type="radio" name="mammals" id="megatherium-control"> <div class="carousel-inner relative"> <div class="carousel-items"> ⋮
num fade
2-4
true
num text
5
By adding the `checked` attribute to this radio button the associated image should now be visible because of those complex CSS selectors we wrote.
num fade
6-11
true
Refresh this in the browser and we should see a single slide: ![](single-slide.jpg) **But not only that—it should fully work!** Click on the different slide names at the bottom and see that the carousel changes.
title before multi_code after
Style the controls
The last thing we have to do is make the controls look nice. That’ll require a little HTML & CSS.
code_lang code_file code lines
html
index.html
⋮ <ul class="carousel-controls list-group-inline pin-cb w-1 text-center push-0 island-1-2"> <li> <label class="carousel-btn icon i-18 ir" for="arsinoitherium-control">Arsinoitherium</label> </li> <li> <label class="carousel-btn icon i-18 ir" for="elasmotherium-control">Elasmotherium</label> </li> <li> <label class="carousel-btn icon i-18 ir" for="glyptodon-control">Glyptodon</label> </li> <li> <label class="carousel-btn icon i-18 ir" for="megatherium-control">Megatherium</label> </li> </ul> ⋮
num fade
3,5-6,8-9,11-12,14-15
true
num text
2
Add the following new classes: - `.list-group-inline` - `.pin-cb` - `.w-1` - `.text-center` - `.push-0` - `.island-1-2`
num text
4
Add the following new classes: - `.icon` - `.i-18` - `.ir`
code_before code_lang code_file code lines
Now for a little bit of CSS to make them look like circles. We’re also going to add some CSS that will highlight the current button. Using complex CSS selectors, like we used for showing the right slide, we’ll color the correct button.
css
css/main.css
⋮ .carousel-btn { border: 4px solid #fff; border-radius: 50%; } #arsinoitherium-control:checked ~ .carousel-inner [for="arsinoitherium-control"], #elasmotherium-control:checked ~ .carousel-inner [for="elasmotherium-control"], #glyptodon-control:checked ~ .carousel-inner [for="glyptodon-control"], #megatherium-control:checked ~ .carousel-inner [for="megatherium-control"] { background-color: #fff; }
num text
2-5
This CSS will make the buttons be circles that aren’t filled.
num text
7-12
This CSS will make the currently visible slide shown in it’s associated button by making the background-color white.
And that’s it! A fully functional carousel using only CSS.