Documentation
Splitview is one JavaScript file and one stylesheet. It turns two images into a before and after comparison with a draggable handle. No dependencies, no build step, no network requests.
Install
Copy splitview.js and splitview.css anywhere in your project and include them once per page.
<link rel="stylesheet" href="splitview.css">
<script src="splitview.js"></script>
Markup
One wrapper, two images. The first image is the before, the second is the after. Both images should have the same aspect ratio; the wrapper takes its height from the first image.
<div class="splitview" data-splitview>
<img class="splitview__img--before" src="before.jpg" alt="Before">
<img class="splitview__img--after" src="after.jpg" alt="After">
</div>
Every element with a data-splitview attribute is set up automatically when the page loads. If you add markup later, call Splitview.autoInit() again or create instances yourself.
Data attributes
| Attribute | Type | What it does |
|---|---|---|
data-splitview | empty or JSON | Marks the element for auto setup. A JSON object in the attribute is passed as options. |
data-position | 0-100 | Starting position of the handle, in percent. |
data-orientation | horizontal | vertical | Which way the handle travels. |
data-mode | drag | hover | Pointer drag, or follow the pointer. |
data-auto-animate | flag | Sweep into place once, when the element scrolls into view. |
<div class="splitview" data-splitview='{"position":45,"mode":"hover","labels":false}'> ... </div>
Options
| Option | Default | Notes |
|---|---|---|
position | 50 | Starting position, 0 to 100. |
orientation | 'horizontal' | 'horizontal' or 'vertical'. |
mode | 'drag' | 'drag' moves while the pointer is held. 'hover' follows the pointer and returns on leave. |
labels | true | Show the two labels. |
beforeLabel | 'Before' | Text of the first label. |
afterLabel | 'After' | Text of the second label. |
handle | true | Show the handle. Keep it on: it is also the keyboard control. |
step | 2 | Percent per arrow key press. |
bigStep | 10 | Percent per Shift + arrow, Page Up and Page Down. |
autoAnimate | false | Animate to the start position when the element first scrolls into view. |
duration | 700 | Animation length in milliseconds. |
returnOnLeave | true | In hover mode, animate back to the start position when the pointer leaves. |
ariaLabel | 'Comparison position' | Accessible name for the handle. |
API
var el = document.querySelector('#my-compare');
var slider = new Splitview(el, { position: 40, mode: 'hover' });
slider.setPosition(60); // move to 60%
slider.getPosition(); // 60
slider.animateTo(20, 500); // animate to 20% over 500ms
slider.destroy(); // remove listeners and the elements it created
Splitview.autoInit('[data-splitview]'); // set up everything that is not set up yet
Splitview.version; // '1.0.0'
Events
A splitview:change event fires on the wrapper every time the position changes, including during a drag. It bubbles, so a listener on a parent works too.
el.addEventListener('splitview:change', function (event) {
console.log(event.detail.position); // 0 - 100
});
Styling
The handle, the labels and the split are all CSS. Override these custom properties on the wrapper or in your own stylesheet.
| Property | Default | What it styles |
|---|---|---|
--splitview-handle-size | 44px | Diameter of the round handle. |
--splitview-handle-color | #ffffff | Handle background. |
--splitview-handle-line | #101418 | Colour of the grip lines. |
--splitview-label-bg | rgba(12,16,20,.72) | Label background. |
--splitview-label-color | #ffffff | Label text colour. |
--splitview-duration | 700ms | Animation length, set by the script. |
Accessibility
- The handle is a real
buttonwithrole="slider",aria-valuemin,aria-valuemaxand a livearia-valuenow. - Keyboard: Left and Right arrows move by
step, Up and Down also work, Home goes to 0, End to 100, Page Up and Page Down move bybigStep. Shift plus an arrow usesbigStep. - The handle takes a visible focus ring through
:focus-visible. - Animations are skipped when the visitor has
prefers-reduced-motionset. - Give both images an
altthat says what the visitor is comparing, for example "Kitchen before renovation" and "Kitchen after renovation".
Using it with a CMS or a framework
WordPress
Enqueue the two files in your theme, then paste the markup into a Custom HTML block. The shortcode example below works with a snippet plugin.
function splitview_shortcode($atts) {
$a = shortcode_atts(array('before' => '', 'after' => ''), $atts);
return '<div class="splitview" data-splitview>'
. '<img class="splitview__img--before" src="' . esc_url($a['before']) . '" alt="Before">'
. '<img class="splitview__img--after" src="' . esc_url($a['after']) . '" alt="After">'
. '</div>';
}
add_shortcode('splitview', 'splitview_shortcode');
React, Vue, Svelte
Render the markup, then create the instance in a ref or an effect and call destroy() on unmount.
useEffect(() => {
const instance = new Splitview(ref.current, { position: 50 });
return () => instance.destroy();
}, []);
Limits, honestly
- Both images are always downloaded. Clipping hides part of the after image, it does not save bandwidth. Use compressed images at the size you actually show.
- The two images should share an aspect ratio. Splitview does not crop or letterbox them for you; if the ratios differ the after image will look stretched.
- The wrapper takes its height from the first image, so set a width on the wrapper and let the image set the height.
- It is a display control, not a security control. Anything you hide behind the clip is still in the page and can be opened with the browser tools.
- No lazy loading of its own: add
loading="lazy"to both images if they are far down the page. - Modern browsers only (Chrome, Edge, Firefox, Safari, and their mobile versions). Internet Explorer is not supported.
- No server side component, no image processing, no uploads.
Changelog
1.0.0 - first release. Drag and hover modes, horizontal and vertical, labels, auto animate, keyboard control, public API and events.