某些时间的颜色变化不适用于Js

时间:2016-07-29 16:33:48

标签: javascript html css colors

所以,基本上,我已经创建了一些Javascript,在一天的某个特定时间(某些小时)之后,它会改变页面的背景颜色。我对如何去做感到困惑,但在这个网站上询问后,我能够指出正确的方向。

以下是代码(有关我的问题的信息在代码下面):



## ----------- ##
## Core tests. ##
## ----------- ##

configure:1384: checking build system type
configure:1402: result: powerpc-ibm-aix6.1.0.0
configure:1410: checking host system type
configure:1424: result: powerpc-ibm-aix6.1.0.0
configure:1433: checking cached host system type
configure:1444: result: ok
configure:1501: checking for gcc
configure:1517: found /usr/bin/gcc
configure:1527: result: gcc
configure:1771: checking for C compiler version
configure:1774: gcc --version </dev/null >&5
Could not load program gcc:
        Dependent module /opt/freeware/lib/libiconv.a(libiconv.so.2) could     not be loaded.
    Member libiconv.so.2 is not found in archive
configure:1777: $? = 255
configure:1779: gcc -v </dev/null >&5
Could not load program gcc:
    Dependent module /opt/freeware/lib/libiconv.a(libiconv.so.2) could not     be loaded.
    Member libiconv.so.2 is not found in archive
configure:1782: $? = 255
configure:1784: gcc -V </dev/null >&5
Could not load program gcc:
    Dependent module /opt/freeware/lib/libiconv.a(libiconv.so.2) could not     be loaded.
    Member libiconv.so.2 is not found in archive
configure:1787: $? = 255
configure:1810: checking for C compiler default output file name
configure:1813: gcc    conftest.c  >&5
Could not load program gcc:
    Dependent module /opt/freeware/lib/libiconv.a(libiconv.so.2) could not be loaded.
    Member libiconv.so.2 is not found in archive
&#13;
//determines the bg color
	var curHour = new Date().getHours(); //finds the current hour
	var background = document.getElementById('myBody'); //gets background
	if (curHour > 4 && curHour < 11){
		background.style.backgroundColor = "lightblue"; //Morning 
	}
	else if (curHour > 11 && curHour < 17) {
		background.style.backgroundColor = "#3a9fe5"; //around afternoon
	}
	else if (curHour > 17 && curHour < 21) {
		background.style.backgroundColor = "#102a55"; //evening
	}
	else {
		background.style.backgroundColor = "black"; //night
	}
//Please note: this isn't ALL of the javascript, just the bit I'm using to change the color
&#13;
#myBody {
	background-color: lightSeaGreen; /*This is the default color      in case something goes wrong with the JS*/
	z-index: -999;
}
&#13;
&#13;
&#13;

好吧我的问题是当我查看页面时(目前大约是上午11:30,我在哪里),背景颜色显示为黑色,尽管黑色不应该显示到晚上9点(即第21小时)

我尝试修复代码的方法是将黑色的else语句更改为:

&#13;
&#13;
<!DOCTYPE html>
<html>
	<head>
		<script src="JsSnow.js"></script>
		<link rel="stylesheet" type="text/css" href="style.css">
	</head>
	<body id="myBody">
		<canvas id="myCanvas"></canvas>
	</body>
</html>
&#13;
&#13;
&#13;

不幸的是,当我尝试这样做时,它会导致页面上的其他内容消失,并使页面成为lightSeaGreen的默认颜色。

奇怪的是,页面在上午11点之前显示了正确的颜色,昨天它显示了晚上和晚上的正确颜色。 。 。

1 个答案:

答案 0 :(得分:3)

你缺少curHour==11的案例,你的代码只是检查严格更小,严格更大,所以在上午11点和下午5点的整个小时,你将陷入黑色。