【问题标题】:JavaScript: How to find out width and height of viewpoint in browser window?JavaScript:如何在浏览器窗口中找出视点的宽度和高度?
【发布时间】:2009-12-07 15:03:30
【问题描述】:

如何在浏览器窗口中找出视点的宽度和高度?以及如何找出向下和向右滚动了多少文档?

【问题讨论】:

标签: javascript


【解决方案1】:

试试这个函数...并在需要时调用它:)

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

【讨论】:

    【解决方案2】:
    height = document.body.clientHeight;
    width = document.body.clientWidth;
    

    关于滚动位置,我不确定是否有标准的方法来确定,但这应该适用于大多数浏览器:

    scrolled = document.body.scrollTop;
    

    【讨论】:

      猜你喜欢
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 2014-05-20
      • 1970-01-01
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多