Skip to content
animorio
IT
Back to the library

Drag-to-close lightbox

Transitions, ImagesGSAPMediumCompressed weight: Demo 7.1 KBGSAP 28 KBFreeDownload the zip

The gallery of Halden, a made-up photography studio, with six real photos of a coastline: a click on a thumbnail flies the photo from its place to the middle of the screen, measured, with a single scale and a crop that opens it without ever stretching it, while the page darkens and the caption, the counter and the buttons arrive. The arrows move to the next photo. Dragging the photo down it follows the finger, shrinks and lets the page through; released past the threshold it flies back exactly into its thumbnail on a spring, otherwise it goes back to the middle. Minimal style, a single accent.

The code

A folder that runs on its own: index.html, style.css, script.js and the libraries from a CDN. The React and Vue tabs hold the same demo as a ready component, which switches its animations off when it leaves the page.

<!doctype html>
<!--
  "Drag-to-close lightbox" from Animorio · https://animorio.com
  License: free on your own sites and your clients’ sites, paid work included.
  Not for resale, redistribution, templates or code collections · https://animorio.com/en/license/
-->
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Drag-to-close lightbox</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fontsource-variable/mona-sans@5.3.0/wdth.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fontsource-variable/jetbrains-mono@5.3.0/index.css">
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <!-- The gallery of an invented photography studio, Halden, with six photographs of a coastline. A click on a
       thumbnail flies the photo from its place to the middle of the screen while the page darkens; the arrows and the
       keyboard move to the next one. Dragging the photo down makes it smaller and lets the page through, and on release
       it flies back exactly into its thumbnail, or back to the middle if the pull was too short. -->
  <main class="pagina">
    <header class="testa" data-entra>
      <p class="marca">
        <svg viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="12" r="8.5" fill="none" stroke="currentColor" stroke-width="1.5"/><path d="M3.5 13.5c3-2.6 5.6-2.6 8.5 0s5.5 2.6 8.5 0" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/></svg>
        Halden Studio
      </p>
      <h1 class="titolo">Coastline</h1>
      <p class="nota">Six photographs, autumn 2026</p>
    </header>

    <ul class="griglia" data-griglia>
      <li><button type="button" class="miniatura" data-miniatura data-titolo="The bridge at low tide" data-luogo="Shell Bay" data-entra><img src="baia.webp" width="960" height="640" alt="A beach at low tide with a stream and a bridge under green hills"></button></li>
      <li><button type="button" class="miniatura" data-miniatura data-titolo="Dry grass and open sea" data-luogo="North Head" data-entra><img src="promontorio.webp" width="960" height="720" alt="A dry grass headland above a deep blue sea"></button></li>
      <li><button type="button" class="miniatura" data-miniatura data-titolo="Swell, before the wind" data-luogo="Outer Banks" data-entra><img src="mare.webp" width="960" height="638" alt="The open sea with slow waves under a pale sky"></button></li>
      <li><button type="button" class="miniatura" data-miniatura data-titolo="Cliffs against the sun" data-luogo="Gull Point" data-entra><img src="scogliera.webp" width="960" height="640" alt="Dark cliffs above a bright sea with the sun low"></button></li>
      <li><button type="button" class="miniatura" data-miniatura data-titolo="Reeds at the edge" data-luogo="Salt Marsh" data-entra><img src="palude.webp" width="960" height="640" alt="Golden reeds bending into still water"></button></li>
      <li><button type="button" class="miniatura" data-miniatura data-titolo="Watching the breakers" data-luogo="Black Rocks" data-entra><img src="scogli.webp" width="960" height="686" alt="A person in a hat standing on mossy rocks facing the waves"></button></li>
    </ul>
  </main>

  <div class="lb" data-lb aria-hidden="true">
    <div class="lb__fondo" data-lb-fondo></div>
    <div class="lb__foto" data-lb-foto role="dialog" aria-modal="true" aria-label="Photo viewer">
      <div class="lb__scivola" data-lb-scivola><img data-lb-img src="baia.webp" alt=""></div>
    </div>
    <div class="lb__barra" data-lb-barra>
      <p class="lb__didascalia"><span class="lb__titolo" data-lb-titolo></span><span class="lb__luogo" data-lb-luogo></span></p>
      <p class="lb__conto" data-lb-conto></p>
    </div>
    <button type="button" class="lb__tasto lb__chiudi" data-lb-chiudi aria-label="Close">
      <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6.5 6.5l11 11M17.5 6.5l-11 11" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round"/></svg>
    </button>
    <button type="button" class="lb__tasto lb__prima" data-lb-prima aria-label="Previous photo">
      <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M14.5 6l-6 6 6 6" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"/></svg>
    </button>
    <button type="button" class="lb__tasto lb__dopo" data-lb-dopo aria-label="Next photo">
      <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M9.5 6l6 6-6 6" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"/></svg>
    </button>
    <p class="lb__aiuto" data-lb-aiuto>Drag down to close</p>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/gsap@3.15.0/dist/gsap.min.js"></script>
  <script src="script.js"></script>
</body>
</html>

Playground

Change values, colours and text with the controls and watch the animation respond live. Or write in the code yourself. Then download the folder with your changes.

18 values to change

Works with Claude, Claude Code, ChatGPT, Cursor, VS Code, Windsurf, Codex, Gemini CLI

Or ask your AI assistant for it

With the Animorio connector, Claude Code, ChatGPT, Cursor or VS Code take it from here, guide included, and fit it into your project. Paste this request:

Take “Drag-to-close lightbox” from Animorio and fit it into my site
Platform licenseFree on your sites and your clients’ sites. Want it inside a website builder, a template platform or a client dashboard? There’s a license for agencies and platforms.

What it does

On a warm white page the Halden Studio brand, the Coastline title and a grid of six thumbnails: low tide under the bridge, the dry grass headland, the slow waves, the cliffs against the sun, the reeds, a person in a hat on the rocks. On hover the thumbnail leans in a little and the others dim slightly. A click and the photo flies from its frame to the middle: it starts cropped like the thumbnail and opens as it grows, the corners tighten, the page behind turns almost black; then the title, the place, the «2 / 6» count, the close cross and the arrows arrive. The arrows or the keyboard move to the next photo with a short slide. Grabbing the photo and pulling it down, it follows the finger, gets smaller and the page shows again; let go low enough, or with a flick, and the photo flies into its thumbnail and settles there on a spring; let go earlier and it goes back to the middle. When nobody touches, the gallery plays by itself: it opens a photo, moves to the next, pulls it down and closes it.

How it works

Where the open photo goes

The open photo has a box measured again on every frame: as big as fits between the cross at the top and the caption at the bottom, with the photo’s proportions. So it holds even if the window changes size halfway through the flight.

The measured flight

The element is always as big as the final box; a single number, p, runs from 0 (the thumbnail) to 1 (the middle). From p come the move of the centre, a single scale, max(thumbnail / box) over the two sides, and a clip-path inset that at p = 0 crops the photo like the thumbnail (4:3, object-fit cover) and at p = 1 crops nothing: the photo never stretches.

The corners

The clip-path radius goes from the thumbnail radius divided by the scale, so on screen it stays the same, to the 6 pixels of the open photo.

The drag

Pulling down, dy follows the finger, dx follows it at half and upwards the photo resists; drag = dy / (0.45 · height) shrinks the photo (scale 1 − drag · SHRINK), lightens the backdrop and fades the caption and buttons. The finger’s speed is measured with the real time between two moves.

Back into the thumbnail

Past DISMISS_AT of the height, or with a quick flick, p goes back to 0 and dx, dy and the drag scale to zero together, with back.out: the photo lands exactly on its thumbnail, overshoots by a breath and settles. Otherwise only the drag goes back to zero and the photo returns to the middle.

Values to change

NameFileDefaultWhat it changes
OPEN_TIMEscript.js0.62How long the flight from the thumbnail to the middle lasts, in seconds.
CLOSE_TIMEscript.js0.6How long the flight back into the thumbnail lasts, in seconds.
CLOSE_SPRINGscript.js1.3How far the photo overshoots the thumbnail before settling (1 none, 2 a big bounce).
DISMISS_ATscript.js0.16How far to pull down before it closes, as a share of the screen height.
SHRINKscript.js0.5How much the photo shrinks while pulled down.
DARKNESSscript.js0.92How dark the page gets behind the open photo (0 to 1).
OPEN_RADIUSscript.js6The corners of the open photo, in pixels.
AUTO_PILOTscript.js2.5After how many still seconds the gallery plays by itself (0 never).

Try them in the Playground

When to use it

In the galleries of a photographer, a studio, a hotel, a restaurant or a shop: wherever a big photo opens from a grid and should close with a gesture, as on a phone.

On phones

The page is sized on the height too; the open photo takes almost the whole width and the arrows go away (keyboard and drag stay). You pull it down with a finger, with the same physics.

With reduced motion

No flight: the lightbox opens and closes with a fade, and the next photo fades in without sliding.

Pitfalls

  • Never animate width and height: the element is always as big as the open photo, and the thumbnail comes from a single scale plus a clip-path; with two different scales the photo would stretch.
  • The final box and the thumbnail box are measured again on every frame, not only at the start: if the window changes height during the flight, the photo still lands right.
  • The clip-path radius must be divided by the scale, or the corners on the thumbnail would be smaller than the real ones.
  • The photo has touch-action: none and setPointerCapture: without them, on a phone the finger would scroll the page.

Libraries

GSAP 3.15.0, Mona Sans (font variabile) 5.3.0, JetBrains Mono (font variabile) 5.3.0.

Compressed with gzip, the three demo files weigh 7.1 KB; the libraries: GSAP 28 KB.

Testing

Tested on 27 November 2026

  • Desktop: Done
  • Phone: Done
  • Reduced motion: Done
  • Safari on iPhone: To do

License

Use it in your own projects and in your clients’ projects. Do not resell it or redistribute it as is. GSAP stays under its own Standard license. Full license.

All animations