Splitview
← Back to the demo

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

AttributeTypeWhat it does
data-splitviewempty or JSONMarks the element for auto setup. A JSON object in the attribute is passed as options.
data-position0-100Starting position of the handle, in percent.
data-orientationhorizontal | verticalWhich way the handle travels.
data-modedrag | hoverPointer drag, or follow the pointer.
data-auto-animateflagSweep into place once, when the element scrolls into view.
<div class="splitview" data-splitview='{"position":45,"mode":"hover","labels":false}'> ... </div>

Options

OptionDefaultNotes
position50Starting 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.
labelstrueShow the two labels.
beforeLabel'Before'Text of the first label.
afterLabel'After'Text of the second label.
handletrueShow the handle. Keep it on: it is also the keyboard control.
step2Percent per arrow key press.
bigStep10Percent per Shift + arrow, Page Up and Page Down.
autoAnimatefalseAnimate to the start position when the element first scrolls into view.
duration700Animation length in milliseconds.
returnOnLeavetrueIn 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.

PropertyDefaultWhat it styles
--splitview-handle-size44pxDiameter of the round handle.
--splitview-handle-color#ffffffHandle background.
--splitview-handle-line#101418Colour of the grip lines.
--splitview-label-bgrgba(12,16,20,.72)Label background.
--splitview-label-color#ffffffLabel text colour.
--splitview-duration700msAnimation length, set by the script.

Accessibility

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

Changelog

1.0.0 - first release. Drag and hover modes, horizontal and vertical, labels, auto animate, keyboard control, public API and events.