Skip to content

Image Compare

The Image Compare component provides an interactive way to compare two images by sliding a divider between them. This is useful for before/after comparisons, showing differences between images, or highlighting changes.

This component has no additional dependencies beyond the Metro UI core.

<div class="image-compare" data-role="image-compare">
<img src="image1.jpg" alt="Before">
<img src="image2.jpg" alt="After">
</div>
// Initialize with default options
Metro.makePlugin("#myImageCompare", "image-compare");
// Initialize with custom options
Metro.makePlugin("#myImageCompare", "image-compare", {
width: "100%",
height: "16/9",
onSliderMove: function(e) {
console.log("Slider moved to position:", e.x);
}
});
ParameterTypeDefaultDescription
imageCompareDeferrednumber0Delay before initialization (milliseconds)
widthstring”100%“Width of the component (CSS value or percentage)
heightstring”auto”Height of the component (CSS value, percentage, or aspect ratio like “16/9”, “21/9”, “4/3”)
onSliderMovefunctionMetro.noopCallback function triggered when the slider is moved
onImageCompareCreatefunctionMetro.noopCallback function triggered when the component is created
  • changeAttribute(attr, val) - Changes the specified attribute of the component.
  • destroy() - Removes the component and its event handlers.
const imageCompare = Metro.getPlugin('#myImageCompare', 'image-compare');
imageCompare.destroy();
EventDescription
onSliderMoveTriggered when the slider is moved. The event object contains the x-coordinate and left position of the slider.
onImageCompareCreateTriggered when the component is created. The event object contains a reference to the element.
VariableDefault (Light)Dark ModeDescription
@orangeOrange colorSameBackground color of the slider
@borderRadiusBorder radius valueSameBorder radius for the slider
/* Custom styling for a specific image compare component */
#myImageCompare .image-slider {
background-color: #ff0000; /* Red slider */
opacity: 1;
}
  • .image-compare - The main container class for the component
  • .image-container - Container for the first image
  • .image-container-overlay - Container for the second image (overlay)
  • .image-slider - The slider element that divides the two images
  • .image-wrapper - Wrapper for each image

The component creates the following structure:

<div class="image-compare">
<div class="image-container">
<div class="image-wrapper" style="background-image: url(image1.jpg)"></div>
</div>
<div class="image-container-overlay">
<div class="image-wrapper" style="background-image: url(image2.jpg)"></div>
</div>
<div class="image-slider"></div>
</div>
  1. Use high-quality images with the same dimensions for best results
  2. Ensure the images being compared are aligned properly
  3. Consider adding descriptive text near the component to explain what is being compared
  4. Use appropriate image formats (JPEG for photos, PNG for graphics with transparency)