Skip to the content

Compare slider

A before and after comparison with weight.

Become a member to unlock the full effect today.

Join now

Already a member? Sign in

Documentation

  1. Copy to Webflow

    Add Geist and Geist Mono to your site's fonts (Site settings, Fonts, Google Fonts) before pasting: Webflow removes any font your site has not installed.

  2. Add custom CSS

    Give the box a width and both pictures the same size: the first picture gives the box its height, the second is stretched over it. The box clips what sticks out of it, so a shadow goes on a wrapper around it.
  3. Add custom JS

  4. Use it on your own elements

    Custom attributes
    Select the element on the canvas and open the Settings panel. Under Custom attributes, add a pair: the name on the left, the value on the right.
    Custom attributeWhat it does
    data-compare-slider Marks the box that holds the two pictures. It takes no value: leave the value field empty. The first picture in the box is the one before, the second the one after, and the handle is added to that box, which also carries the settings. Without it on any box of the page nothing is mounted, and the embed says so in the console.
    data-compare-slider-picture Marks the two elements to compare when they are not images, videos or canvases, two blocks with a background for one: the first marked is before, the second after. Without it the first two pictures of the box are taken, and the engine writes the mark on them itself, before and after. [ Optional ]
    data-compare-slider-state Written by the engine on the box: live while the split is moving, idle at rest, still under reduced motion. Never written by hand. [ Optional ]
    data-compare-slider-axis Written by the engine on the box from the orientation setting: x for a split that travels left and right, y for one that travels up and down. Never written by hand; the stylesheet reads it to turn the clip, the rule and the tags. [ Optional ]
    Settings
    Every setting is a custom attribute too, added the same way and on the same element. An attribute always wins over the defaults written in the code.
    AttributeDefaultWhat it is
    data-compare-slider-color #ffab1a Colour of the rule and of the ground of its two tags. Options: any CSS colour, the amber #ffab1a by default.
    data-compare-slider-label-color #15100c Colour of the words on the two tags, read against color. Options: any CSS colour, the ink #15100c on the amber by default.
    data-compare-slider-rest 50 Where the split sits when nobody is on the box, 0 to 100, and where it glides back to when the pointer leaves.
    data-compare-slider-stiffness 140 Pull of the spring that carries the split to the pointer.
    data-compare-slider-damping 16 Brake of the spring, read against stiffness.
    data-compare-slider-intro true Whether the split plays one short sweep the first time the box enters the window. Options: true plays it once, when the box is a third in view and its pictures are loaded; false leaves the split at rest.
    data-compare-slider-follow true Whether the split follows a mouse that only passes over the box. Options: true follows the pointer and glides back to rest when it leaves; false moves on a press or a drag only, and stays where it is left.
    data-compare-slider-orientation horizontal The way the split travels. Options: horizontal, a vertical rule that moves left and right, the first picture on the left; vertical, a horizontal rule that moves up and down, the first picture on top.
    data-compare-slider-before Before Word of the tag on the side of the first picture, also what a screen reader calls that picture. Options: any short word; empty takes the tag away.
    data-compare-slider-after After Word of the tag on the side of the second picture, also what a screen reader calls that picture. Options: any short word; empty takes the tag away.

What it does

Compare slider is a cursor animation for Webflow and vanilla JavaScript, with no library. Also called before after slider, image comparison slider or before and after image slider.

A before and after comparison with weight. Two pictures of the same size share one box, the second laid over the first and clipped at a split, and a thin rule marks the split with a small mono tag on each side. The rule follows the pointer on a spring, so it lags a little and overshoots a little before it settles, and it glides back to its rest position when the pointer leaves. A finger drags it while the page still scrolls the other way, and the handle is a real slider: it takes the focus, the arrow keys move it, and a screen reader hears its value. As the box enters the window the split plays one short sweep, so the frame says it moves before anyone touches it. Horizontal or vertical, no library, and no loop at rest. Under reduced motion nothing springs: the split jumps to where it is asked. Pattern type before and after slider, image comparison, image reveal.

Updated

Questions

Add the custom attribute data-compare-slider to a box whose first two children are the two pictures, the before one first; the script adds the handle. Settings are attributes on the box, for instance data-compare-slider-rest="35", and the two words on the rule are data-compare-slider-before and data-compare-slider-after. Export both pictures at the same size, because two different ratios do not line up.

No. It has no library and no build step: the second picture is cut by clip-path: inset() from one custom property, --compare-slider-at, and the rule is carried by a spring written in the engine. The loop stops as soon as the split has settled and does not exist at rest.

Yes, and it is the default: with follow on, a mouse over the box carries the split, and leaving sends it back to rest, 50 by default. Write data-compare-slider-follow="false" and only a press or a drag moves the split, which then stays where it is left. On touch a finger drags the rule along its axis, and a swipe the other way scrolls the page as usual.

Write data-compare-slider-orientation="vertical": the rule turns horizontal and moves up and down, with the first picture on top. The default is horizontal, the first picture on the left. Inside a horizontal carousel the vertical one is the safe choice, since the box keeps the swipe along its own axis.

Yes. The handle carries role="slider" and is in the tab order: the arrow keys move the split by 2, Shift or the page keys by 10, and Home and End send it to either edge. A screen reader gets aria-valuenow from 0 to 100 and an aria-valuetext that says the share of each picture, so give both pictures an alt.