2011-04-24 74 views
17

我試圖根據窗口高度來設置不同的瀏覽器行爲。 我想要的是 如果用戶在上網本上,我的腳本只會激活css overflow -y爲'auto',所以如果內容比屏幕大,用戶可以看到所有內容。 如果用戶在一個大屏幕上,我想要隱藏溢出,並且只需要我的main div具有overflow ='auto',所以頁腳可以位於屏幕的底部,但是如果屏幕比屏幕更大,則內容也可以被修改。

發佈這個基本的代碼,它可以在Mac上的大屏幕,但是在Internet Explorer這不,無論是在或大或小屏幕...

怎麼辦?

感謝您的幫助提前

CSS

html, body { 
    min-width: 600px; 
    margin: 0; 
    padding: 0; 
    overflow: hidden; 
} 
#header { 
    position:relative; /* IE7 overflow bug */ 
    clear:both; 
    float:left; 
    width:100%; 
    height: 200px; 
    overflow: hidden; 
} 
#main { 
    position:relative; /* IE7 overflow bug */ 
    clear:both; 
    float:left; 
    width:100%; 
    overflow-x: hidden; 
} 
#footer { 
    position:relative; /* IE7 overflow bug */ 
    clear:both; 
    float:left; 
    width:100%; 
    height: 100px; 
    overflow: hidden; 
} 

jQuery的

if(typeof(window.innerWidth) == 'number') { 
    // No-IE 
    var screen_height = window.innerHeight; 
} else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) { 
    //IE 6 + 
    var screen_height = document.documentElement.clientHeight; 
} else if(document.body && (document.body.clientWidth || document.body.clientHeight)) { 
    //IE 4 
    var screen_height = document.body.clientHeight; 
} 

var header_height = $('#header').height(); 
var footer_height = $('#footer').height(); 
var main_height = screen_height - header_height - footer_height; 
// 
if (navigator.userAgent.toLowerCase().search("iphone") > -1 || navigator.userAgent.toLowerCase().search("ipod") > -1) { 
    $('body').css('overflow-y', 'auto'); 
} else { 
    if(screen_height > 550) { 
     $('#main').css('height', main_height + 'px'); 
$('#main').css('overflow-y', 'auto'); 
    } else { 
     $('html, body').css('overflow-y', 'auto'); 
    } 
} 
+0

IE 究竟發生了什麼或沒有發生? – 2011-04-24 18:40:59

+0

你好,在IE中溢出總是隱藏的,所以我看不到所有的內容。在IE 7,8和9測試。謝謝。 – Pluda 2011-04-24 18:45:33

+0

如果你擺脫了!重要的事情,會發生什麼? – Vervious 2011-04-24 18:47:29

回答

35
$('html, body').css('overflowY', 'auto'); 

解決了這個問題。

+0

短跑發生了什麼:是故意省略,還是一個錯字? – SamB 2011-05-04 21:03:28

+1

那就是IE – 2011-05-26 13:55:18

8

嘗試用overflowY設置它:在屬性名TY

$('body').css('overflowY', 'auto'); 

破折號使用JavaScript進行設置時,特別是在IE中無法工作。

希望工程。

+0

只是增加$('html')的作品就像一個魅力,我已經更新了未來的搜索者的初始代碼,但沒有找到問題解決的地方:-( – Pluda 2011-04-25 13:40:45

+0

很高興它的工作!應該有一個鏈接接受答案作爲正確的權利 – clmarquart 2011-04-25 13:43:42

相關問題