首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法将模式图像垂直居中

无法将模式图像垂直居中
EN

Stack Overflow用户
提问于 2021-07-06 22:39:53
回答 2查看 41关注 0票数 1

我在这个图片库工作,在点击图像缩略图后,打开一个模式,以显示全尺寸的图像。

模式工作得很好,但当图像高度较短时,图像不会显示在中心。我说的中心是指垂直方向。从水平上看,它完美地处于中心位置。

模态代码取自W3School's网站,我去掉了标题,对html和css做了一点修改。

我尝试过的:

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

代码语言:javascript
运行
复制
  top:50%; 
  left:50%; 
  transform:translate(-50%, -50%); 

我也尝试过margin: 0 auto。但似乎什么都不起作用。

以下是使用不是垂直居中的图像的模式的工作版本:

代码语言:javascript
运行
复制
// 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';
  }
}
代码语言:javascript
运行
复制
.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;
}
代码语言:javascript
运行
复制
<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>

如何使其垂直居中?

EN

回答 2

Stack Overflow用户

发布于 2021-07-06 22:45:46

将此内容添加到.modal-content

代码语言:javascript
运行
复制
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
票数 0
EN

Stack Overflow用户

发布于 2021-07-06 22:59:53

这将使模式图像居中。在全屏视图中签入。

编辑:一行JS,两行CSS。(rest是您的代码)

代码语言:javascript
运行
复制
// 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';
  }
}
代码语言:javascript
运行
复制
.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;
}
代码语言:javascript
运行
复制
<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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68272595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档