简体   繁体   English

如何垂直居中jQuery Modal?

[英]How to vertically center jQuery Modal?

jQuery code: jQuery代码:

function thumb(id,ths) {
    if (<?=$loggedin?>) {
        $.post(base_url+"index.php/myad/addthumbs", {uniqueid:id});
        $(ths).addClass("red");
    } else {
        _ths=$(ths);
        var number = Math.floor(Math.random()*90000) + 10000;
        $("#captcha").attr("data-id",id);
        $("#captcha").text(number);
        $("#pop").modal("show");
    }
}

How can I center modal? 我怎样才能使模态居中? Please help me and thanks in advance. 请帮助我,在此先感谢。

I find solution on google and on stackoverflow but question is asked for bootstrap based modal when it build by a pure jquery. 我在google和stackoverflow上找到了解决方案,但是当它是由纯jquery构建时,会询问基于引导程序的模式。

One method is you can adjust the percentage of margin-top style 一种方法是您可以调整页边距样式的百分比

.modal-dialog {
    margin-top:10%;  /* Based on your modal height adjust the percentage */
}

OR 要么

.modal-dialog {
    padding-top:10%; /* Based on your modal height adjust the percentage */
}

Give your popup a fixed css like this: 给您的弹出窗口一个固定的CSS,如下所示:

#pop {
  position:fixed;
  top:50%;
  left:50%;
}

Then align it by it´s width and height in your JS: 然后根据它在JS中的宽度和高度对齐它:

$("#pop").css({
  "margin-top":-($(this).height()/2),
  "margin-left":-($(this).width()/2)
});

Without using jQuery, you can simply use display: table to the main content container together with margin: auto . 无需使用jQuery,您可以简单地将display: tablemargin: auto一起用于主要内容容器。

A working example of this centered modal is here . 这里是该中心模态的一个工作示例。

Basically, these are the important rules: 基本上,这些是重要的规则:

.modal-content {
    display: table;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    max-width: 60%; /* here you can also use a fixed width */
    overflow: auto;
    z-index: 50;
}

Use JavaScript or jQuery to trigger the opening and closing of the modal. 使用JavaScript或jQuery触发模式的打开和关闭。

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

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