网页背景图片自适应浏览器大小

本文介绍了一种通过JavaScript实现背景图片自适应浏览器窗口大小的方法。利用window.onresize和window.onload事件监听器来获取浏览器窗口的宽度和高度,并相应地调整背景图片的尺寸。

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

背景图片自适应浏览器大小

 
<!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”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>背景图片自适应浏览器大小</title>
<script type=”text/javascript”>
window.onresize = window.onload = function(){
var w,h
if(!!(window.attachEvent && !window.opera))
{
h = document.documentElement.clientHeight;
w = document.documentElement.clientWidth;
}else{
h =    window.innerHeight;
w = window.innerWidth;
}
document.getElementById(‘container’).value  =’窗口大小:’ + ‘width:’ + w + ‘; height:’+h;
var bgImg = document.getElementById(‘bg’).getElementsByTagName(‘img’)[0];
bgImg.width = (w);
bgImg.height= (h) ;

}
</script>
<style type=”text/css”>
*html#container{background:url(../images/bg2.gif) no-repeat; width:100%;  min-height:1000px; height:auto !important; height:1000px;}
#bg{position:fixed;top:0px;left:0px;z-index:-999;}
.login{margin:0 auto; width:100%; height:420px; position:absolute; top:15%;}
.logo{margin:0 auto; width:450px; height:36px; background:url(../images/logo.png) no-repeat;}
.login_ct{background:#666; height:323px; margin-top:50px;}
.login_form{width:681px; height:323px; background:#CCC; margin:0 auto;}
</style>
</head>

<body>
<div id=”container”>

<div id=”bg”><img src=”images/bg2.gif” width=”1920″ height=”1080″ />
</div>
<div>
<div>
<div>

</div>
</div>
</div>
</div>
</body>
</html>

 

转载于:https://www.cnblogs.com/siri/p/3410837.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值