Draggable Image Comparison Slider In JavaScript

Category: Image , Javascript | October 30, 2020
Author: The Programming Expert
Views Total: 228 views
Official Page: Go to website
Last Update: October 30, 2020
License: MIT

Preview:

Draggable Image Comparison Slider In JavaScript

Description:

A simple, plain image comparison slider to compare two images placed in top & bottom layers using mouse drag.

How to use it:

1. Add before & after images to the webpage.

<div class="compare">
  <div class="compare__image">
    <img src="1.png">
  </div>
  <div class="compare__image compare__overlay">
    <img src="2.png">
  </div>
</div>

2. The CSS styles for the image comparison area.

:root {
  --slider-width: 50rem;
  --slider-height: 30rem;
}

.compare {
  position: relative;
  height: var(--slider-height);
  width: var(--slider-width);
}

.compare__image {
  position: absolute;
  height: var(--slider-height);
  width: var(--slider-width);
  border-radius: 0.4rem;
  overflow: hidden;
  user-select: none;
  pointer-events: none;
}

.compare__image img {
  height: var(--slider-height);
  width: var(--slider-width);
  object-fit: cover;
}

3. Stylize the image comparison slider.

.compare__slider {
  position: absolute;
  z-index: 1;
  width: 3.6rem;
  height: 3.6rem;
  background: #121212;
  border: 0.1rem solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 0.2rem 0.8rem rgba(0, 0, 0, 0.8);
  border-radius: 50%;
  cursor: grab;
  transition: border-color 0.2s;
}

.compare__slider:active {
  border: 0.1rem solid rgba(255, 255, 255, 0.3);
  cursor: grabbing;
}

4. The main JavaScript to activate the image comparison slider.

let isClicked = false;
const image = document.querySelector(".compare__overlay");
const width = image.offsetWidth;
const height = image.offsetHeight;
const slider = document.createElement("div");
compare();
function compare() {
  image.parentElement.insertBefore(slider, image);
  slider.classList.add("compare__slider");
  slider.style.top = height / 2 - slider.offsetHeight / 2 + "px";
  slider.style.left = width / 2 - slider.offsetWidth / 2 + "px";
  image.style.width = "50%";
  slider.addEventListener("mousedown", onSlideStart);
  slider.addEventListener("touchstart", onSlideStart);
  window.addEventListener("mouseup", () => (isClicked = false));
  window.addEventListener("touchstop", () => (isClicked = false));
  window.addEventListener("mousemove", onSlideMove);
  window.addEventListener("touchmove", onSlideMove);
}
function onSlideStart(event) {
  event.preventDefault();
  isClicked = true;
}
function onSlideMove(event) {
  if (!isClicked) return;
  doSlide(currentPosition(event));
}
function currentPosition(event = window.event) {
  let xImage = image.getBoundingClientRect();
  let x = 0;
  x = event.pageX - xImage.left;
  if (x < 0) x = 0;
  if (x > width) x = width;
  return x;
}
function doSlide(x) {
  image.style.width = x + "px";
  slider.style.left = image.offsetWidth - slider.offsetWidth / 2 + "px";
}

You Might Be Interested In:


Leave a Reply