图像比较滑块容器不会弯曲包装

时间:2018-09-25 03:54:09

标签: javascript html css css3 flexbox

以下是我的密码笔的链接:https://codepen.io/Bryandbronstein/pen/NLVQjB

所以这是一个奇怪的问题,我对这个问题一无所知。我已经学习了更多有关CSS和Javascript的知识,并决定尝试在W3C网站上找到的图像比较滑块。它完美地作为一个元素工作,但是我想拥有一个完整的画廊。但是,无论我尝试什么,他们似乎都不想遵守我为其父容器设置的所有flex规则。您会注意到在Codepen中,一个比较容器隐藏在另一个容器之后。有什么想法吗?

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>

1 个答案:

答案 0 :(得分:0)

由于.img-comp-container的所有子元素都已完全定位,因此图像不会自动换行。这样会折叠所有的flex元素,以使flex wrap无法使用。

使.img-comp-container的多个子项中的一个相对或静态,或者将宽度和高度设置为.img-comp-container

以下是我对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用笔寻求解决方案。

相关问题