如何在屏幕上保留jQuery对话框?

时间:2009-10-19 18:22:27

标签: jquery jquery-ui-dialog

我不希望对话框在屏幕上居中,所以我设置了框的顶部和左侧坐标。我将它定位,使其显示在链接旁边,并且在点击之前最初不会打开。

$("#error").dialog({
    bgiframe: true,
    autoOpen: false,
    width: 'auto',
    height: 'auto',
    hide: 'slide',
    show: 'clip'
});

<div id="error" title="Error">
<div id="errorText">&nbsp;</div>
</div>

从这里我想在屏幕上显示错误信息。例如,如果我在页面的底部,我不希望用户必须向下滚动才能看到对话框。同样的事情,如果错误消息一直在右边,我想在被点击的元素的左侧显示它。唯一的问题,因为我有自动宽度和高度,它似乎不知道div的高度/宽度,然后我显示对话框;使用$('#error')。height()或$('#error')。width()。

$("#errorText").html(request.responseText + '<p>(Esc or click to close)</p>');
var x = el.position().left + el.outerWidth();
var y = el.position().top - $(document).scrollTop();
var position = el.position();
var bottomOfDialog = position.top + heightOfTheDialog;
if(bottomOfDialog > document.height)
{
     y -= heightOfTheDialog;
}
var rightSideOfDialog = position.left + widthOfTheDialog;
if(rightSideOfDialog > document.width)
{
    x -= (widthOfTheDialog + el.outerWidth());
}               
$("#error").dialog('option', 'position', [x, y]).dialog('open');

如何在打开实际对话框之前获得正确的heightOfTheDialog和widthOfTheDialog?或者我应该使用其他东西吗?

1 个答案:

答案 0 :(得分:4)

如果您的目标是向用户显示一条消息并让他们不必滚动发现它 - 我相信这是基于您的描述的情况 - 您可能需要考虑使用jQuery BlockUI Plugin来告诉用户用户错误消息。查看以下演示:

http://malsup.com/jquery/block/#demos

我认为咆哮功能可能是你可以使用的东西。

这里的主要好处是错误信息会出现在一个非常一致的地方,你不需要关心“违规”元素的确切位置,但可以 - 同时 - 放心,错误消息将始终对用户可见。