javascript - 如何在模态中垂直居中图像?

标签 javascript html css modal-dialog

我有一个显示图像的模式。我希望手机上的图像能够在中间(垂直)居中,而不拉伸(stretch)它或任何东西,但无法实现。

这是我的模式(使用显示此模式的 JavaScript):

var modal = document.getElementById('imgmodal');
var img = document.getElementById('picture');
var modalImg = document.getElementById("img");
var download = document.getElementById('download-link');

img.onclick = function(){
  modal.style.display = "block";
  modalImg.src = this.src;
  download.href = this.src;
}

var span = document.getElementById("close");
span.onclick = function() { 
  modal.style.display = "none";
}
/* exam_img */
#imgmodal {
  display: none; 
  position: fixed; 
  z-index: 1; 
  padding-top: 80px; 
  left: 0;
  top: 0;
  width: 100%;
  height: 100%; /* Full height */
  background-color: rgb(0,0,0); /* Fallback color */
  overflow: auto;
  background-color: rgb(0,0,0); /* Black w/ opacity */
  transition: 0.3s;
}

/* Modal Content (image) */
.content {
  margin: auto;
  display: block;
  height: 90%;
}

/* Add Animation */
.content, #caption {    
  -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 {transform:scale(0)} 
  to {transform:scale(1)}
}

/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 768px){
  .content { /* I want image to be vertically centered on smaller screens)*/
    width: 100%;
    max-height: 90%;
    height: auto;
  }
  #close {
    top: 2px;
    font-size: 40px;
  }
  #imgmodal {
    padding-top: 50px;
  }
  #caption {
    top: -1px;
  }
}
<li class="exam_li">
  <img id="picture" src="https://www.w3schools.com/css/img_fjords.jpg" alt="Slika Testa" width="60" height="60" class="img-resposive exam-img">
</li>

<div id="imgmodal">
  <div id="caption">
    <a id="download-link" download>
      <span class="glyphicon glyphicon-download"></span>
      <a id="download-link" download></a>
   </div>
  <span id="close">&times;</span>
  <img class="content" id="img">
</div>

提前谢谢您! “工作”代码在这里:https://jsfiddle.net/dccLtfeh/

最佳答案

You need to add a css logic for the image to be vertically align on mobile device.

发布顶部翻译将执行逻辑Postion:relativetop:50% 一起使用,这将使图像从顶部移动 50%,然后 transform:translateY (-50%);将其向上移动 25%,使其垂直居中。

@media only screen and (max-width: 768px) {
  .content {
    width: 100%;
    height: auto;
    position: relative;
    top: calc(50% - 25px);
    top: -moz-calc(50% - 25px);
    top: -webkit-calc(50% - 25px);
    transform: translateY(-50%);
    -webkit-transform: translateY(-50%);
    -moz-transform: translateY(-50%);
  }
}

Here is the working code comaptible with mobile device. Check this on mobile device.

var modal = document.getElementById('imgmodal');
var img = document.getElementById('picture');
var modalImg = document.getElementById("img");
var download = document.getElementById('download-link');
img.onclick = function() {
  modal.style.display = "block";
  modalImg.src = this.src;
  download.href = this.src;
}
var span = document.getElementById("close");
span.onclick = function() {
  modal.style.display = "none";
}
.exam-img:hover {
  cursor: pointer;
  transition: 0.2s;
  overflow: hidden;
}


/* exam_img */

#imgmodal {
  display: none;
  position: fixed;
  z-index: 1;
  padding-top: 80px;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  /* Full height */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  overflow: auto;
  background-color: rgb(0, 0, 0);
  /* Black w/ opacity */
  transition: 0.3s;
}


/* Modal Content (image) */

.content {
  margin: auto;
  display: block;
  height: 90%;
}


/* Caption of Modal Image */

#caption {
  text-align: center;
  color: #ccc;
  position: absolute;
  top: 15px;
  left: 35px;
  font-size: 40px;
  margin-top: 0;
}


/* Add Animation */

.content,
#caption {
  -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 {
    transform: scale(0)
  }
  to {
    transform: scale(1)
  }
}


/* The Close Button */

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

#close:hover,
#close:focus {
  color: #989898;
  text-decoration: none;
  cursor: pointer;
}

#download-link {
  font-size: 25px;
  color: #f1f1f1;
  font-weight: normal;
  text-decoration: none;
  transition: 0.2s;
  padding: 10px;
}

#download-link:hover {
  color: #989898;
}


/* 100% Image Width on Smaller Screens */

@media only screen and (max-width: 768px) {
  .content {
    width: 100%;
    height: auto;
    position: relative;
    top: calc(50% - 25px);
    top: -moz-calc(50% - 25px);
    top: -webkit-calc(50% - 25px);
    transform: translateY(-50%);
    -webkit-transform: translateY(-50%);
    -moz-transform: translateY(-50%);
  }
  #close {
    top: 2px;
    font-size: 40px;
  }
  #imgmodal {
    padding-top: 50px;
  }
  #caption {
    top: -1px;
  }
  
  @-webkit-keyframes zoom {
  from {
    -webkit-transform: scale(0) translateY(-50%);
  }
  to {
    -webkit-transform: scale(1) translateY(-50%);
  }
}

@keyframes zoom {
  from {
    transform: scale(0) translateY(-50%); 
  }
  to {
    transform: scale(1) translateY(-50%);
  }
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="exam_li"><img id="picture" src="https://www.w3schools.com/css/img_fjords.jpg" alt="Slika Testa" width="60" height="60" class="img-resposive exam-img"></li>

<div id="imgmodal">
  <div id="caption"><a id="download-link" download><span class="glyphicon glyphicon-download"></span><a id="download-link" download></a></div>
  <span id="close">&times;</span>
  <img class="content" id="img">

关于javascript - 如何在模态中垂直居中图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43654510/

相关文章:

javascript - RequireJS:如何控制并行加载的js脚本的执行顺序

javascript - 要求不在 vue.js 中工作

javascript - Html 默认选择选项 - 不同的颜色 - 不适用于当前的 Jquery

javascript - 在不使用过渡的情况下将html div的高度更改为顶部

css - dt(向左浮动)和第一个 dd(向右浮动)在同一行

javascript - <IMG> 不保持内联 - Javascript

javascript - 仅隐藏和显示直接子项的第 n 项之后的 div 列表项

javascript - Angular 休息调用未定义响应对象属性

html - 中间框可滚动的垂直布局(不是位置 : absolute)

html - 为什么我的日历图标没有出现在表格单元格中?