JavaScript:如何在浏览器窗口中找出视点的宽度和高度?

JavaScript:如何在浏览器窗口中找出视点的宽度和高度?

问题描述:

如何在浏览器窗口中找出视点的宽度和高度?以及如何找出有多少文件上下滚动?

How to find out width and height of viewpoint in browser window? And How to find out how much document scrolled to down and to right?

尝试使用此函数...,并在需要时调用它:)

Try this function... and call it when needed :)

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;
}