Home / Website Sections / Touch Image Slider

Touch Image Slider in JavaScript, Free with Live Demo

Free image slider in plain JavaScript. Swipe on touch screens, drag with a mouse, autoplay that pauses on hover, dots, arrows and keyboard control. No library needed.

Open live demoDownload HTML fileView code on GitHub
Touch Image Slider JavaScript project: a photo slider with swipe, autoplay, dots and keyboard control

Runs on: Pointer events and CSS transforms. Works in all modern browsers.

What is the Touch Image Slider?

A photo slider with big images and captions. Swipe it on a phone, drag it with a mouse, or use arrows, dots and the keyboard.

Autoplay moves to the next photo on a timer, pauses when you hover or tab into it, and switches off for people who prefer less motion.

Good for

  • Hero banners on home pages
  • Hotel, travel and property photos
  • Product galleries in shops
  • Portfolio projects

What this project does

How it works

  1. Move the whole trackAll slides sit in one row. Showing slide n just means moving the row by n times 100 percent.
  2. Follow the fingerPointer events work for touch, pen and mouse. While dragging, the track follows the finger with no transition.
  3. Decide on releaseIf the drag was more than 15 percent of the width, go to the next slide. Otherwise snap back.

The key JavaScript

This is the heart of the project. The full file has the rest, including the screen layout and error handling.

slider.addEventListener("pointerdown", (e) => { startX = e.clientX; track.classList.add("drag"); });
slider.addEventListener("pointermove", (e) => {
  if (startX == null) return;
  dx = e.clientX - startX;
  track.style.transform = `translateX(calc(${-index * 100}% + ${dx}px))`;
});
slider.addEventListener("pointerup", () => {
  track.classList.remove("drag");
  go(Math.abs(dx) > width * 0.15 ? index + (dx < 0 ? 1 : -1) : index);
  startX = null;
});

How to use it

  1. Click Download HTML file above.
  2. Open the file in a code editor, like VS Code.
  3. Run it from a local server with npx serve . so the camera, microphone and AI features are allowed.
  4. Change the text and colors, then upload it to GitHub Pages, Netlify or your own site. It is one file with no build step.

Questions people ask

Do I need a slider library?

Not for most sites. This whole slider is about 40 lines of JavaScript.

Is autoplay bad for accessibility?

It can be. This slider pauses on hover and focus and turns autoplay off when the user asks for reduced motion.

How do I add more slides?

Add another item to the list at the top of the script. Dots and counts update by themselves.

More Website Sections projects