如何使用 JavaScript 获取页面,窗口的高度

如何使用 JavaScript 获取页面,窗口的高度,第1张

javascript里面documentbody的clientWidth、clientHeight、offsetWidth  、offsetHeight 等属性可以获得网页可见区域的宽和高,window和windowscreen也有一些相关属性,例子代码如下:

<body><SCRIPT LANGUAGE="JavaScript">

var  s = "";

s += "\r\n网页可见区域宽:"+ documentbodyclientWidth;

s += "\r\n网页可见区域高:"+ documentbodyclientHeight;

s += "\r\n网页可见区域宽:"+ documentbodyoffsetWidth  +" (包括边线和滚动条的宽)";

s += "\r\n网页可见区域高:"+ documentbodyoffsetHeight +" (包括边线的宽)";

s += "\r\n网页正文全文宽:"+ documentbodyscrollWidth;

s += "\r\n网页正文全文高:"+ documentbodyscrollHeight;

s += "\r\n网页被卷去的高:"+ documentbodyscrollTop;

s += "\r\n网页被卷去的左:"+ documentbodyscrollLeft;

s += "\r\n网页正文部分上:"+ windowscreenTop;

s += "\r\n网页正文部分左:"+ windowscreenLeft;

s += "\r\n屏幕分辨率的高:"+ windowscreenheight;

s += "\r\n屏幕分辨率的宽:"+ windowscreenwidth;

s += "\r\n屏幕可用工作区高度:"+ windowscreenavailHeight;

s += "\r\n屏幕可用工作区宽度:"+ windowscreenavailWidth;

s += "\r\n你的屏幕设置是 "+ windowscreencolorDepth +" 位彩色";

s += "\r\n你的屏幕设置 "+ windowscreendeviceXDPI +" 像素/英寸";

alert(s);

</SCRIPT>

1、首先在html页面上,看到有一个按钮,要获得这个按钮离顶部的距离。

2、在按钮的事件函数里,先取这个按钮的offset数据,通过jquery的offset方法。

3、得到的结果是一个数组对象,只要获取里面的top数据就行了。

4、使用log方法,把数据输出到浏览器的控制台。

5、运行页面,看到现在的按钮位置。距离顶部估计是500px左右吧,点击一下这个按钮。

6、然后看一下控制台上的数据,得到距离为439的像素。

jQuery的api文档中有列出来,获取网页的滚动高度用方法:scrollTop() 即可。注意:要获取网页的滚动高度,兼容所有浏览器的写法是获取window对象然后调用scrollTop()函数。简单示例:alert($(window)scrollTop());jQuery的api文档目录翻译见:

<html>

<head>

</head>

<body style="height:400px">

<div id="div1">12312</div>

<script type="text/javascript">

documentgetElementById("div1")styleheight=documentbodystyleheight;

alert(documentgetElementById("div1")styleheight);

//如果body,没有设置样式,这个高度就为空

documentgetElementById("div1")styleheight=documentbodyclientHeight

alert(documentgetElementById("div1")styleheight);

//如果设置了样式,offsetHeight就等于样式的height否则等于clientHeight

documentgetElementById("div1")styleheight=documentbodyoffsetHeight

alert(documentgetElementById("div1")styleheight);

</script>

</body>

</html>

你的意思是想根据mainFrame里面的实际内容动态的更改它的高是吗?

那你可以在mainaspx初始化写上这样一句

function screeChange(){

windowparentdocumentgetElementById('rightFrame')rows="50,"+documentbodyscrollHeight+",100";

}

</script>

body里面加上:onload="screeChange()"

注意一定要在body里面或者它的后面初始化,不然获取不到内容高度。

其中documentbodyscrollHeight获取mainaspx页面的实际文本高度,然后赋值给它的父窗体中的rightFrame的row值。。。

另外:<frame src="bottomhtm" id="bottomFrame" name="mainFrame" frameborder="0" scrolling="no" >

这个name值是不是写错鸟。。。。

另外在aspnet编程中。。。建议舍弃frameset 使用irame哈。。

以上就是关于如何使用 JavaScript 获取页面,窗口的高度全部的内容,包括:如何使用 JavaScript 获取页面,窗口的高度、如何获取元素距离页面顶部的高度、怎么获取网页高度,屏幕高度,滚动高度等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9660180.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存