简体   繁体   English

如何在屏幕上将不同大小的div居中

[英]How to center div with different sizes on the screen

I'm trying to make a "modal" and everything works fine except for the first time I open a new modal window, it does not appear in the center of the page. 我正在尝试制作一个“模态”,并且一切正常,但第一次打开新的模态窗口时,它不会出现在页面中央。 After I open a modal for the first time then all the windows appear centered on the screen. 第一次打开模态后,所有窗口均居中显示在屏幕上。

HTML: HTML:

<div id="modal3" class="galleryContent">
    <div class="modalheader">
        <h3>Imagen 2</h3>
        <span class="fa fa-close"></span>
    </div>
    <div class="contenidomodal">
        <p>La imagen número 1 reprensta al profesor Sebastian.</p>
        <img src="img/copy_mcrepeatsalot.png" alt="Sebastian">
    </div>
    <div class="modalfooter">
        <button type="button" class="btn btn-default">Cerrar</button>
    </div>
</div>

jQuery jQuery的

  <script> var $overlay = $('<div id="overlay"></div>') $("body").append($overlay); $(document).ready(function(){ $("#imageGallery a").click(function(event){ event.preventDefault(); var $target = $(this).attr("data-target"); var $altura = $(".galleryContent").height(); var $anchura = $(".galleryContent").width(); $(".galleryContent").css("margin-top", - $altura/2); $(".galleryContent").css("margin-left", - $anchura/2); $overlay.fadeIn(); $($target).fadeIn(); $("#overlay").click(function(){ $($target).fadeOut(); $overlay.fadeOut(); }); $(".fa-close").click(function(){ $($target).fadeOut(); $overlay.fadeOut(); }); $(".modalfooter button").click(function(){ $($target).fadeOut(); $overlay.fadeOut(); }); }); }); </script> 

the variable $altura and $anchura = height and width are inside the click function so It takes the width and height of the current object. 变量$ altura和$ anchura =高度和宽度位于click函数内部,因此它将获取当前对象的宽度和高度。

Thanks. 谢谢。

When the assets inside your modal element fully load, your element's width/height will change, therefore your calculations will be wrong. 当模态元素内的资产完全加载时,元素的宽度/高度将发生变化,因此您的计算将是错误的。

A long time ago I created something similar and had to insert a placeholder image into the modal's content. 很久以前,我创建了类似的东西,并且不得不在模式的内容中插入一个占位符图像。 Then I changed that placeholder's src attribute for every image that I wanted to show. 然后,我为每个要显示的图像更改了占位符的src属性。 However, before displaying the modal I waited for the image to load. 但是,在显示模态之前,我等待图像加载。 Here are some links to that project: 以下是该项目的一些链接:

If you are supporting only modern Browsers though, you can center a block level element only with CSS. 但是,如果仅支持现代浏览器,则只能将CSS放在块级元素的中央。 Here is an example of that: 这是一个例子:

.modal {
  position: fixed;
  left: 50%;
  top: 50%;
  -moz-transform: translate(-50%, -50%);
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}

Live example: http://jsbin.com/rawomasaqo/1/edit?css,output 实时示例: http//jsbin.com/rawomasaqo/1/edit?css,输出

My guess is when you call .height() the content is not yet rendered and sized. 我的猜测是,当您调用.height()时,内容尚未呈现和调整大小。 You'll want to do that when fadeIn is complete, or at least started. 您需要在fadeIn完成或至少开始时执行此操作。 Alternately, using margin: auto; 或者,使用margin: auto; usually works for this sort of thing. 通常适用于这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM