Reputation: 41
I'm trying to make two different backgrounds depending on time. Day/night background. I programmed this but it doesn't work...
<script type="text/javascript">
var currentTime = new Date().getHours();
if (7 <= currentTime && currentTime < 20) {
<style>
html, body {
height: 100%;
margin: 0;
overflow-x: hidden;
overflow-y: auto;
padding: 0;
width: 100%;
background-image: url("bg_day.jpg");
background-repeat: no-repeat;
background-position: center center;
background-attachment: fixed;
}
</style>
}
else {
<style>
html, body {
height: 100%;
margin: 0;
overflow-x: hidden;
overflow-y: auto;
padding: 0;
width: 100%;
background-image: url("bg_night.jpg");
background-repeat: no-repeat;
background-position: center center;
background-attachment: fixed;
}
</style>
}
</script>
Can somebody please help me?
Upvotes: 2
Views: 745
Reputation: 206111
Keep your style
inside <style>
(or rather inside a separate .css file) where it needs to be... don't mix it with JS.
Use:
<script>
var currentTime = new Date().getHours();
var day = 7 <= currentTime && currentTime < 20;
document.body.style.backgroundImage = 'url('+ (day?"day.jpg":"night.jpg")+ ')';
</script>
</body>
in jQuery looks like:
var CTime = new Date().getHours();
$('body').css({backgroundImage:"url("+(CTime>=7&&CTime<20?"day.jpg":"night.jpg")+")"});
Or the readable way:
function bgByTime(){
var CTime = new Date().getHours();
return CTime >= 7 && CTime < 20 ?
"day.jpg" :
"night.jpg" ;
}
$('body').css({ backgroundImage: "url("+ bgByTime() +")" });
or the Object Literal way (jQuery):
var ct = new Date().getHours(),
img = ct>=7&&ct<20 ? "day.jpg" : "night.jpg";
bg = {backgroundImage : "url("+ img +")"};
$('body').css(bg);
Upvotes: 4
Reputation: 1308
Here you go:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>day/night dynamic background for user3329709</title>
<style type="text/css">
.day
{
background: url(http://miriadna.com/desctopwalls/images/max/Day-sky.jpg) no-repeat;
width: 500px;
height: 500px;
}
.night
{
background: url(http://growwhereyoureplanted.com/wp-content/uploads/2013/11/palmlixcom-night-sky-stars-background-psdgraphics.jpg) no-repeat;
width: 500px;
height: 500px;
}
</style>
<script src="http://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
var currentTime = new Date().getHours();
if (currentTime >= 7 && currentTime <= 15) {
$("#background").addClass("day");
} else {
$("#background").addClass("night");
}
});
</script>
</head>
<body>
<div id="background" class="day">
</div>
</body>
</html>
Upvotes: 1
Reputation: 18833
Adding style tags in js like that will not work. But since you specified jQuery, here's a basic example...
<style>
html, body {
height: 100%;
margin: 0;
overflow-x: hidden;
overflow-y: auto;
padding: 0;
width: 100%;
background-image: url("bg_day.jpg");
background-repeat: no-repeat;
background-position: center center;
background-attachment: fixed;
}
.nighttime{
background-image: url("bg_night.jpg");
}
</style>
In your footer somewhere add the js
<script type="text/javascript">
var currentTime = new Date().getHours();
if (currentTime > 20) {
$('body').addClass('nighttime');
}
</script>
Upvotes: 0
Reputation: 41
Tnx for all your answers but I solved my problem with this:
<script type="text/javascript">
<!--
var now = new Date();
var hours = now.getHours();
var psj=0;
document.write('<style type="text/css">')
if (hours >= 6 && hours <= 20){
document.write('html, body{height: 100%; margin: 0; overflow-x: hidden; overflow-y: auto; padding: 0; width: 100%; background-image: url("bg_day.jpg"); background-repeat: no-repeat; background-position: center center; background-attachment: fixed;}')
}
else{
document.write('html, body{height: 100%; margin: 0; overflow-x: hidden; overflow-y: auto; padding: 0; width: 100%; background-image: url("bg_night.jpg"); background-repeat: no-repeat; background-position: center center; background-attachment: fixed;}')
}
document.write('<\/style>')
//-->
</script>
Upvotes: -2