2012-10-17 42 views
13

我使用的是Twitter Bootstrap's modal,但我必須將其位置更改爲絕對位置,因爲我需要它們能夠滾動小屏幕的原因。不幸的是,有了這個,模態開放到了站點的固定位置,而不是在查看區域。 有什麼辦法打開他們(並能夠滾動)到我目前正在查看的屏幕?將模態圖層設置爲查看屏幕的中心

Picture of my problem

CSS:

.modal { 
    width: 845px; 
    margin: -250px 0 0 -430px; 
    background-color: #f6f5ed; 
    position: absolute; 
    border: 1px solid #cdc4b2; 
    top: 50%; 
    left: 50%; 
} 

回答

14

更多細節此主題:Modal plugin of Bootstrap 2 is not displayed by the center具有正確的答案,在這裏張貼的:

$('#YourModal').modal().css(
      { 
       'margin-top': function() { 
        return window.pageYOffset-($(this).height()/2); 
       } 
      }); 
+0

謝謝,這有幫助! :) – Stefanie

+0

這確實有幫助,但是現在,模式一直沿着非移動網站的文檔向我發送。 – mbrochh

+0

看到我剛剛給出的答案,我從css只維護 – Innodel

4

從@steve的解決方案並沒有爲我,儘管我的工作問題被等同於@斯蒂芬妮。

重申的是斯蒂芬妮和我分享問題:

我有很長的引導模態。在小屏幕上,如果沒有滾動,模式將無法完整顯示。默認情況下,引導模式的位置是:固定的。我可以通過將其更改爲position:absolute來讓它們滾動。

但現在我有一個新問題。我的頁面也很長,當我從頁面的底部中觸發模式時,該模式出現在當前瀏覽器視圖之外的頁面的頂部處。

因此,要解決這兩個:

CSS:

// allows the modal to be scrolled 
.modal {position: absolute;} 

的JavaScript/jQuery的:

// jumps browser window to the top when modal is fired 
$('body').on('show', '.modal', function(){ 
    $('body').scrollTop(0); 
}); 

但事實證明,Firefox不喜歡 '身體' 的選擇爲scrollTop,但確實喜歡'HTML'。 Webkit反過來操作。從這裏使用註釋:Animate scrollTop not working in firefox

由於我使用jQuery < 1.9我可以做瀏覽器嗅探解決這個問題:

// jumps browser window to the top when modal is fired 
$('body').on('show', '.modal', function(){ 
    // Firefox wants 'html', others want 'body' 
    $(jQuery.browser.mozilla ? "html" : "body").scrollTop(0); 
}); 

現在,當模態被解僱,他們出現在頁面的頂部,瀏覽器窗口會向上滾動以顯示它們,但如果用戶的屏幕太小而無法顯示模式的整個長度,用戶仍然可以向下滾動。

+0

我知道我遲到了黨,但這是真正的正確的解決方案對於所述的問題。謝謝你。 – max7

0

不得不改變絕對定位,所以它會滾動手機。

這爲我工作,以及(由ALLINONE溶液):

// jumps browser window to the top when modal is fired 
$('body').on('show', '.modal', function(){ 
    // Firefox wants 'html', others want 'body' 
    $(jQuery.browser.mozilla ? "html" : "body").scrollTop(0); 
}); 
1

有點晚了比賽,但是這是修復我現在有到位和工作在Firefox,Chrome和IE。

$('body').on('show', '.modal', function() { 
$(this).css({ 'margin-top': window.pageYOffset - $(this).height()/2, 'top': '50%' }); 
$(this).css({ 'margin-left': window.pageXOffset - $(this).width()/2, 'left': '50%' }); 
}); 
0

的問題是CSS - 你的「身體」 &「HTML」代碼設置爲「高度:100%」