How to make a before and after image comparison module using slick-image-compare
In September 2024 I was building a portfolio site for an interior designer on Statamic (Laravel, Tailwind, Alpine and Vite). Project pages are a Replicator over a shared blocks fieldset, one Antlers partial per set. She wanted the renovation pages to show the room before and after, with a handle you drag between the two.
I didn't want to write the drag maths myself that week, so I picked slick-image-compare: vanilla JavaScript, no dependencies, a built file under 30 KB. One npm install and a copy of its stylesheet into resources/css.
The block
The Replicator set is two asset fields:
image_comparison:
display: 'Image Comparison'
icon: media-image-photo-focus-frame
fields:
-
handle: before_image
field:
max_files: 1
container: uploads
type: assets
display: 'Before Image'
-
handle: after_image
field:
max_files: 1
container: uploads
type: assets
display: 'After Image'
The site renders every set through a partial of the same name, so this one lives at resources/views/blocks/image_comparison.antlers.html. An editor adds the block, picks two images, done.
Wiring the JavaScript
The library gets imported once in app.js and hung on window, next to Alpine:
import SlickImageCompare from 'slick-image-compare'
window.SlickImageCompare = SlickImageCompare
and its CSS gets pulled into the main stylesheet after Tailwind's utilities:
@import './slick-image-compare.css';
Exposing it on window is deliberate. Alpine is already on every page, so each block initialises itself with x-init and I never need a document-wide query or an init order. Two comparisons on one page just work. The first version of the partial handed the library two images rendered by the site's {{ img }} tag:
<section class="bg-tan" x-data x-init="() => {
new window.SlickImageCompare($refs.slickImageCompare, {
animateIn: true,
beforeLabel: 'Before',
afterLabel: 'After',
});
}">
<div class="container">
<div x-ref="slickImageCompare">
{{ img :src="before_image" size="768x500" }}
{{ img :src="after_image" size="768x500" }}
</div>
</div>
</section>
That worked. Then came the afternoon.
Nine commits on a Wednesday
The git log for that file on 11 September is a list of one-line commits, and each one is something the first version got wrong.
Width. I fixed the container at w-[768px] to match the image size. Two commits later it became max-w-[768px], because a fixed width overflows a phone. Obvious in hindsight, invisible on a desktop.
Direction. The library's default shows the after image when the handle is on the right. I wanted the after image revealed from the left, which is ltr: false.
Markup images versus URLs. The commit says only "use js". It removed both {{ img }} tags and passed the images as options instead:
afterImage: '{{ glide :src="after_image" width="768" height="500" }}',
beforeImage: '{{ glide :src="before_image" width="768" height="500" }}',
with an empty <div x-ref="slickImageCompare"></div> for the library to fill. The library supports both modes: read the <img> children it finds, or build its own from beforeImage and afterImage. Reading the built source now, in URL mode it measures each image with new Image() and naturalWidth, then appends its own <img draggable="false"> elements. That means everything my {{ img }} tag normally emits (WebP via Glide, width and height attributes, loading="lazy", the asset's alt text) does not go along for the ride. I traded my markup for the library's, and the slider was happier for it.
The handle. animateIn went off, then back on, and handleAngle: 8 arrived in the same commit. A slightly tilted divider line looks less like a form control and more like a page turn. Pure taste, and it took two tries.
Three weeks later I doubled the images to 1500x1000 and let the block take the full container width. This is where the partial sits now:
<section class="bg-tan" x-data x-init="() => {
new window.SlickImageCompare($refs.slickImageCompare, {
animateIn: true,
beforeLabel: 'Before',
afterLabel: 'After',
afterImage: '{{ glide :src="after_image" width="1500" height="1000" }}',
beforeImage: '{{ glide :src="before_image" width="1500" height="1000" }}',
ltr: false,
handleAngle: 8,
});
}">
<div class="container">
{{ partial:spacing method="padding" }}
<div x-ref="slickImageCompare"></div>
{{ /partial:spacing }}
</div>
</section>
What it doesn't do
I grepped the built file before writing this. There is no aria- attribute, no role, no tabindex and no keydown handler anywhere in it. The slider is mouse and touch only, the Before and After labels are plain elements, and in URL mode the two images have no alt text because the library creates them.
So it's decoration. That is fine for a renovation reveal when the page above it already says what was done, which is how these project pages are written. It is not fine if the comparison is the content. In that case put both images in the page yourself with real alt text and let the slider enhance them, which is the markup mode I moved away from. I didn't do that here, and I'm noting it so the next site gets the choice made on purpose instead of by whichever mode the library liked better.
The short version: two asset fields, one global, x-init per block, pass URLs and let the library own the markup. Then budget an afternoon for the nine commits, because the first version is never the one that ships.