2015-07-10 63 views
2

我遇到問題。如果頁面真的很長......如何讓彈出框出現在可見區域內?目前它顯示在頁面中間,並且滾動跳轉到頁面頂部?彈出窗口的位置(視圖中心!不是頁面的中心?)

我希望彈出窗口出現在滾動(在視圖中)的當前位置,甚至不改變當前的滾動位置。向下滾動以查看鏈接和測試。

非常感謝。

代碼:http://jsfiddle.net/gyeo03nk/6/

的Javascript

$(document).ready(function() { 

// if user clicked on button, the overlay layer or the dialogbox, close the dialog 
$('a.btn-ok, #dialog-overlay, #dialog-box').click(function() {  
    $('#dialog-overlay, #dialog-box').hide();  
    return false; 
}); 

// if user resize the window, call the same function again 
// to make sure the overlay fills the screen and dialogbox aligned to center  
$(window).resize(function() { 

    //only do it if the dialog box is not hidden 
    if (!$('#dialog-box').is(':hidden')) popup();  
}); 


}); 

//Popup dialog 
function popup(message) { 

// 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()); 
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(); 

// display the message 
$('#dialog-message').html(message); 

} 

CSS:

/* Popup window ----------------------------------------*/ 

#dialog-overlay { 

    /* set it to fill the whil screen */ 
    width:100%; 
    height:100%; 

    /* transparency for different browsers */ 
    filter:alpha(opacity=50); 
    -moz-opacity:0.5; 
    -khtml-opacity: 0.5; 
    opacity: 0.5; 
    background:#000; 

    /* make sure it appear behind the dialog box but above everything else */ 
    position:absolute; 
    top:0; left:0; 
    z-index:3000; 

    /* hide it by default */ 
    display:none; 
} 


#dialog-box { 

    /* css3 drop shadow */ 
    -webkit-box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5); 
    -moz-box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5); 

    /* css3 border radius */ 
    -moz-border-radius: 15px; 
    -webkit-border-radius: 15px; 

    background:#eee; 
    /* styling of the dialog box, i have a fixed dimension for this demo */ 
    width:328px; 

    /* make sure it has the highest z-index */ 
    position:absolute; 
    z-index:5000; 

    /* hide it by default */ 
    display:none; 
} 

#dialog-box .dialog-content { 
    /* style the content */ 
    text-align:left; 
    padding:10px; 
    margin:13px; 
    color:#666; 

} 


/* extra styling */ 
#dialog-box .dialog-content p { 
    font-weight:700; margin:0; 
} 

#dialog-box .dialog-content ul { 
    margin:10px 0 10px 20px; 
    padding:0; 
    height:50px; 
} 

HTML:

<div id="dialog-overlay"></div> 
<div id="dialog-box"> 
    <div class="dialog-content"> 
     <div id="dialog-message"></div> 
     <a href="#" class="button-small">close</a> 
    </div> 
</div> 

Scroll to end to see href, thanks. 

test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
test<br> 
<a href="#" onclick="return popup('Please Log in');">This launches the popup, but the popup appears in the middle of the page, and not in view (at the current scroll)..</a> 
+1

你有沒有試過改變diaglouge over lay到'postion:fixed;'?我不確定這是否是您的解決方案,但可能會有所幫助。 – Becky

+0

感謝貝基,這似乎是做到這一點的方法。 –

回答

4

使用

position: 'fixed' 

使窗口的當前視圖上的彈出窗格。

同時將href='#'更改爲href或將其完全刪除並創建CSS鏈接,因爲這是滾動到頁面頂部的內容。

THIS在滾動條的當前位置打開它。

THIS顯示瞭如何在頁面頂部打開它。

+0

謝謝斯托揚,任何使第一個例子(在滾動條的當前位置打開它)顯示爲可點擊鏈接(藍色)的方法..? –

+1

[demo](http://jsfiddle.net/gyeo03nk/19/)。請接受答案,如果它解決了你的問題=)快樂編碼! –

+0

謝謝斯托揚! –

4

正如我的評論。使用:

position: fixed; 

爲您的對話結束。這應該適合你。