[ javascript ] javascript计算屏幕中的各种高度!

这几天对于js控制元素在浏览器端的高度问题,

有点麻烦,于是决定深入学习一番,经过一番努力测试,得如下结果:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script> 
  <title></title>
</head>
<body>
	<div style="height:800px;width:500px;border:10px solid red;" id="div1">
		<div style="margin-top:100px;height:800px;width:500px;border:10px solid blue;" id="div2">
		<div style="margin-top:100px;height:100px;width:500px;border:10px solid green;position:fixed;bottom:100px;" id="div3">
			<div ></div>
		</div>
	</div>

	<script type="text/javascript">
	$(window).scroll(function(){
		console.log("window-height:" + $(window).height());
		console.log("div1-height:" + $("#div1").height());
		console.log("div2-height:" + $("#div2").height());
		console.log("div3-height:" + $("#div3").height());
		console.log("div1到document顶部距离:div1-offset-height:" + $("#div1").offset().top);
		console.log("div2到document顶部距离:div2-offset-height:" + $("#div2").offset().top);
		console.log("div3到document顶部距离:div3-offset-height:" + $("#div3").offset().top);
		console.log("document被压缩的距:document-scrollTop:" + $(document).scrollTop());
		console.log("div3距离浏览器边框底部高度:" + ($(window).height() - $("#div3").height() - ($("#div3").offset().top- $(document).scrollTop())));
		console.log("div3距离浏览器边框底部真实高度(不包括div的上下border宽度):" + ($(window).height() - $("#div3").outerHeight() - ($("#div3").offset().top- $(document).scrollTop())));

	});
	</script>
</body>
</html>

结合前两天对scrollTop与offset的理解,终于对于浏览器中各种高度作出了一个总结。

哈哈,妈妈再也不用担心浏览器的高度问题了。


偶有所得,以记录之,谨防忘记。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值