代码之家  ›  专栏  ›  技术社区  ›  Petah

如何使图像缩小到适合固定位置的伸缩框模式

  •  0
  • Petah  · 技术社区  · 5 年前

    如何使图像缩小以适合固定位置的弹性框模式?

    尝试以下操作时,图像与页眉/页脚重叠。它需要工作的图像既高,或宽,并在IE。

    .modal-dialog {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
    }
    
    .modal-content {
      display: flex;
      flex-direction: column;
      height: 100%;
    }
    
    .modal-header {
      background: red;
      margin: 20px;
    }
    
    .modal-body {
      background: green;
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      margin: 0 20px;
    }
    
    .modal-footer {
      background: blue;
      margin: 20px;
    }
    <div class="modal">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            header
          </div>
          <div class="modal-body">
            <img src="https://via.placeholder.com/900x1200" />
          </div>
          <div class="modal-footer">
            footer
          </div>
        </div>
      </div>
    </div>

    注意这是一个简单的例子,我实际上使用的是一个复杂的SVG,而不是图像标记,所以我不能使用背景图像。

    0 回复  |  直到 5 年前
        1
  •  0
  •   Salman Arshad    5 年前

    你可以用老的 position: absolute 最大宽度和 max-height :

    /* for demonstration I have replaced all irrelavant styles with this */
    .modal-body {
      width: 80vw;
      height: 60vh;
      margin: 10% auto 0;
      background: green;
    }
    /* parent must be positioned */
    .modal-body {
      position: relative;
    }
    /* size and center */
    .modal-body img {
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      max-width: 100%;
      max-height: 100%;
      margin: auto;
    }
    <div class="modal-body">
      <img src="https://via.placeholder.com/900x1200" />
    </div>
        2
  •  0
  •   Bryce Howitson    5 年前

    只是一个提醒,但默认情况下,图像会尝试保持其纵横比 ,因此只要设置百分比宽度,内联图像就可以增长到适当的高度 或者 仅将高度设置为百分比将保持宽度。

    关键是只设置一个维度,让浏览器计算出其余的维度。

    如果你设定尺寸的话尤其如此 内联 使用适当的HTML结构。

    <div class="modal-body">
        <img src="https://via.placeholder.com/900x1200" width="100%" />
      </div>
    

    现在,您只需使用CSS设置容器的最大宽度,一切都很好。任何使用 img 标签。也就是说,如果在img src属性中使用SVG,就可以使它像图像一样工作。也就是说,如果这样做,我相信您会失去SVG交互。


    因此,假设要将SVG真正放入代码中,则需要对SVG做一些工作。

    注意大多数SVG在标题中有硬编码的高度和宽度 删除硬编码的高度/宽度 “应该”使SVG正确缩放,因为 view-box 保持图像的比例。

    <svg width="299px" height="138px" viewBox="0 0 299 138" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
        <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
            <g id="example">
                <rect id="Rectangle" fill="#D8D8D8" x="0" y="0" width="299" height="138"></rect>
                <circle id="Oval" fill="#46BEC6" cx="53" cy="50" r="26"></circle>
                <circle id="Oval-Copy" fill="#46BEC6" cx="105" cy="76" r="26"></circle>
                <circle id="Oval-Copy-2" fill="#46BEC6" cx="165" cy="95" r="26"></circle>
                <circle id="Oval-Copy-3" fill="#46BEC6" cx="217" cy="63" r="26"></circle>
                <circle id="Oval-Copy-4" fill="#46BEC6" cx="269" cy="32" r="26"></circle>
            </g>
        </g>
    </svg>
    
    
    
    <svg viewBox="0 0 299 138" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
        <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
            <g id="example">
                <rect id="Rectangle" fill="#D8D8D8" x="0" y="0" width="299" height="138"></rect>
                <circle id="Oval" fill="#46BEC6" cx="53" cy="50" r="26"></circle>
                <circle id="Oval-Copy" fill="#46BEC6" cx="105" cy="76" r="26"></circle>
                <circle id="Oval-Copy-2" fill="#46BEC6" cx="165" cy="95" r="26"></circle>
                <circle id="Oval-Copy-3" fill="#46BEC6" cx="217" cy="63" r="26"></circle>
                <circle id="Oval-Copy-4" fill="#46BEC6" cx="269" cy="32" r="26"></circle>
            </g>
        </g>
    </svg>

    如果您不知道需要设置哪一个(例如在动态图像的情况下),则需要在应用正确的度量之前使用javascript对其进行度量。大致如下:

    fixImage(){
      var img = findTheImageInTheDom;
      if (img.width > img.height){
        img.width = "100%";
      } else {
        img.height = "100%";
    }
    
        3
  •  0
  •   Temani Afif    5 年前

    将图像用作背景:

    .modal-dialog {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
    }
    
    .modal-content {
      display: flex;
      flex-direction: column;
      height: 100%;
    }
    
    .modal-header {
      background: red;
      margin: 20px;
    }
    
    .modal-body {
      background: green;
      flex: 1;
      /*display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;*/
      margin: 0 20px;
      background-size:contain;
      background-position:center;
      background-repeat:no-repeat;
    }
    
    .modal-footer {
      background: blue;
      margin: 20px;
    }
    <div class="modal">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            header
          </div>
          <div class="modal-body" style="background-image:url(https://via.placeholder.com/900x1200)">
          </div>
          <div class="modal-footer">
            footer
          </div>
        </div>
      </div>
    </div>

    或者更容易 max-width:100%;max-height:100%

    .modal-dialog {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
    }
    
    .modal-content {
      display: flex;
      flex-direction: column;
      height: 100%;
    }
    
    .modal-header {
      background: red;
      margin: 20px;
    }
    
    .modal-body {
      background: green;
      flex: 1;
      height:100%; /*to be able to use max-height*/
      min-height:0; /*to allow the shrink*/
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      margin: 0 20px;
    }
    
    .modal-footer {
      background: blue;
      margin: 20px;
    }
    img {
      max-width:100%;
      max-height:100%;
      min-width:0;
      min-height:0;
    }
    <div class="modal">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            header
          </div>
          <div class="modal-body" >
           <img src="https://via.placeholder.com/900x1200)">
          </div>
          <div class="modal-footer">
            footer
          </div>
        </div>
      </div>
    </div>