How can I dynamically change the gradient in the body with js based on time of day?
Thanks
—
Img
http://a1.dspncdn.com/media/692x/da/dc/4e/dadc4ed5117d4a8cc582199bb3ac9c68.jpg
How can I dynamically change the gradient in the body with js based on time of day?
Thanks
—
Img
http://a1.dspncdn.com/media/692x/da/dc/4e/dadc4ed5117d4a8cc582199bb3ac9c68.jpg
use the following code
HTML
<div id='time'>
</div>
JavaScript
var d = new Date();
var time = d.getHours();
var div=document.getElementById('time');
if (time < 12)
{
div.style.backgroundImage ="url('morning image')";
}
if (time >= 12 && time < 3)
{
div.style.backgroundImage ="url('afternoon image')";
}
if (time > 3)
{
div.style.backgroundImage ="url('http://a1.dspncdn.com/media/692x/da/dc/4e/dadc4ed5117d4a8cc582199bb3ac9c68.jpg')";
}
CSS
#time{
height:400px;
width:400px;
}
refer this fiddle
It might help to have a look at https://stackoverflow.com/a/4358182/2588199 and then either set a different background or a css gradient string.
Something like:
var currentTime = new Date().getHours();
//Change here to set the hours to wish to change between
if (7 <= currentTime && currentTime < 20) {
//place image or strng here
}else {
//Place image or string here
}