如何创建:图片对比滑块

学习如何创建比较两幅图像的滑块。

图像对比滑块

移动蓝色滑块可比较图像:

创建图像比较滑块

第一步 - 添加 HTML:

<div class="img-comp-container">
  <div class="img-comp-img">
    <img src="img_snow.jpg" width="300" height="200">
  </div>
  <div class="img-comp-img img-comp-overlay">
    <img src="img_forest.jpg" width="300" height="200">
  </div>
</div>

第二步 - 添加 CSS:

容器必须具有“相对”定位。

* {box-sizing: border-box;}

.img-comp-container {
  position: relative;
  height: 200px; /* 应与图像高度相同 */
}

.img-comp-img {
  position: absolute;
  width: auto;
  height: auto;
  overflow: hidden;
}

.img-comp-img img {
  display: block;
  vertical-align: middle;
}

.img-comp-slider {
  position: absolute;
  z-index: 9;
  cursor: ew-resize;
  /* 设置滑块的外观: */
  width: 40px;
  height: 40px;
  background-color: #2196F3;
  opacity: 0.7;
  border-radius: 50%;
}

第三步 - 添加 JavaScript:

function initComparisons() {
  var x, i;
  /* 查找具有 "overlay" 类的所有元素: */
  x = document.getElementsByClassName("img-comp-overlay");
  for (i = 0; i < x.length; i++) {
    /* 
	对于每个“覆盖”元素执行一次:
    执行compareImages函数时,将“覆盖”元素作为参数传递:
	*/
    compareImages(x[i]);
  }
  function compareImages(img) {
    var slider, img, clicked = 0, w, h;
    /* 获取 img 元素的宽度和高度 */
    w = img.offsetWidth;
    h = img.offsetHeight;
    /* 将 img 元素的宽度设置为 50%: */
    img.style.width = (w / 2) + "px";
    /* 创建滑块: */
    slider = document.createElement("DIV");
    slider.setAttribute("class", "img-comp-slider");
    /* 插入滑块 */
    img.parentElement.insertBefore(slider, img);
    /* 将滑块置于中间: */
    slider.style.top = (h / 2) - (slider.offsetHeight / 2) + "px";
    slider.style.left = (w / 2) - (slider.offsetWidth / 2) + "px";
    /* 按下鼠标按钮时执行的函数: */
    slider.addEventListener("mousedown", slideReady);
    /* 释放鼠标按钮时执行的另一个函数: */
    window.addEventListener("mouseup", slideFinish);
    /* 或者触摸(对于触摸屏:) */
    slider.addEventListener("touchstart", slideReady);
     /* 并且释放(对于触摸屏:) */
    window.addEventListener("touchend", slideFinish);
    function slideReady(e) {
      /* 防止在图像上移动时可能发生的任何其他操作: */
      e.preventDefault();
      /* 滑块现已被点击并准备移动: */
      clicked = 1;
      /* 当滑块移动时执行的函数: */
      window.addEventListener("mousemove", slideMove);
      window.addEventListener("touchmove", slideMove);
    }
    function slideFinish() {
      /* 不再单击滑块: */
      clicked = 0;
    }
    function slideMove(e) {
      var pos;
      /* 如果不再点击滑块,则退出此函数: */
      if (clicked == 0) return false;
      /* 获取光标的 x 位置: */
      pos = getCursorPos(e)
      /* 防止滑块位于图像之外: */
      if (pos < 0) pos = 0;
      if (pos > w) pos = w;
      /* 执行一个函数,根据光标调整覆盖图像的大小: */
      slide(pos);
    }
    function getCursorPos(e) {
      var a, x = 0;
      e = (e.changedTouches) ? e.changedTouches[0] : e;
      /* 获取图像的 x 位置: */
      a = img.getBoundingClientRect();
      /* 计算光标相对于图像的 x 坐标: */
      x = e.pageX - a.left;
      /* 考虑任何页面滚动: */
      x = x - window.pageXOffset;
      return x;
    }
    function slide(x) {
      /* 调整图像大小: */
      img.style.width = x + "px";
      /* 定位滑块: */
      slider.style.left = img.offsetWidth - (slider.offsetWidth / 2) + "px";
    }
  }
}

第四步 - 执行脚本:

<script>
initComparisons();
</script>

亲自试一试