This before and after image slider WordPress build lets visitors see a transformation happen right under their mouse. Move the cursor and the old image turns into the new one, no slider plugin and no code needed.
It’s built from two images, a few nested divs, and a single mouse move interaction, then saved as a component so you can drop it anywhere and still tweak each copy on its own.
Set up the before and after slider
From the Add panel, drop a Div into your section. Any heading or paragraph in the section is optional, the effect doesn’t depend on them.
Right-click the div and choose Rename, or double-click its label in the Navigator. Name it Before after wrapper to keep the structure easy to follow.
In the Style panel, set Position to Relative. Any absolutely positioned element inside it will now position itself relative to this wrapper.
From the Add panel, add an Image element to the wrapper, then select your before image from the media library.
Set the image’s width to 100% and its height to auto so it stretches across the wrapper. If your theme adds a border radius to all images, set it to 0 here.
Add the after image
From the Add panel, add another Div inside the Before after wrapper and rename it After wrapper.
In the Style panel, set Position to Absolute and set each single position property (top, right, bottom, left) to 0. Then set Overflow to Hidden.
Duplicate the before image, drop the copy into the After wrapper, and replace it with your after image.
Animate the before and after slider
Select the Before after wrapper, open the Interaction tab, and add a Mouse move interaction.
Select the After wrapper in the Navigator as the target. For the action, add Transform, then Translate, then Translate X, and set To to 100%. On its own, this pushes the after image out of view.
Select the image inside the After wrapper and add a Translate X action to it too, with To set to -100%. The image now stays in place while its wrapper moves, so the after image is revealed as the mouse moves.
In the interaction settings, change Tracking from Whole page to Trigger element. The image comparison slider now only reacts while the mouse is over the images, not anywhere on the page.
Find the Smoothing option in the interaction settings. Any value above 0 makes the reveal trail behind the cursor (it was 60% in the video), so set it to 0 for an instant follow.
Add a divider
From the Add panel, drop in another Div and rename it Divider wrapper. Set Position to Absolute, raise the Z-index a little so it sits on top, and set each single position property to 0 so it stretches across the area.
Duplicate the Divider wrapper, place the copy inside it, and rename it Divider. Give it a background color, for example white.
The divider now covers the whole area, so limit it with a max width. Instead of typing a value, create a new variable named Vertical divider with a value of 6px. Copy its CSS custom property, then press Enter to confirm.
Setting Left to 50% would leave the divider slightly off center, since it doesn’t account for the divider’s own width. Mosaic supports CSS functions like calc(), so set Left to calc(50% – var(–your-variable) / 2), pasting in the custom property you copied. Because it uses the variable, the divider stays centered even when you change the Vertical divider value later.
Select the Divider wrapper in the Navigator and add a Transform action with Translate, then Translate X. Since the divider starts in the center, set Initial to -50% and To to 50%. The divider now moves along the edge between the two images.
Make it a component
Copying and pasting the slider doesn’t scale, since every later change would have to be made in each copy. Instead, create a component from it. Name it Before after, set the main category to Part, and pick a category or make a new one (Uncategorized works too). Click Create, and the slider on the page becomes an instance of the component.
From the Add panel, drop a Component element where you want the slider, then select the Before after part component. Remove any old image it replaces.
If the instance doesn’t fill the available height, click the component icon in the toolbar to enter the original component. Any change you make here affects every instance across your site.
The component’s outer container has no set width or height, so its children decide its size. In a narrower container, the images keep their aspect ratio and scale down, which makes the slider shorter. Set its height to 100% so it fills the space. The images may get cropped as a result, which works fine as the default.
Adjust one instance
Leave the component scope and enter the scope of the specific instance instead. Changes made now only affect this instance.
Select the image, then open the more options at the bottom of the Style panel to find Object fit. Change it from Cover to Contain to keep the full image visible. Or keep Cover and adjust the focus point, for example to center left, so that part of the image always stays in view.
Replace the before image with the one this instance needs. The after image sits hidden under it in the editor, so temporarily hide the other image in the editor to bring it into view, then replace it too.
Result
Your before and after image slider is ready. When visitors move their mouse over it, the after image slides into view and follows the cursor instantly, with a thin divider tracking the edge between the two. Since it’s a component, you can reuse it anywhere, update every copy from one place, and still adjust a single instance when you need to.