Advertisement
ikai2

Image comparison K

Jul 7th, 2022 (edited)
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 4.42 KB | None | 0 0
  1. <div class="img-comp-container">
  2.   <div class="img-comp-img">
  3.     <img src="https://cdn.shopify.com/s/files/1/0285/5482/1691/files/day1_6cc0e382-3999-4537-bc9d-a0f48ac4ff1e.png?v=1656772827" width="320" height="555">
  4.   </div>
  5.   <div class="img-comp-img img-comp-overlay-2">
  6.     <img src="https://cdn.shopify.com/s/files/1/0285/5482/1691/files/day2_png.jpg?v=1656772827" width="320" height="555">
  7.   </div>
  8. </div>
  9.  
  10. <style>
  11. .img-comp-container {
  12.   position: relative;
  13.   left: 8rem;
  14.   height: 485px; /*should be the same height as the images*/
  15. }
  16.  
  17.    @media (max-width: 767px){
  18.   .img-comp-container{
  19.     left: 0 !important;
  20.   }
  21.   }
  22. .img-comp-img {
  23.   position: absolute;
  24.   width: auto;
  25.   height: 100%;
  26.   overflow:hidden;
  27. }
  28.  
  29. .img-comp-img img {
  30.   display:block;
  31.   vertical-align:middle;
  32.   max-width: unset !important;
  33.   max-height: 100%;
  34. }
  35.  
  36. .img-comp-slider-1, .img-comp-slider-2, .img-comp-slider-3 {
  37.   position: absolute;
  38.   z-index:5;
  39.   cursor: ew-resize;
  40.   /*set the appearance of the slider:*/
  41.   width: 45px;
  42.   height: 45px;
  43.   background-image: url("https://cdn.shopify.com/s/files/1/0285/5482/1691/files/image_25986b4c-cec5-4545-9532-96c9adb4220b.png") !important;
  44.   background-size: 65% 65%;
  45.   background-color: #000;
  46.   background-repeat: no-repeat;
  47.   background-position: center;
  48.   opacity: 0.7;
  49.   border-radius: 50%;  
  50. }
  51. </style>
  52.  
  53. <script>
  54.   function initComparisons(overlayClass, sliderClass) {
  55.   var x, i;
  56.   /* Find all elements with an "overlay" class: */
  57.   x = document.getElementsByClassName(overlayClass);
  58.   for (i = 0; i < x.length; i++) {
  59.    /* Once for each "overlay" element:
  60.    pass the "overlay" element as a parameter when executing the compareImages function: */
  61.    compareImages(x[i]);
  62.  }
  63.  function compareImages(img) {
  64.    var slider, img, clicked = 0, w, h;
  65.    /* Get the width and height of the img element */
  66.    w = img.offsetWidth;
  67.    h = img.offsetHeight;
  68.    /* Set the width of the img element to 50%: */
  69.    img.style.width = (w / 2) + "px";
  70.    /* Create slider: */
  71.    slider = document.createElement("DIV");
  72.    slider.setAttribute("class", sliderClass);
  73.    /* Insert slider */
  74.    img.parentElement.insertBefore(slider, img);
  75.    /* Position the slider in the middle: */
  76.    slider.style.top = (h / 2) - (slider.offsetHeight / 2) + "px";
  77.    slider.style.left = (w / 2) - (slider.offsetWidth / 2) + "px";
  78.    /* Execute a function when the mouse button is pressed: */
  79.    slider.addEventListener("mousedown", slideReady);
  80.    /* And another function when the mouse button is released: */
  81.    window.addEventListener("mouseup", slideFinish);
  82.    /* Or touched (for touch screens: */
  83.    slider.addEventListener("touchstart", slideReady);
  84.     /* And released (for touch screens: */
  85.    window.addEventListener("touchend", slideFinish);
  86.    function slideReady(e) {
  87.      /* Prevent any other actions that may occur when moving over the image: */
  88.      e.preventDefault();
  89.      /* The slider is now clicked and ready to move: */
  90.      clicked = 1;
  91.      /* Execute a function when the slider is moved: */
  92.      window.addEventListener("mousemove", slideMove);
  93.      window.addEventListener("touchmove", slideMove);
  94.    }
  95.    function slideFinish() {
  96.      /* The slider is no longer clicked: */
  97.      clicked = 0;
  98.    }
  99.    function slideMove(e) {
  100.      var pos;
  101.      /* If the slider is no longer clicked, exit this function: */
  102.      if (clicked == 0) return false;
  103.      /* Get the cursor's x position: */
  104.      pos = getCursorPos(e)
  105.      /* Prevent the slider from being positioned outside the image: */
  106.      if (pos < 0) pos = 0;
  107.      if (pos > w) pos = w;
  108.       /* Execute a function that will resize the overlay image according to the cursor: */
  109.       slide(pos);
  110.     }
  111.     function getCursorPos(e) {
  112.       var a, x = 0;
  113.       e = (e.changedTouches) ? e.changedTouches[0] : e;
  114.       /* Get the x positions of the image: */
  115.       a = img.getBoundingClientRect();
  116.       /* Calculate the cursor's x coordinate, relative to the image: */
  117.       x = e.pageX - a.left;
  118.       /* Consider any page scrolling: */
  119.       x = x - window.pageXOffset;
  120.       return x;
  121.     }
  122.     function slide(x) {
  123.       /* Resize the image: */
  124.       img.style.width = x + "px";
  125.       /* Position the slider: */
  126.       slider.style.left = img.offsetWidth - (slider.offsetWidth / 2) + "px";
  127.     }
  128.   }
  129. }
  130.  
  131. initComparisons('img-comp-overlay-2', 'img-comp-slider-2');
  132. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement