HTML - 根据分辨率让网页底部不留空

这篇博客探讨了在不同分辨率下,特别是在火狐浏览器中,如何利用JavaScript解决网页底部留空的问题。作者指出,当分辨率较低时,通过document.body.clientHeight获取的页面高度可能为0,从而引发错误。文章通过实例展示了JS如何调整页面样式,以确保在各种分辨率下,滚动到页面底部时无空白区域。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<html>

	<head>
		<title></title>
		<style type="text/css">
			html, body{
				margin:0;
				padding:0;
			}
			.top{
				width:100%;
				height:36px;
				background-color: #ff0000;
				z-index:9999;
			}
			.bottom{
				width:100%;
				height: 36px;
				margin-top:52px;
				background-color: #0000ff;
				float:left;
				border-top:2px solid black;
				position:relative;
				bottom:0;
			}
			.centerHeight{
				width:100%;
				height:60px;
				background-color: #00ff00;
				float:left;
				margin-top:60px;
				border-top:2px solid black;
			}
			.topMargin{
				margin-top:36px;
			}
			.bottomMargin{
				border-bottom:2px solid black;
			}
		</style>
	</head>

	<body>
		<div id="topDiv" class="top"><div>
		
		<div id="centerDiv" class="centerHeight topMargin"><div>
		<div id="centerDiv" class="centerHeight"><div>
		<div id="centerDiv" class="centerHeight"><div>
		<div id="centerDiv" class="centerHeight"><div>
		<div id="centerDiv" class="centerHeight"><div>
		<div id="centerDiv" class="centerHeight bottomMargin"><div>
		
		<div id="buttomDiv" class="bottom"><div>
		
		<script>
        
        var BodyHeight = document.body.scrollHeight;
        var ClientHeight = window.screen.height;

        if (BodyHeight <= ClientHeight) {
            document.getElementById("buttomDiv").style.position = 'absolute';
            document.getElementById("buttomDiv").style.bottom = '0px';
            document.getElementById("buttomDiv").style.width = '100%';
        }
		
		</script>
	</body>

</html>


↑ 这是在火狐里看到的效果,分辨率:279*340

↓ 下面这个是滚动条滚到最下面的效果

↑ 这个是在火狐里浏览,分辨率为:285*528

这是JS更改的效果,可以看的出来获取到网页浏览的大小其实是获取滚动条的大小,如果你获取document.body.clientHeight,当分辨率过低的时候就会获取到0,就会导致错误。

这个是原来的样式,这个就不解释了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值