当季节背景发生变化时,我如何修改代码,使html和jscript同时工作

How may i amend the code so that both html and jscript is working at the same time, when the background is changed during the seasons?

本文关键字:修改 代码 html 工作 jscript 何修改 背景 季节 变化      更新时间:2023-09-26

如何使背景壁纸位于固定位置,并在打开时缩放到浏览器的窗口?我想改变背景以适应一年中的季节。

<html>
<head>
<?xml version="1.0" encoding="iso-8859-1"?>
<!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">
<title>TWGSB Departments</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
#fsbg {
width: 100%;
left: 0px;
top: 0px;
height: auto;
position: fixed;
z-index: -100;
min-height: 100%;
min-width: 1040px;
}
</style>
</head>
<body>
<script type="text/JavaScript">
var north = true;
var today = new Date();
var month = today.getMonth();
if (today.getDate() > 20) month++;
var backImg = ['summer.jpg','autumn.jpg','winter.jpg','spring.jpg'];
document.getElementsByTagName('body').item('0').style.backgroundImage = 
'url('+backImg[Math.floor((month+(north?6:0))/3)%4]+')';
</script>
</body>
</html><img        src="images/summer.jpg","images/autumn.jpg","images/winter.jpg","images/spring.jpg", name="fsbg" width="1920" height="1200" id="fsbg">
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

您的HTML有一些问题。如果修复了它们,可以使用background-size: cover覆盖元素,使用background-position: center center将背景居中放置在元素中。

它可能看起来像这样:DEMO

html, body {
    margin: 0;
    width: 100%;
    height: 100%;
    background: #ccc;
    background-size: cover;
    background-position: center center;
}