不同的CSS背景图像取决于月和年 [英] Different CSS background-image depending on month and year
问题描述
基本上,我有一个带有文本的div,我想让背景显示不同的图像,具体取决于它的月份和年份。
Basically, I have a div with text in it, and I want the background to display a different image depending on what month and year it is.
我该如何实现?
任何帮助将会非常好评!
How can I achieve this? Any help would be greatly appriciated!
*我已经准备了4年价值的每月图片,标有 month 0_2011.png到month11_2014.png已经有帮助?*
*I've prepared 4 years worth of monthly images labelled "month0_2011.png" to "month11_2014.png" already if that helps?*
推荐答案
添加一个。 php
扩展到CSS并使用PHP代码来确定。只需使用标准的PHP标签。
Add a .php
extension to the CSS and use PHP code to determine that. Just use standard PHP tags.
例如:
body
{
background-image:url('<?php echo $currentImagePath ?>');
}
其中 $ currentImagePath
是您的图像的路径,使用PHP事先确定(即页首)。
Where $currentImagePath
is the path to your image, determined beforehand (i.e. top of page) using PHP.
$currentImagePath = $_SERVER['DOCUMENT_ROOT'] . "/css/images/" . "month" . (date("n") - 1) . "_" . date("Y") . ".png";
将它们放在一起:
<?php
header("Content-type: text/css");
$currentImagePath = $_SERVER['DOCUMENT_ROOT'] . "/css/images/" . "month" . (date("n") - 1) . "_" . date("Y") . ".png";
?>
body
{
background-image:url('<?php echo $currentImagePath ?>');
}
剩下的是调整路径以适应您的配置。
All that is left is adjusting the path to fit your configuration.
这篇关于不同的CSS背景图像取决于月和年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!