Vanilla JS Image Slider

Slide1
Slide 2
Slide 3
Slide 4
Slide 5

What’s happening on this page?

This slider is built with vanilla JavaScript (no jQuery and no plugins). Instead of calling a library function, we wrote the slider logic ourselves in javascript-slider.js.

1) The HTML (structure)

2) The CSS (appearance + layout)

3) The JavaScript (behavior)

The JavaScript keeps track of one main value: the current slide index (0, 1, 2, 3). When the index changes, we:

  1. Move the track with transform: translateX(...)
  2. Update which dot has the .active class

Customization ideas

Change the “window” size

In javascript-slider.css, adjust the height on .slide img.

/* TRY THIS: change slider height */
.slide img {
  height: 350px; /* try 250px, 400px, 500px */
}

Crop vs. fit the whole image

/* cover = fills frame (crops) | contain = shows whole image (letterbox) */
.slide img {
  object-fit: cover;   /* try contain */
}

Change the animation speed

The speed is controlled by CSS (not JavaScript) because we’re animating the transform.

/* TRY THIS: faster/slower slide animation */
.track {
  transition: transform 0.35s ease; /* try 0.2s or 0.6s */
}

Change the look of arrows and dots

Troubleshooting checklist