在特定日期更改背景与CSS? [英] Change Background with CSS on particular date?

查看:155
本文介绍了在特定日期更改背景与CSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道如何在特定日期使用CSS自动更改网站的背景?喜欢情人节,复活节,圣诞节等。

Does anyone know how to change the background of a website automatically using CSS on specific dates? Like valentines, easter, xmas etc.

推荐答案

您必须使用服务器端语言这个。例如,使用PHP,您可以使用 date()并执行类似以下操作:

You'll have to use either a server-side language like PHP or JavaScript for this. With PHP, for example, you can access the server date/time with date() and do something like:

if(date('m/d') == '2/14'))
    'get your girl a present, dude!';

使用服务器端代码的好处是用户可以更快。缺点是它是你的服务器的日期和时间,而不是用户的。所以你可以使用Javascript做同样的事情,例如:

The upside of using server-side code is that it's faster for the user. The disadvantage is that it's your server's date and time, not the user's. So you could do the same thing with Javascript, like so:

var curtime = new Date(),
    curday = curtime.getDate(),
    curmonth = curtime.getMonth()+1;

if(curmonth == 2 && curday == 14)
   alert('better be quick');

任何一种方式都可以使用。

Either way will work.

为了清楚起见:上面将允许你检查一天。然后,您可以使用它来向您的HTML元素添加一个CSS类,例如,您为那一天准备的。假设你使用Javascript做,你可以写

Added for clarity: The above will allow you to check the day. You can then use that to add a CSS class to your HTML element, for example, which you've prepared for that day. Say you do it with Javascript, you'd write

$('#wrap').addClass('valentines');

而不是上面的 alert()这将向< div id =wrap> 添加类 valentines - 仅作为示例。

instead of the alert() above. This will add the class valentines to the <div id="wrap"> - just as an example. You can then do whatever you like with that CSS class.

编辑:我使用了 jQuery 在最后一个代码片段。你也可以用Javascript单独做 - Loktar已经在他的答案中添加了。

I used jQuery in the last snippet. You can do it with Javascript alone too - Loktar already added that in his answer.

这篇关于在特定日期更改背景与CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆