2009-12-07 80 views
5

如何在瀏覽器窗口中找出視點的寬度和高度?以及如何找出多少文件滾動到下和向右?JavaScript:如何在瀏覽器窗口中查看視點的寬度和高度?

+0

和如何找出多少文件滾動到下降和權利? – xXx 2009-12-07 15:05:00

+0

嘗試閱讀以下任何一個:http://stackoverflow.com/questions/871399/cross-browser-method-for-detecting-the-scrolltop-of-the-browser-window http://stackoverflow.com/questions/1248081/get-the-browser-viewport-dimensions-with-javascript http://stackoverflow.com/questions/817446/good-way-to-estimate-available-browser-area – random 2009-12-07 15:06:46

回答

7

試試這個功能......並調用它當需要時:)

function getViewPortSize() 
{ 
    var viewportwidth; 
    var viewportheight; 

    //Standards compliant browsers (mozilla/netscape/opera/IE7) 
    if (typeof window.innerWidth != 'undefined') 
    { 
     viewportwidth = window.innerWidth, 
     viewportheight = window.innerHeight 
    } 

    // IE6 
    else if (typeof document.documentElement != 'undefined' 
    && typeof document.documentElement.clientWidth != 
    'undefined' && document.documentElement.clientWidth != 0) 
    { 
     viewportwidth = document.documentElement.clientWidth, 
     viewportheight = document.documentElement.clientHeight 
    } 

    //Older IE 
    else 
    { 
     viewportwidth = document.getElementsByTagName('body')[0].clientWidth, 
     viewportheight = document.getElementsByTagName('body')[0].clientHeight 
    } 

    return viewportwidth + "~" + viewportheight; 
} 
+0

沒有真正回答後續問題,問題雖然... – 2009-12-08 08:08:14

0
height = document.body.clientHeight; 
width = document.body.clientWidth; 

關於滾動位置,我不知道是否有確定,但本應在大多數瀏覽器工作的標準方式:

scrolled = document.body.scrollTop; 
相關問題