Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <div class="img-comp-container">
- <div class="img-comp-img">
- <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">
- </div>
- <div class="img-comp-img img-comp-overlay-2">
- <img src="https://cdn.shopify.com/s/files/1/0285/5482/1691/files/day2_png.jpg?v=1656772827" width="320" height="555">
- </div>
- </div>
- <style>
- .img-comp-container {
- position: relative;
- left: 8rem;
- height: 485px; /*should be the same height as the images*/
- }
- @media (max-width: 767px){
- .img-comp-container{
- left: 0 !important;
- }
- }
- .img-comp-img {
- position: absolute;
- width: auto;
- height: 100%;
- overflow:hidden;
- }
- .img-comp-img img {
- display:block;
- vertical-align:middle;
- max-width: unset !important;
- max-height: 100%;
- }
- .img-comp-slider-1, .img-comp-slider-2, .img-comp-slider-3 {
- position: absolute;
- z-index:5;
- cursor: ew-resize;
- /*set the appearance of the slider:*/
- width: 45px;
- height: 45px;
- background-image: url("https://cdn.shopify.com/s/files/1/0285/5482/1691/files/image_25986b4c-cec5-4545-9532-96c9adb4220b.png") !important;
- background-size: 65% 65%;
- background-color: #000;
- background-repeat: no-repeat;
- background-position: center;
- opacity: 0.7;
- border-radius: 50%;
- }
- </style>
- <script>
- function initComparisons(overlayClass, sliderClass) {
- var x, i;
- /* Find all elements with an "overlay" class: */
- x = document.getElementsByClassName(overlayClass);
- for (i = 0; i < x.length; i++) {
- /* Once for each "overlay" element:
- pass the "overlay" element as a parameter when executing the compareImages function: */
- compareImages(x[i]);
- }
- function compareImages(img) {
- var slider, img, clicked = 0, w, h;
- /* Get the width and height of the img element */
- w = img.offsetWidth;
- h = img.offsetHeight;
- /* Set the width of the img element to 50%: */
- img.style.width = (w / 2) + "px";
- /* Create slider: */
- slider = document.createElement("DIV");
- slider.setAttribute("class", sliderClass);
- /* Insert slider */
- img.parentElement.insertBefore(slider, img);
- /* Position the slider in the middle: */
- slider.style.top = (h / 2) - (slider.offsetHeight / 2) + "px";
- slider.style.left = (w / 2) - (slider.offsetWidth / 2) + "px";
- /* Execute a function when the mouse button is pressed: */
- slider.addEventListener("mousedown", slideReady);
- /* And another function when the mouse button is released: */
- window.addEventListener("mouseup", slideFinish);
- /* Or touched (for touch screens: */
- slider.addEventListener("touchstart", slideReady);
- /* And released (for touch screens: */
- window.addEventListener("touchend", slideFinish);
- function slideReady(e) {
- /* Prevent any other actions that may occur when moving over the image: */
- e.preventDefault();
- /* The slider is now clicked and ready to move: */
- clicked = 1;
- /* Execute a function when the slider is moved: */
- window.addEventListener("mousemove", slideMove);
- window.addEventListener("touchmove", slideMove);
- }
- function slideFinish() {
- /* The slider is no longer clicked: */
- clicked = 0;
- }
- function slideMove(e) {
- var pos;
- /* If the slider is no longer clicked, exit this function: */
- if (clicked == 0) return false;
- /* Get the cursor's x position: */
- pos = getCursorPos(e)
- /* Prevent the slider from being positioned outside the image: */
- if (pos < 0) pos = 0;
- if (pos > w) pos = w;
- /* Execute a function that will resize the overlay image according to the cursor: */
- slide(pos);
- }
- function getCursorPos(e) {
- var a, x = 0;
- e = (e.changedTouches) ? e.changedTouches[0] : e;
- /* Get the x positions of the image: */
- a = img.getBoundingClientRect();
- /* Calculate the cursor's x coordinate, relative to the image: */
- x = e.pageX - a.left;
- /* Consider any page scrolling: */
- x = x - window.pageXOffset;
- return x;
- }
- function slide(x) {
- /* Resize the image: */
- img.style.width = x + "px";
- /* Position the slider: */
- slider.style.left = img.offsetWidth - (slider.offsetWidth / 2) + "px";
- }
- }
- }
- initComparisons('img-comp-overlay-2', 'img-comp-slider-2');
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement