为什么我的 div 没有正确居中?

发布于 2024-08-27 02:53:10 字数 1462 浏览 5 评论 0原文

我使用 $.blockUI 和 jQuery 来显示模式对话框。 HTML 看起来像:

<div id="progressDialogue" class="mp_modalpopup">
    <div class="mp_container">
        <div class="mp_header">
            <span id="pd_header_text" class="mp_msg">Please wait..</span>
        </div>
        <div class="mp_body">
            <img src="ajax-loader.gif" style="text-align:center" alt="loading" />
        </div>
    </div>
</div>

CSS 看起来像:

.mp_modalpopup
{
    font-family: arial,helvetica,clean,sans-serif;
    font-size: small;
    padding: 2px 3px;
    bottom: 50%;
    right: 50%;
    position: absolute;
    width: 400px;
    z-index:999;    
}

.mp_container
{
    width: 400px;
    border: solid 1px #808080;
    border-width: 1px 1px;
    left: 50%;
    position: relative;
    top: 50%;
}

/* removed mp_header, mp_body, mp_msg CSS for brevity */

这将愉快地在页面中心的其他 HTML 之上呈现出“smack bang”。

但是,如果我在 .mp_modalpopup CSS 类中设置 display:none ,然后使用 $.blockUI 使其可见,则在 IE 8 中,对话会垂直居中,但与左对齐一半的对话离开页面,在 Google Chrome 和 Firefox 中,对话根本不可见(但 blockUI 正在工作,因为页面呈灰色)。

这是 blockUI javascript:

$.blockUI.defaults.css = {};

$.blockUI({ 
    message: $('#progressDialogue'), 
    overlayCSS: { backgroundColor: '#000', opacity: 0.1 },
    css: {backgroundColor: '#00f', color: '#100'} 
});

为什么会发生这种情况?

I'm using $.blockUI with jQuery to show a modal dialogue. The HTML looks like:

<div id="progressDialogue" class="mp_modalpopup">
    <div class="mp_container">
        <div class="mp_header">
            <span id="pd_header_text" class="mp_msg">Please wait..</span>
        </div>
        <div class="mp_body">
            <img src="ajax-loader.gif" style="text-align:center" alt="loading" />
        </div>
    </div>
</div>

The CSS looks like:

.mp_modalpopup
{
    font-family: arial,helvetica,clean,sans-serif;
    font-size: small;
    padding: 2px 3px;
    bottom: 50%;
    right: 50%;
    position: absolute;
    width: 400px;
    z-index:999;    
}

.mp_container
{
    width: 400px;
    border: solid 1px #808080;
    border-width: 1px 1px;
    left: 50%;
    position: relative;
    top: 50%;
}

/* removed mp_header, mp_body, mp_msg CSS for brevity */

This will happily render smack bang in the center of the page on top of other HTML.

However if I set display:none in the .mp_modalpopup CSS class and then use $.blockUI to make it visible, in IE 8 the dialogue centers itself vertically but aligns left with half of the dialogue off the page and in Google Chrome and Firefox the dialogue is not visible at all (but blockUI is working because the page greys out).

This is the blockUI javascript:

$.blockUI.defaults.css = {};

$.blockUI({ 
    message: $('#progressDialogue'), 
    overlayCSS: { backgroundColor: '#000', opacity: 0.1 },
    css: {backgroundColor: '#00f', color: '#100'} 
});

Why is this happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南风几经秋 2024-09-03 02:53:10

发生这种情况是因为 blockUI 也试图将您的

居中。如果你从 CSS 中删除所有定位,它应该可以工作。

删除这个:

bottom: 50%;
right: 50%;
position: absolute;

还有这个

left: 50%;
position: relative;
top: 50%;

或者,您可以禁用 blockUI 居中,如下所示:

$.blockUI({ 
    centerX: false, 
    centerY: false, 
    message: $('#progressDialogue'), 
    overlayCSS: { backgroundColor: '#000', opacity: 0.1 },
    css: {backgroundColor: '#00f', color: '#100'} 
});

This is happening because blockUI is also trying to center your <div>. If you remove all positioning from your CSS, it should work.

Remove this:

bottom: 50%;
right: 50%;
position: absolute;

And this

left: 50%;
position: relative;
top: 50%;

Alternatively, you could disable the blockUI centering like this:

$.blockUI({ 
    centerX: false, 
    centerY: false, 
    message: $('#progressDialogue'), 
    overlayCSS: { backgroundColor: '#000', opacity: 0.1 },
    css: {backgroundColor: '#00f', color: '#100'} 
});
折戟 2024-09-03 02:53:10

实际上我刚刚找到了一个非常简单的解决方案。

如果您阻止一个元素而不是整个页面,blockUI 将为您提供使用 centerX 和 centerY 的选项。

我屏蔽了 html 元素,仅此而已。我的 div 水平和垂直居中。当然,我不希望它垂直居中,所以我将 centerY 设置为 false,并在 CSS 中将其设置为 top: '5%',如下所示。还要确保在 blockUI css 中设置元素的宽度。

$('html').block({ message: $('#layOver'),
    centerX: true,
    centerY: false,
    css: {
            top: '5%',
            width: '720px',
            height: 'auto',
            cursor: 'null',
            border: 'none',
            textalign: 'center',
            backgroundColor: 'auto', 
    }, 
    overlayCSS:  { 
            opacity: 0.4 ,
            cursor: 'null',
    } 
});

Actually I just found a very easy solution.

If you block an element instead of the entire page, blockUI gives you the options to use centerX and centerY.

I blocked the element html and that was it. My div was horizontally and vertically centered. Ofourse I didn't want it vertically centered, so I set centerY to false, and in the CSS set it to top: '5%', as below. Also make sure you set the width of the element in the blockUI css.

$('html').block({ message: $('#layOver'),
    centerX: true,
    centerY: false,
    css: {
            top: '5%',
            width: '720px',
            height: 'auto',
            cursor: 'null',
            border: 'none',
            textalign: 'center',
            backgroundColor: 'auto', 
    }, 
    overlayCSS:  { 
            opacity: 0.4 ,
            cursor: 'null',
    } 
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文