2009-12-07 78 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; 
相关问题