About this snippet

Show the difference between two images with this interactive comparison slider. It uses a simple range input to control the reveal, so it works perfectly on touch screens and desktops without heavy code.

Size
2.5 KB
Uses
HTML, CSS, JavaScript
Loads
placehold.co
Views
443
<div class="ba-wrapper" id="baWrapper">
  <div class="ba-image-container">
    <img src="https://placehold.co/800x500/e2e8f0/1e293b?text=Original+Design" alt="Before" class="ba-img-base">
    <div class="ba-img-overlay">
      <img src="https://placehold.co/800x500/3b82f6/ffffff?text=New+Design" alt="After" class="ba-img-top">
    </div>
  </div>
  <div class="ba-handle">
    <svg viewBox="0 0 24 24" width="16" height="16" stroke="currentColor" stroke-width="3" fill="none" stroke-linecap="round" stroke-linejoin="round"><polyline points="15 18 9 12 15 6"></polyline></svg>
    <svg viewBox="0 0 24 24" width="16" height="16" stroke="currentColor" stroke-width="3" fill="none" stroke-linecap="round" stroke-linejoin="round"><polyline points="9 18 15 12 9 6"></polyline></svg>
  </div>
  <input type="range" min="0" max="100" value="50" class="ba-range" aria-label="Comparison Slider">
</div>
.ba-wrapper {
  --position: 50%;
  font-family: system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  position: relative;
  width: 100%;
  max-width: 700px;
  margin: 0 auto;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0,0,0,0.1);
}
.ba-image-container {
  position: relative;
  width: 100%;
  height: 0;
  padding-bottom: 62.5%; /* Aspect Ratio */
}
.ba-img-base {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.ba-img-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--position);
  height: 100%;
  overflow: hidden;
  border-right: 2px solid #fff;
}
.ba-img-top {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  object-fit: cover;
  width: 100vw; /* Ensure image does not squish */
  max-width: 700px; /* Match container max-width */
}
.ba-range {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: ew-resize;
  z-index: 20;
  margin: 0;
}
.ba-handle {
  position: absolute;
  top: 50%;
  left: var(--position);
  transform: translate(-50%, -50%);
  width: 44px;
  height: 44px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 4px 10px rgba(0,0,0,0.2);
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #1f2937;
  pointer-events: none;
}
const wrapper = document.getElementById("baWrapper");
const range = wrapper.querySelector(".ba-range");

range.addEventListener("input", function() {
  wrapper.style.setProperty("--position", `${this.value}%`);
});
Esc
↑↓ moveEnter select

Start from a template

Open an .html file, or HTML, CSS, JS, Markdown, Less or TypeScript files. You can also drop them here. Styles and scripts go into their panes.

Share

Source

Keyboard shortcuts

Save