Skip to content
Tutorials Center
My learning

Topics

Lesson 53 of 56 ยท Projects

Project: Image Slider, Magnifier and Spotlight

Build three pointer-and-animation projects with jQuery: an auto-playing image slider, a hover image magnifier and a spotlight that follows the mouse.

Three small projects, one idea: respond to position and time. The slider moves through slides on a timer and on button clicks, the magnifier turns the mouse position into a zoomed view, and the spotlight makes a bright circle follow the pointer. Along the way you use .animate(), mousemove and .offset(), and you learn how to keep timers and pointer events from misbehaving.

All three use inline SVG images created in the script, so they work anywhere without extra files. In your own project, replace them with real <img> elements.

Prerequisites

You should know custom animations, dimensions and position and common events.

Project 1: an image slider

Structure

A slider is a viewport that hides overflow, containing a track that is as wide as all slides together. To show slide n, you shift the track left by n viewport widths. Animating that single margin-left or transform moves every slide at once.

HTML
<div id="slider">
  <div class="track">
    <figure class="slide">...</figure>
    <figure class="slide">...</figure>
    <figure class="slide">...</figure>
  </div>
  <button class="prev">Prev</button>
  <button class="next">Next</button>
  <div class="dots"></div>
</div>

Behaviour

The state is one number, the current index. Everything else (the track position, the highlighted dot, the label) is calculated from it. Four rules cover the behaviour:

  1. Next and previous wrap around. After the last slide comes the first: (index + 1) % count. For going backward use (index - 1 + count) % count, because -1 % 3 is -1 in JavaScript.
  2. One animation at a time. A fast double click should not queue two animations. .stop(true) on the track discards the old one.
  3. Autoplay pauses on hover and on keyboard focus, so people can read.
  4. Dots are buttons built from the slide count, so adding a slide needs no markup change.
Auto-playing image slider

The wrap-around uses (n + count) % count so both -1 and count land in range. The stop(true) call is the important detail: without it, clicking Next quickly queues several animations and the track keeps moving after you stop clicking. (The .stop() family is explained in the animation queue.)

A carousel that moves by itself is hard for some people to use. Pause it on hover and focus, as above, and consider a visible pause button and prefers-reduced-motion support: when that setting is on, skip autoplay entirely.

Project 2: an image magnifier

A magnifier shows a zoomed part of the image under the mouse. The usual technique needs no second request: a "lens" element has the same image as its background, scaled up, and its background-position is shifted so that the point under the mouse sits in the middle of the lens.

The maths is short. Let the image be w by h pixels, the zoom factor be z, the lens be L pixels wide, and the mouse be at (x, y) relative to the image. The enlarged background is w*z by h*z. The point (x, y) is now at (x*z, y*z), and to centre it in the lens we shift the background by L/2 - x*z and L/2 - y*z.

Getting (x, y) is the part where .offset() earns its place: event.pageX - $img.offset().left gives the position inside the image regardless of scrolling.

Hover magnifier

Three decisions are worth noting. The lens has pointer-events: none, otherwise it would sit under the cursor and steal the mousemove events, making the effect flicker. Mouse coordinates are clamped so the lens never reads outside the picture. And on touch devices there is no hover, so offer a click-to-zoom alternative.

Project 3: a spotlight that follows the mouse

The spotlight (or "follow spot") covers the page with a dark layer that has a transparent circle at the pointer. The simplest implementation uses a CSS radial gradient whose centre is set from JavaScript. CSS variables keep jQuery's work down to two numbers.

Mouse-following spotlight

With many events firing per second, mousemove handlers should stay cheap: read the position, write two properties, do nothing else. If your handler does heavier work, throttle it with requestAnimationFrame so it runs at most once per frame.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the slider, why is the wrap-around calculated as (n + count) % count?

    The remainder operator keeps the sign of the dividend. Adding count first makes negative indexes positive before taking the remainder.
  2. 2. What does .stop(true) prevent in the slider?

    stop(true) halts the running animation and clears the queue, so the track always heads for the most recent target.
  3. 3. Why does the magnifier lens have pointer-events: none?

    The lens is positioned under the cursor. Without pointer-events: none the events would target the lens, and the effect would flicker.
  4. 4. How does the magnifier get the mouse position inside the image regardless of page scrolling?

    pageX/pageY are document coordinates and .offset() returns the image's document position, so the difference is the position within the image.

Summary

  • A slider is a viewport with overflow: hidden and a wide track; one index drives the track position, the dots and the label.
  • Wrap indexes with (n + count) % count, use .stop(true) before animating, and pause autoplay on hover and focus.
  • A magnifier is a scaled background on a lens: shift background-position by lensSize/2 - mouse * zoom, and get the mouse position from pageX - $img.offset().left.
  • A spotlight is a radial-gradient overlay whose centre comes from CSS variables, with pointer-events: none so it never blocks clicks.
  • Keep mousemove handlers cheap, and give touch users another way in.

Next: Practice Exercises with Solutions.