我有一个简单的代码,可以在晚上显示一个图像,在白天显示一个图像。由于某种原因,日图像始终显示。
javascript有点分裂。这部分在页面上:
<script>
//waits until document is open
$(window).load(function() {
//if it's night time when you read this it displays a night time photo, if it's day time it's a day time photo... don't know how many people might notice
if (time === "night"){
$.backstretch("cafenight.jpg");
//makes the font white with a black outline so you can read it easily on the night photo
$("#content").css("color","#FFFFFF");
$("#content").css("text-shadow","1px 0 0 #000, 0 -1px 0 #000, 0 1px 0 #000, -1px 0 0 #000");
}
else {
$.backstretch("cafeday.jpg");
$("#content").css("text-shadow","1px 0 0 #fff, 0 -1px 0 #fff, 0 1px 0 #fff, -1px 0 0 Ωfff");}});
$(document).ready(function(){
//fades in the first line of text
$("#partFive").fadeIn(2000)
setTimeout(function () {
//fades in the second line of text
$("#partFive").fadeOut(2000)
setTimeout(function () {
$("#partSix").fadeIn(3000)
setTimeout(function () {
$("#partSix").fadeOut(3000)
setTimeout(function () {
window.location.href = "6.html";
}, 4000);
}, 2000);
}, 2000);
}, 3000);
});
</script>
这部分在basic2.js
中// Javascript and Jquery rock!
//Variables to be used throughout
//The user's current hour
var hour = (new Date()).getHours()
//use the variable 'time' to know if it is day or night. Can use to change background even wording.
//The variable 'time' will either be Day or Night depending on what time it is
var time = []
if (hour < 20){
time.push("day");
}
else {
time.push("night");
}
//Gets the user's city based on IP and replaces #homeCity span with the name of their city
// not perfectly accurate but fun
$.getJSON("http://www.telize.com/geoip?callback=?",
function(json) {
$("#homeCity").html(json.city);
}
);
//The user's username, they enter it near the beginning
var userName = []
答案 0 :(得分:3)
我认为你的类型犯了错误。您将时间定义为数组并推入其中...但在另一个文件中,您将其称为字符串并对其进行类型和值比较。它永远不会在这一行上等于“夜晚”,因此总是与其他一起使用,因此默认为日期图像。
编辑:
var isDay = (hour < 20);
if (isDay) {
//do day stuff
}else{
//do night stuff
}
答案 1 :(得分:0)
看起来你的时间变量是一个数组
var time = []
if (hour < 20){
time.push("day");
}
else {
time.push("night");
}
然而,在你的if语句中,它被视为字符串
if (time === "night"){
$.backstretch("cafenight.jpg");
//makes the font white with a black outline so you can read it easily on the night photo
$("#content").css("color","#FFFFFF");
$("#content").css("text-shadow","1px 0 0 #000, 0 -1px 0 #000, 0 1px 0 #000, -1px 0 0 #000");
}
更改if语句应该可以解决问题
if (time[0] === "night") {
...
}
编辑:否则,请使用其他注释中建议的布尔值
var day;
if (hour < 20){
day = true;
}
else {
day = false
}
然后
if (!day){
$.backstretch("cafenight.jpg");
//makes the font white with a black outline so you can read it easily on the night photo
$("#content").css("color","#FFFFFF");
$("#content").css("text-shadow","1px 0 0 #000, 0 -1px 0 #000, 0 1px 0 #000, -1px 0 0 #000");
}