HTML DOM宽度+可见窗口的高度 [英] HTML DOM width + height of visible window

查看:125
本文介绍了HTML DOM宽度+可见窗口的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在浏览器中打开可用空间的当前高度和宽度。

How do I get the current height and width of the available space in the browser as it is open.

我不想要总文档的高度,只是屏幕上显示的内容。

I don't want the height of the total document, just what's visible on the screen.

推荐答案

你可以看看这个 blog post 查看方法。

You can take a look at this blog post to see the method.

简而言之,给代码

function alertSize() {
    var myWidth = 0, myHeight = 0;
    if(typeof(window.innerWidth) == 'number') {
        // Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } 
    else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        // IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } 
    else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
        // IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }

    window.alert( 'Width = ' + myWidth );
    window.alert( 'Height = ' + myHeight );
}

还需要注意的是,大多数js框架(jquery,ext,prototype )将提供一个功能(IMHO)。

also have to be noted that most of js framework (jquery, ext, prototype) would provide a function for doing that (IMHO).

在jQuery中:

$(window).width();
$(window).height();

这篇关于HTML DOM宽度+可见窗口的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆