2011-08-22 53 views
1

我已經去這個教程來做一個模式彈出窗口,它似乎是我需要做的很好。然而,我想知道如果有人能幫我弄清楚我需要計算什麼來改變對話框,所以無論你什麼時候調整瀏覽器的大小。本教程是很好的,因爲如果您調整瀏覽器的大小(寬度方向),框會跟着進入中間。但是,如果它是高度明智的,它不會。模態彈出窗口,以適應瀏覽器視圖,如果調整大小高度

教程: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(); 

我試着使用$(窗口).height(),但我不認爲那也可以。我試圖模仿寬度樣式,因爲那個樣式可行,但它似乎不適用於高度?有人可以幫我弄這個嗎?

謝謝!

回答

3

試試這個

工作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(); 
}); 
+0

我遇到的問題是彈出窗口只顯示窗口屏幕分辨率,而不是瀏覽器窗口屏幕。如果我的網頁很長,並且底部有鏈接。彈出窗口將是頁面頂部窗口屏幕的一半(基於計算)。我試圖讓它彈出我的當前瀏覽器視圖。 – hellomello

+0

看看這個小提琴http://jsfiddle.net/tF8TU/2/我現在修改了它。還修改了我的答案。 – ShankarSangoli

+0

現在,如果您滾動覆蓋層的高度覆蓋整個文檔,並且該框始終停留在中心。 – ShankarSangoli

相關問題