首页  ·  知识 ·  前端
获取窗口高、宽的JS代码
QLeelulu  http://www.cnblogs.com/QLeelulu/  综合  编辑:dezai  图片来源:网络
"COLOR: #0000ff">var getViewPortWidth = "COLOR: #0000ff">function() { "COLOR: #0000ff">v
var getViewPortWidth = function()
{
    var width = 0;

    if ((document.documentElement) && (document.documentElement.clientWidth))
    {
        width = document.documentElement.clientWidth;
    }
    else if ((document.body) && (document.body.clientWidth))
    {
        width = document.body.clientWidth;
    }
    else if (window.innerWidth)
    {
        width = window.innerWidth;
    }

    return width;
};

var getViewPortHeight = function()
{
    var height = 0;

    if (window.innerHeight)
    {
        height = window.innerHeight - 18;
    }
    else if ((document.documentElement) && (document.documentElement.clientHeight))
    {
        height = document.documentElement.clientHeight;
    }

    return height;
};

var getContentHeight = function()
{
    if ((document.body) && (document.body.offsetHeight))
    {
        return document.body.offsetHeight;
    }

    return 0;
};

var getViewPortScrollX = function()
{
    var scrollX = 0;

    if ((document.documentElement) && (document.documentElement.scrollLeft))
    {
        scrollX = document.documentElement.scrollLeft;
    }
    else if ((document.body) && (document.body.scrollLeft))
    {
        scrollX = document.body.scrollLeft;
    }
    else if (window.pageXOffset)
    {
        scrollX = window.pageXOffset;
    }
    else if (window.scrollX)
    {
        scrollX = window.scrollX;
    }

    return scrollX;
};

var getViewPortScrollY = function()
{
    var scrollY = 0;

    if ((document.documentElement) && (document.documentElement.scrollTop))
    {
        scrollY = document.documentElement.scrollTop;
    }
    else if ((document.body) && (document.body.scrollTop))
    {
        scrollY = document.body.scrollTop;
    }
    else if (window.pageYOffset)
    {
        scrollY = window.pageYOffset;
    }
    else if (window.scrollY)
    {
        scrollY = window.scrollY;
    }

    return scrollY;
};
本文作者:QLeelulu 来源:http://www.cnblogs.com/QLeelulu/
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读