Image Carousel in JavaScript
Browse three generated image cards in JavaScript with Previous and Next buttons. Use % to wrap indexes; each change updates the caption and alt text.
Save this as index.html and open it in a browser.
1<!doctype html>2<html lang="en">3<meta charset="utf-8">4<title>JavaScript practice</title>5<body>6<img id="photo" width="260" height="140" alt=""><p id="caption"></p><button id="prev">Previous</button><button id="next">Next</button>7<script>8const slides = [9 { title: 'Library', color: '#d4e8ff' },10 { title: 'Science lab', color: '#d9f5db' },11 { title: 'Sports ground', color: '#ffe6c9' }12];13let current = 0;14function render() {15 const slide = slides[current];16 const svg = '<svg xmlns="http://www.w3.org/2000/svg" width="260" height="140"><rect width="260" height="140" fill="' + slide.color + '"/><text x="20" y="75" font-size="22">' + slide.title + '</text></svg>';17 const photo = document.querySelector('#photo');18 photo.src = 'data:image/svg+xml,' + encodeURIComponent(svg);19 photo.alt = slide.title;20 document.querySelector('#caption').textContent = (current + 1) + ' / ' + slides.length + ': ' + slide.title;21}22document.querySelector('#next').onclick = () => { current = (current + 1) % slides.length; render(); };23document.querySelector('#prev').onclick = () => { current = (current - 1 + slides.length) % slides.length; render(); };24render();25</script>26</body>27</html>
Sample Output
1 / 3: Library
Human numbering starts at one while the array index starts at zero.
3 / 3 back to 1 / 3
The remainder operation wraps the next index to zero after the last slide.
In this one
- Keep the current slide index
- Wrap forward and backward safely
- Update image text and caption
- Use local generated images for practice
How it works
- 1
Create three fixed slide records.
- 2
Generate the current card image and set its alt text.
- 3
Display the human-friendly number current + 1.
- 4
Use remainder arithmetic in Next and Previous handlers.
- 5
Click Next three times to return from Library to Library.
Edge cases
Empty slide list
Remainder by zero produces an unusable index.
Check slides.length before rendering if slides come from external data. This fixed example always has three slides.
Previous at the first slide
A plain current - 1 can become negative.
Add slides.length before % so the result wraps to the last valid index.
What's going on
An image carousel shows one picture at a time and lets the person move forward or backward. This campus example uses three small generated image cards, so there are no missing files or outside image downloads to set up.
current stores the position of the visible slide. The remainder operator, written %, wraps the position after the last slide. The Previous calculation adds the array length before taking the remainder so moving back from position zero reaches the last slide rather than a negative position.
render creates a simple SVG image, which is an image described with text shapes. encodeURIComponent makes that text safe to place in a data URL for these fixed cards. The code also updates alt text and a numbered caption. In a real gallery, replace the generated images with your own image addresses and meaningful descriptions. This example changes only when a button is clicked; it does not auto-advance while someone is reading.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What follows index 2 in this three-slide carousel?
Pick an answer to check it.
Question 2
What should alt describe?
Pick an answer to check it.
Questions people ask
Why add one in the caption?
Array indexes start at zero, but people normally count slides from one.
Are these real campus photos?
No. They are local SVG practice cards. Replace them with your own pictures when building a gallery.
Why avoid automatic sliding here?
Manual controls make the state changes easy to follow and let people read at their own pace.