JavaScript Notes
Learn to build an accessible image slider in JavaScript with keyboard navigation, auto-advance, fade transitions, caption overlay, and fullscreen mode.
What You'll Learn
- How to manage a current index and switch images
- How to implement fade transitions via CSS class toggling
- How to add captions that update with each image
- How to support keyboard navigation (ArrowLeft / ArrowRight)
- How to use the Fullscreen API for an immersive view
Project Overview
An image slider is simpler than a carousel — it shows one image at a time with a smooth crossfade and a caption. It's great for portfolios, product photography, or any content gallery.
Component Structure
How It Works — Flow Diagram
goTo(index)
→ fade out current image (opacity: 0)
→ after 300ms: swap src, update caption, counter
→ fade in new image (opacity: 1)
Auto-play
→ setInterval(goNext, 4000)
→ pauses on mouseover / focus
Keyboard
→ ArrowLeft → goPrev()
→ ArrowRight → goNext()
→ "f" → toggleFullscreen()Step-by-Step Build
Step 1 — Image Data
Step 2 — HTML Structure
<div class="slider" id="slider">
<figure class="slider-frame">
<img id="sliderImg" src="" alt="" class="slider-img active">
<figcaption id="caption" class="caption"></figcaption>
</figure>
<button id="prevBtn" class="nav-btn nav-prev" aria-label="Previous">‹</button>
<button id="nextBtn" class="nav-btn nav-next" aria-label="Next">›</button>
<div id="counter" class="counter"></div>
<div class="controls">
<button id="fsBtn" class="ctrl-btn">⛶ Fullscreen</button>
<button id="pauseBtn" class="ctrl-btn">⏸ Pause</button>
</div>
</div>Step 3 — GoTo with Crossfade
Step 4 — CSS for Fade
.slider-img {
opacity: 0;
transition: opacity 0.3s ease;
}
.slider-img.active {
opacity: 1;
}Step 5 — Navigation Buttons
Step 6 — Auto-Play
Step 7 — Keyboard Navigation
Step 8 — Fullscreen API
Step 9 — Initialize
goTo(0);
startTimer();Console Output
goTo(0) → img.src = "img/mountain.jpg" → caption = "Sunrise over the Rockies" → counter = "1 / 5" --- 4 seconds pass --- goTo(1) → img fade out → src = "img/ocean.jpg" → fade in → caption = "Pacific Ocean at dusk" → counter = "2 / 5" --- User presses ArrowLeft --- goTo(0) → wrapped to: counter = "1 / 5" --- User presses "f" --- slider.requestFullscreen() → browser enters fullscreen
Edge Cases to Handle
| Situation | Expected Behavior |
|---|---|
| Wrap around (last → next) | Index wraps to 0 |
| Wrap around (first → prev) | Index wraps to last |
| Fullscreen not supported | catch() silently handles |
| Rapid clicks | 300ms fade completes before next swap |
| Single image | Hide prev/next buttons |
Challenge Yourself
- Thumbnail strip — show small clickable thumbnails below the slider
- Progress bar — animate a thin bar that fills during the auto-advance delay
- Lazy loading — only load images as they become active
- Zoom on hover — scale the image slightly when the mouse enters
- Share slide — add a copy-URL button that links directly to the current slide
Best Practices
- Use
setTimeoutinsidegoTo()for the crossfade delay, notsetInterval - Always use safe wrap-around:
((index % len) + len) % len - Apply CSS
transitionon the element, not JavaScript animations - Provide
aria-labelon all navigation buttons for accessibility - Pause auto-play when the user is actively interacting
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Build a JavaScript Image Slider - Step by Step Project Tutorial.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, mini, projects, image
Related JavaScript Master Course Topics