简体   繁体   English

Javascript 根据时间更改图像

[英]Javascript to change image depending on time

Basically, I've got a script which changes the page background depending on the time.基本上,我有一个脚本可以根据时间更改页面背景。

<script language="JavaScript">
day=new Date()     //..get the date
x=day.getHours()    //..get the hour

if(x>=0 && x<4) {

   document.write('<style type="text/css">#header{background: white url(images/assets/1st.jpg); color: black}"></style>')

} else

if(x>=4 && x<12) {

   document.write('<style type="text/css">#header{background: white url(images/assets/2nd.jpg); color: black}"></style>')

} else

if(x>=12 && x<18) {

   document.write('<style type="text/css">#header{background: white url(images/assets/3rd.jpg); color: black}"></style>')

} else

if (x>=18 && x<24) {

   document.write('<style type="text/css">#header{background: white url(images/assets/4th.jpg); color: black}"></style>')

}

So as you can see, the first background changes between at 4am and so on.如您所见,第一个背景在凌晨 4 点之间发生变化,依此类推。

What I would look to do is to change the background at different times every day, reading from some sort of text file or something.我想做的是每天在不同的时间改变背景,从某种文本文件或其他东西中读取。 For example, on the 10th June the first background changes at 4:15am and the others with different times, on the 11th June the first background changes at 4:22am or something and so on.例如,6 月 10 日第一个背景在凌晨 4:15 变化,其他时间不同,6 月 11 日第一个背景在凌晨 4:22 变化等等。

Could someone possibly find me or write me something to do this?有人可能会找到我或给我写一些东西来做这件事吗? I can't find anything anywhere!我在任何地方都找不到任何东西!

Thanks ever so much非常感谢

Let's start by rewriting the script in a more modern form (ie using a script tag that is not deprecated, declaring variables...), and putting the settings in an array to make it more configurable:让我们首先以更现代的形式重写脚本(即使用未弃用的脚本标签,声明变量......),并将设置放入数组中以使其更具可配置性:

<script type="text/javascript">

var now = new Date();
var date = (now.getMonth() + 1) * 100 + now.getDate();
var time = now.getHours() * 100 + now.getMinutes();

var settings = [
  { date: 1224, time: 2400, image: 'xmas.jpg'}, // all christmas eve
  { date: 704, time: 2400, image: 'bang.jpg'}, // all fourth of july
  // any other day:
  { date: -1, time: 400, image: '1st.jpg'},
  { date: -1, time: 1200, image: '2nd.jpg'},
  { date: -1, time: 1800, image: '3rd.jpg'},
  { date: -1, time: 2400, image: '4th.jpg'}
];

var setting;
for (var i = 0; i < settings.length; i++) {
  var s = settings[i];
  if ((s.date == -1 || s.date == date) && time < s.time) {
    setting = settings[i];
    break;
  }
}

document.write('<style type="text/css">#header{background: white url(images/assets/'+setting.image+'); color: black}</style>');

</script>

Note: I removed a spurios "> in the style sheet code.注意:我在样式表代码中删除了一个 spurios ">

Edit:编辑:

I added code for handling dates, and simplified the time format.我添加了处理日期的代码,并简化了时间格式。 At the end of the settings you have items with date: -1 which apply to any date that is not earlier in the settings.在设置结束时,您有带有date: -1适用于设置中不早于的任何日期。

On the server, place the file in a format such as JSON or XML with information about changes in the background.在服务器上,以 JSON 或 XML 等格式放置文件,其中包含有关后台更改的信息。

For example something like this: (file: http://example.com/bg.json )例如这样的:(文件: http://example.com/bg.json

{
    "default": {
        "00:00": "http://example.com/image1.png",
        "04:00": "http://example.com/image2.png",
        "10:00": "http://example.com/image3.png",
        "18:00": "http://example.com/image4.png",
    },
    // In format DD/MM/YYYY
    // This means that every Christmast (every year)
    "24/12" : {
        "00:00": "http://example.com/mary-xmas.png"
    },
    // and so on ...
}

Next you wil load this file by jQuery json (ajax) page's url is for example http://example.com/index.html : Next you wil load this file by jQuery json (ajax) page's url is for example http://example.com/index.html :

$(function() {
      $.ajax({
          url: 'bg.json',
          type: 'json',
          success: function(data) {
              // In data you have complete object, you can easily parse it
              // and create timer to change the background 
          }
      });
  });

And for changing background rather use this:而对于改变背景,宁愿使用这个:

$('#header').css('background', 'white url(...) scroll no-repeat 0 0');

Thi exaple needs jQuery, but it is not too hard to rewrite it to pure JavaScript or to another framework like Prototype or Mootools.这个例子需要 jQuery,但将其重写为纯 JavaScript 或其他框架(如 Prototype 或 Mootools)并不难。

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM