开发者

Change background based on the time of day

im trying to write a script to change my body background based on the whether its am or pm, I have the following, only its not working, can anybody see why?

<!-- Change开发者_C百科 background -->
var currentTime = new Date().getHours();
if (7 <= currentTime && currentTime < 20) {
    if (document.body) {
        document.body.background = "images/backgrounds/day.jpg";
    }
}
else {
    if (document.body) {
        document.body.background = "images/backgrounds/night.jpg";
    }
}
<!-- Change background -->


A good idea would be to actually use jQuery selectors and modify the CSS:

$('body').css({
    background: 'url(yourUrl)'
});


there maybe several problems with this

1) I can't seem to access those two images. Are they actually at the path you specified?

http://int.test.venndigital.co.uk/correlatesearch/_includes/images/backgrounds/day.jpg http://int.test.venndigital.co.uk/correlatesearch/_includes/images/backgrounds/night.jpg


This should be what you need. You should really try to learn javascript instead of just asking people to write it for you, though. Either that or hire someone else to do the job.

$('body').css('background-image', function(){
  var url, hour = new Date().getHours();
  if (hour > 7 && hour < 20) {
    return 'http://example.com/day.jpg';
  } else {
    return 'http://example.com/night.jpg';
  }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜