Search code examples
javascripthtmlcssflexbox

Image Comparison Slider Containers Won't Flex Wrap


Here's a link to my codepen: https://codepen.io/Bryandbronstein/pen/NLVQjB

So this is a strange issue and I'm at the end of my rope with this one. I've been learning a bit more about CSS and Javascript, and decided to try out an image comparison slider I found on W3C's website. It works perfectly as a single element, however I want to have a full gallery of these. Yet no matter what I try, they don't seem to want to obey any of the flex rules I set for their parent container. You'll notice in the codepen that one comparison container is hidden behind another. Any ideas?

function initComparisons() {
          var x, i;
          /*find all elements with an "overlay" class:*/
          x = document.getElementsByClassName("img-comp-overlay");
          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", "img-comp-slider");
            /*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("touchstop", 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;
              slider.style.border = "0";
              /*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;
              slider.style.border = "3px solid white";
            }
            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 || window.event;
              /*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";
            }
          }
        } 
html, body { 
          background-color: #333333;
          margin: 0;
          padding: 0;
         }

        * {box-sizing: border-box;}

        .gallery_text {
          color: white;
          font-family: Abel, Helvetica, sans-serif;
          font-size: 1.7rem;
          text-align: center;
          line-height: 1.8em;
        }

        .row{
          display: flex;
          flex-wrap: wrap;
          justify-content: center;
          align-items: center;
        }

        .img-comp-container{
          position: relative;
          flex: 50%;
        }

        .img-comp-overlay{
          border-right: 2px solid rgba(51,51,51, .5) ;
        }

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

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

        .img-comp-slider {
          position: absolute;
          z-index: 9;
          cursor: ew-resize;
          width: 40px;
          height: 40px;
          transform: rotate(136deg);
          background-color: #333333;
          opacity: .8;
          border-radius: 10%;
          border: 3px solid white;
        }
 <body onload="initComparisons();">

	<div class="row">

			<div class="img-comp-container" >
			  <div class="img-comp-img">
			    <img src="https://images.pexels.com/photos/162389/lost-places-old-decay-ruin-162389.jpeg?cs=srgb&dl=black-and-white-dark-building-162389.jpg&fm=jpg" width="500" height="450">
			  </div>
			  	<div class="seperator"></div>
			  <div class="img-comp-img img-comp-overlay">
			    <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSslfAcWKXuMxBpzcJC5ZUyFqMOb2Jtd12x4kBUGG9mTe3KeMJz" width="500" height="450">
			  </div>
			</div>

			<div class="img-comp-container">
			  <div class="img-comp-img">
			    <img src="https://images.pexels.com/photos/162389/lost-places-old-decay-ruin-162389.jpeg?cs=srgb&dl=black-and-white-dark-building-162389.jpg&fm=jpg" width="500" height="450">
			  </div>
			  	<div class="seperator"></div>
			  <div class="img-comp-img img-comp-overlay">
			    <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSslfAcWKXuMxBpzcJC5ZUyFqMOb2Jtd12x4kBUGG9mTe3KeMJz" width="500" height="450">
			  </div>
			</div>

			<div class="img-comp-container">
			  <div class="img-comp-img">
			    <img src="https://images.pexels.com/photos/162389/lost-places-old-decay-ruin-162389.jpeg?cs=srgb&dl=black-and-white-dark-building-162389.jpg&fm=jpg" width="500" height="450">
			  </div>
			  	<div class="seperator"></div>
			  <div class="img-comp-img img-comp-overlay">
			    <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSslfAcWKXuMxBpzcJC5ZUyFqMOb2Jtd12x4kBUGG9mTe3KeMJz" width="500" height="450">
			  </div>
			</div>
		
	</div>

</body>


Solution

  • The images won't wrap because all children of .img-comp-container were absolutely positioned. This collapsed all the flex elements so that the flex wrap wouldn't work.

    Either make one of more of the children of .img-comp-container relative or static, or set a width and height to .img-comp-container.

    Here are the changes I made to the CSS:

    .img-comp-container{
      position: relative;
      flex: 50%;
    }
    
    .img-comp-img {
      position: relative;
      width: auto;
      height: auto;
      overflow: hidden;
    }
    
    .img-comp-overlay {
      border-right: 2px solid rgba(51, 51, 51, 0.5);
      position: absolute;
      top: 0;
      left: 0;
    }
    

    See this codepen fork of your pen for a working solution.