Advertisement
ikai2

before and after

Sep 12th, 2022 (edited)
852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 4.38 KB | None | 0 0
  1. <div class="before-after-container">
  2.         <div class="image-container">
  3.         <!-- update the before image URL in the src down below -->
  4.           <img
  5.            class="image-before slider-image"
  6.           src="https://cdn.shopify.com/s/files/1/0335/3618/0361/t/17/assets/img_2888-1663639342920.jpg?v=1663639344"
  7.            alt="color photo"
  8.          />
  9.         <!-- update the after image URL in the src down below -->
  10.           <img
  11.            class="image-after slider-image"
  12.            src="https://cdn.shopify.com/s/files/1/0335/3618/0361/t/17/assets/pf133f59b2thewitcherminimalistye1366x768-1647361942303.jpg?v=1647362024"
  13.            alt="black and white"
  14.          />
  15.         </div>
  16.         <!-- step="10" -->
  17.         <input
  18.          type="range"
  19.          min="0"
  20.          max="100"
  21.          value="50"
  22.          aria-label="Percentage of before photo shown"
  23.          class="slider"
  24.        />
  25.         <div class="slider-line" aria-hidden="true"></div>
  26.         <div class="slider-button" aria-hidden="true">
  27.           <svg
  28.            xmlns="http://www.w3.org/2000/svg"
  29.            width="30"
  30.            height="30"
  31.            fill="currentColor"
  32.            viewBox="0 0 256 256"
  33.          >
  34.             <rect width="256" height="256" fill="none"></rect>
  35.             <line
  36.              x1="128"
  37.              y1="40"
  38.              x2="128"
  39.              y2="216"
  40.              fill="none"
  41.              stroke="currentColor"
  42.              stroke-linecap="round"
  43.              stroke-linejoin="round"
  44.              stroke-width="16"
  45.            ></line>
  46.             <line
  47.              x1="96"
  48.              y1="128"
  49.              x2="16"
  50.              y2="128"
  51.              fill="none"
  52.              stroke="currentColor"
  53.              stroke-linecap="round"
  54.              stroke-linejoin="round"
  55.              stroke-width="16"
  56.            ></line>
  57.             <polyline
  58.              points="48 160 16 128 48 96"
  59.              fill="none"
  60.              stroke="currentColor"
  61.              stroke-linecap="round"
  62.              stroke-linejoin="round"
  63.              stroke-width="16"
  64.            ></polyline>
  65.             <line
  66.              x1="160"
  67.              y1="128"
  68.              x2="240"
  69.              y2="128"
  70.              fill="none"
  71.              stroke="currentColor"
  72.              stroke-linecap="round"
  73.              stroke-linejoin="round"
  74.              stroke-width="16"
  75.            ></line>
  76.             <polyline
  77.              points="208 96 240 128 208 160"
  78.              fill="none"
  79.              stroke="currentColor"
  80.              stroke-linecap="round"
  81.              stroke-linejoin="round"
  82.              stroke-width="16"
  83.            ></polyline>
  84.           </svg>
  85.         </div>
  86.       </div>
  87. <style>
  88. div:empty {
  89.     display: block;
  90. }
  91.  
  92. .image-container * img {
  93.   display: block;
  94.   max-width: 100%;
  95. }
  96.  
  97. .before-after-container {
  98.   display: grid;
  99.   place-content: center;
  100.   position: relative;
  101.   overflow: hidden;
  102.   /* border-radius: 1rem; */
  103.   --position: 50%;
  104. }
  105.  
  106. /*.image-container {
  107.   max-width: 800px;
  108.   max-height: 90vh;
  109.   aspect-ratio: 1/1;
  110. }*/
  111.  
  112. .slider-image {
  113.   width: 100%;
  114.   height: 100%;
  115.   object-fit: cover;
  116.   object-position: left;
  117. }
  118.  
  119. .image-before {
  120.   position: absolute;
  121.   inset: 0;
  122.   width: var(--position);
  123.  /* filter: grayscale(100%);*/
  124. }
  125.  
  126. .slider {
  127.   position: absolute;
  128.   inset: 0;
  129.   cursor: pointer;
  130.   opacity: 0;
  131.   /* for Firefox */
  132.   width: 100%;
  133.   height: 100%;
  134. }
  135.  
  136. .slider:focus-visible ~ .slider-button {
  137.   outline: 5px solid black;
  138.   outline-offset: 3px;
  139. }
  140.  
  141. .slider-line {
  142.   position: absolute;
  143.   inset: 0;
  144.   width: .2rem;
  145.   height: 100%;
  146.   background-color: #fff;
  147.   /* z-index: 10; */
  148.   left: var(--position);
  149.   transform: translateX(-50%);
  150.   pointer-events: none;
  151. }
  152.  
  153. .slider-button {
  154.   position: absolute;
  155.   background-color: #fff;
  156.   color: black;
  157.   padding: .5rem;
  158.   border-radius: 100vw;
  159.   display: grid;
  160.   place-items: center;
  161.   top: 50%;
  162.   left: var(--position);
  163.   transform: translate(-50%, -50%);
  164.   pointer-events: none;
  165.   /* z-index: 100; */
  166.   box-shadow: 1px 1px 1px hsl(0, 50%, 2%, .5);
  167. }
  168. </style>
  169.  
  170. <script>
  171. const container = document.querySelector('.before-after-container');
  172. document.querySelector('.slider').addEventListener('input', (e) => {
  173.   container.style.setProperty('--position', `${e.target.value}%`);
  174. })
  175. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement