jquery - 如果调整高度,模式弹出窗口将适合浏览器 View

标签 jquery popup resize modal-dialog

我已经阅读了本教程来制作模式弹出窗口,它似乎非常适合我需要做的事情。但是,我想知道是否有人可以帮助我弄清楚我需要计算什么来更改对话框,以便每当您调整浏览器大小时它都会跟随。本教程很好,因为如果您调整浏览器的大小(宽度方向),该框将跟随并进入中间。但如果是高度的话,就不会了。

教程:Here

我认为这与这些代码有关:

    // get the screen height and width  
var maskHeight = $(document).height();  
var maskWidth = $(window).width();

// calculate the values for center alignment
var dialogTop =  (maskHeight/3) - ($('#dialog-box').height()/3);  
var dialogLeft = (maskWidth/2) - ($('#dialog-box').width()/2); 

// assign values to the overlay and dialog box
$('#dialog-overlay').css({height:maskHeight, width:maskWidth}).show();
$('#dialog-box').css({top:dialogTop, left:dialogLeft}).show();

我尝试使用 $(window).height() 但我认为这也不起作用。我试图模仿宽度样式,因为它可以工作,但它似乎不适用于高度?有人可以帮我解决这个问题吗?

谢谢!

最佳答案

试试这个

工作 demo 。您可以调整窗口大小并尝试将对话框自动重新定位到中心。

$(function(){
    $(window).resize(function(){
    // get the screen height and width  
    var maskHeight = $(window).height();  
    var maskWidth = $(window).width();

    // calculate the values for center alignment
    var dialogTop =  (maskHeight  - $('#dialog-box').height())/2;  
    var dialogLeft = (maskWidth - $('#dialog-box').width())/2; 

    // assign values to the overlay and dialog box
    $('#dialog-overlay').css({ height:$(document).height(), width:$(document).width() }).show();
    $('#dialog-box').css({ top: dialogTop, left: dialogLeft, position:"fixed"}).show();
    }).resize();
});

关于jquery - 如果调整高度,模式弹出窗口将适合浏览器 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7143333/

相关文章:

jquery - 遍历父元素,如果子元素不存在则显示文本

javascript - MasterPage ASP.Net 和 Jquery 关于 css 类的点击问题

c# - 允许鼠标事件通过半透明的 Popup

javascript - 单击弹出图像和生物框

css - 动态字体大小 Flex 4 可在调整窗口/面板大小时调整大小

jquery :gt() inclusive

javascript - 如果 TD 中存在某种样式,则将 CSS 应用于 TR

c++ - 您无权写入文件 “ostream” 所在的文件夹

java - 调整 JComponent 的大小以进行文件导出

jquery - 使用 jquery 更改图像尺寸