簡體   English   中英

無法垂直居中模式圖像

[英]Unable to center modal image vertically

我正在處理這個照片庫,單擊圖像縮略圖后會打開一個模式以顯示全尺寸圖像。

模態工作完美,但當圖像高度較短時,圖像不會顯示在中心。 我所說的中心是垂直的。 從水平上看,它完全位於中心。

模態代碼取自W3School 的網站,我通過刪除標題稍微更改了 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>

我怎樣才能垂直居中?

將此添加到.modal-content

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

這應該使模態圖像居中。 簽入全屏視圖。

編輯:一行 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM