2013-07-10 38 views
0

我已經在這個網站的彈出窗口居中,但是,它只在x軸上。我也需要將它集中在y軸上,但我似乎無法實現。試圖將JS彈出窗口放置在頁面中心?

這裏是JavaScript的...

function toggle(div_id) { 
    var el = document.getElementById(div_id); 
    if (el.style.display == 'none') { el.style.display = 'block';} 
    else {el.style.display = 'none';} 
} 

function blanket_size(popUpDivVar) { 
    if (typeof window.innerWidth != 'undefined') { 
     viewportheight = window.innerHeight; 
    } else { 
     viewportheight = document.documentElement.clientHeight; 
    } 
    if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) { 
     blanket_height = viewportheight; 
    } else { 
     if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) { 
      blanket_height = document.body.parentNode.clientHeight; 
     } else { 
      blanket_height = document.body.parentNode.scrollHeight; 
     } 
    } 
    var blanket = document.getElementById('blanket'); 
    blanket.style.height = blanket_height + 'px'; 
    var popUpDiv = document.getElementById(popUpDivVar); 
    popUpDiv_height=blanket_height/2-200;//200 is half popup's height 
    popUpDiv.style.top = popUpDiv_height + 'px'; 
} 
function window_pos(popUpDivVar) { 
    if (typeof window.innerWidth != 'undefined') { 
     viewportwidth = window.innerHeight; 
    } else { 
     viewportwidth = document.documentElement.clientHeight; 
    } 
    if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) { 
     window_width = viewportwidth; 
    } else { 
     if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) { 
      window_width = document.body.parentNode.clientWidth; 
     } else { 
      window_width = document.body.parentNode.scrollWidth; 
     } 
    } 
    var popUpDiv = document.getElementById(popUpDivVar); 
    window_width=window_width/2-200;//200 is half popup's width 
    popUpDiv.style.left = window_width + 'px'; 
} 
function popup(windowname) { 
    blanket_size(windowname); 
    window_pos(windowname); 
    toggle('blanket'); 
    toggle(windowname);  

} 

我認爲這將這項工作...

popUpDiv_height=blanket_height/2-200;//200 is half popup's height 

,但事實並非如此。有想法該怎麼解決這個嗎?

編輯:這是鏈接到站點here

這裏是歸因於這個彈出所有的CSS ...

#blanket { 
    background-color:#111; 
    opacity: 0.65; 
    filter: alpha(opacity=65) 
    *background:none; 
    position:absolute; 
    z-index: 9001; 
    top:0px; 
    left:0px; 
    width:100%; 
} 

#popUpDiv { 
    position:absolute; 
    background-image:url(../images/popup_bg.png); 
    background-repeat:no-repeat;  
    top:50%; 
    left: 50%; 
    width:400px; 
    height:400px; 
    margin-top: -120px auto 0 auto; /* auto, centers horizontally and -120px is half your height to finish the centering vertically */ 
    border:5px solid #000; 
    z-index: 9002; 
} 


#popUpDiv .close { 
    background-image: url(../images/x.png); 
    background-repeat: no-repeat; 
    position:absolute; 
    top:10px; 
    right:10px 
} 

回答

0

你可以像這樣用CSS實現這一點:

HTML

<div id="popUpDiv"> 
    Your content here 
</div> 

CSS

#popUpDiv { 
    position:absolute; 
    background-image:url(../images/popup_bg.png); 
    background-repeat:no-repeat;  
    top:50%; 
    width:400px; 
    height:400px; 
    margin: -200px auto 0 auto; /* auto, centers horizontally and -120px is half your height to finish the centering vertically */ 
    border:5px solid #000; 
    z-index: 9002; 
} 

總之,如果您使用此方法,您可能只需要使用javascript來動態設置高度和邊距頂部值,具體取決於您當前的div及其內容。

更多的信息在這裏:http://blog.themeforest.net/tutorials/vertical-centering-with-css/

+0

這並沒有很好的工作。我已經設置了#blanket樣式,但這不適用於#blanket。這是Popup本身。看到我上面的編輯... – webfrogs

+0

OH!將您的頁邊距編輯爲您的彈出窗口高度的一半,所以'margin-top:-200px',然後刪除左邊:50%。或者如果你保持'left:50%;''你必須做更多的事情'邊距:-200px auto 0 -200px;' – jrthib

+0

我編輯我的例子來反映你的CSS – jrthib