【问题标题】:Unable to center modal image vertically无法将模态图像垂直居中
【发布时间】:2021-09-17 04:57:22
【问题描述】:

我正在处理此照片库,单击图像缩略图后会打开一个模式以全尺寸显示图像。

模态效果很好,但是当图像高度不足时,图像不会显示在中心。中心是指垂直。在水平方向上,它完全位于中心。

模态代码取自W3School's网站,我通过删除标题更改了html和css。

我尝试了什么:

我尝试通过在.modal 类中添加以下代码来使模态居中:

  top:50%; 
  left:50%; 
  transform:translate(-50%, -50%); 

也试过margin: 0 auto。但似乎没有任何效果。

这是模态框的工作版本,其图像未垂直居中:

// Get the modal
const modal = document.querySelector(".modal");

// Get the image and insert it inside the modal - use its "alt" text as a caption
const img = document.querySelectorAll(".myImg");
const modalImg = document.querySelector(".img01");


img.forEach((item) => {
  item.onclick = function(e) {
    modal.style.display = "block";
    modalImg.src = e.target.src;
  }
});


// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}

window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = 'none';
  }
}
.myImg {
  border-radius: 5px;
  cursor: pointer;
  -webkit-transition: 0.3s;
  -o-transition: 0.3s;
  transition: 0.3s;
}

.myImg:hover {
  opacity: 0.7;
}

.modal {
  display: none;
  position: fixed;
  z-index: 999;
  padding-top: 35px;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: rgb(0, 0, 0);
  background-color: rgba(0, 0, 0, 0.9);
}


/* Modal Content (image) */

.modal-content {
  margin: auto;
  display: block;
}


/* Add Animation */

.modal-content {
  -webkit-animation-name: zoom;
  -webkit-animation-duration: 0.6s;
  animation-name: zoom;
  animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
  from {
    -webkit-transform: scale(0)
  }
  to {
    -webkit-transform: scale(1)
  }
}

@keyframes zoom {
  from {
    -webkit-transform: scale(0);
    transform: scale(0)
  }
  to {
    -webkit-transform: scale(1);
    transform: scale(1)
  }
}


/* The Close Button */

.close {
  position: absolute;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  -webkit-transition: 0.3s;
  -o-transition: 0.3s;
  transition: 0.3s;
}

.close:hover,
.close:focus {
  color: #bbb;
  text-decoration: none;
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img class="myImg" src="https://lazykcamping.com/assets/Slide-3-Field.jpg" style="width:100%;max-width:500px">


<!-- The Modal -->
<div class="modal">
  <span class="close">&times;</span>
  <img class="modal-content img01">
</div>

如何垂直居中?

【问题讨论】:

    标签: javascript html jquery css css-position


    【解决方案1】:

    将此添加到.modal-content

    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    

    【讨论】:

    • 我以前试过这个。但是模态图像从最右边打开然后进入中间。
    【解决方案2】:

    这应该使模态图像居中。签入全屏视图。

    编辑:一行 JS,两行 CSS。 (剩下的就是你的代码)

    // Get the modal
    const modal = document.querySelector(".modal");
    
    // Get the image and insert it inside the modal - use its "alt" text as a caption
    const img = document.querySelectorAll(".myImg");
    const modalImg = document.querySelector(".img01");
    
    
    img.forEach((item) => {
      item.onclick = function(e) {
        modal.style.setProperty("display", "flex", "important");
        modalImg.src = e.target.src;
      }
    });
    
    
    // Get the <span> element that closes the modal
    var span = document.getElementsByClassName("close")[0];
    
    // When the user clicks on <span> (x), close the modal
    span.onclick = function() {
      modal.style.display = "none";
    }
    
    window.onclick = function(event) {
      if (event.target == modal) {
        modal.style.display = 'none';
      }
    }
    .myImg {
      border-radius: 5px;
      cursor: pointer;
      -webkit-transition: 0.3s;
      -o-transition: 0.3s;
      transition: 0.3s;
    }
    
    .myImg:hover {
      opacity: 0.7;
    }
    
    .modal {
      display: none;
      position: fixed;
      align-items: center;
    justify-content: center;
      z-index: 999;
      padding-top: 35px;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      overflow: auto;
      background-color: rgb(0, 0, 0);
      background-color: rgba(0, 0, 0, 0.9);
    }
    
    .center-img{
    display: flex;
    align-items: center;
    justify-content: center;
    }
    /* Modal Content (image) */
    
    .modal-content {
      margin: auto;
      display: block;
    }
    
    
    /* Add Animation */
    
    .modal-content {
      -webkit-animation-name: zoom;
      -webkit-animation-duration: 0.6s;
      animation-name: zoom;
      animation-duration: 0.6s;
    }
    
    @-webkit-keyframes zoom {
      from {
        -webkit-transform: scale(0)
      }
      to {
        -webkit-transform: scale(1)
      }
    }
    
    @keyframes zoom {
      from {
        -webkit-transform: scale(0);
        transform: scale(0)
      }
      to {
        -webkit-transform: scale(1);
        transform: scale(1)
      }
    }
    
    
    /* The Close Button */
    
    .close {
      position: absolute;
      top: 15px;
      right: 35px;
      color: #f1f1f1;
      font-size: 40px;
      font-weight: bold;
      -webkit-transition: 0.3s;
      -o-transition: 0.3s;
      transition: 0.3s;
    }
    
    .close:hover,
    .close:focus {
      color: #bbb;
      text-decoration: none;
      cursor: pointer;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
     <img class="myImg" src="https://lazykcamping.com/assets/Slide-3-Field.jpg" style="width:100%;max-width:500px">
    
    
    
    <!-- The Modal -->
    <div class="modal">
      <span class="close">&times;</span>
      <img class="modal-content img01">
    </div>

    【讨论】:

    • 嘿。模态中的图像未居中。不是那个图片。
    • aaa,现在它应该在编辑后工作。我看错了你的问题。
    猜你喜欢
    • 2014-02-14
    • 2016-12-30
    • 2012-10-02
    • 2010-12-30
    • 1970-01-01
    • 2019-01-29
    • 2012-07-30
    相关资源
    最近更新 更多