PHP jquery随机更改每个季度的div背景

PHP jquery随机更改每个季度的div背景,php,jquery,html,css,Php,Jquery,Html,Css,在文件夹(1、2、3、4)中,我每个季节(春夏…)有10个背景。现在我需要为每一季随机更改div背景。我的意思是为spring更改文件夹1的随机图像 如何使用PHP或混合jquery/css更改背景 如果希望在每个页面上重新加载一个随机图像,请在PHP模板文件中包含以下代码段 $season = 'spring'; function random_pic($season) { $dir = 'backgrounds/' . $season; $files = glob($dir

在文件夹(1、2、3、4)中,我每个季节(春夏…)有10个背景。现在我需要为每一季随机更改div背景。我的意思是为spring更改文件夹1的随机图像


如何使用PHP或混合jquery/css更改背景

如果希望在每个页面上重新加载一个随机图像,请在PHP模板文件中包含以下代码段

$season = 'spring';
function random_pic($season)
{
    $dir = 'backgrounds/' . $season;
    $files = glob($dir . '/*.*');
    $file = array_rand($files);
    return $files[$file];
}

要动态获取
$seasure
值,您可以参考的,并包括该函数。

您必须编写自己的代码,以便在php中检测seasure。没有将返回当前季节的现有函数。问题是北半球和南半球的情况不同

佩奇有一个很好的例子,说明了如何获得当前的季节。一旦你有了它,你可以使用一个switch语句来选择一个应该选择背景的文件夹

接下来,您可以使用查找此文件夹中的所有文件,并使用选择其中一个文件


最后一部分是将图像放入html中。最简单的方法是使用内联css,但您也可以将路径传递给jQuery脚本,让它处理背景。

您需要记录当前日期并跟踪它是否在哪个季节,因此基于此,根据季节从服务器获取文件以应用主题。您将以php/cakephp更好的方式进行操作。

我会这样做。获取季节,在每个文件夹中有预先指定数量的图像,并按以下方式命名:1.jpg、2.jpg、3.jpg:

//Get Season
$month = DATE("m");
$season = "winter";

if ($month >= "03" && $month <= "05")
  $season = "spring";
elseif($month >="06" && $month <="08")
  $season = "summer";
elseif ($month >= "09" && $month <= "11")
  $season = "autumn";

//Generate a random number between 1 and 10 (change to match your range of backgrounds)
$random_number = rand (1, 10);

$image_path = "backgrounds/" . $season . "/" . $random_number . ".jpg";

// Print into tag, CSS code, etc
echo "Image path: " . $image_path; 
//获取季节信息
$month=日期(“m”);
$season=“winter”;

如果($month>=“03”&&&$month=“06”&&&&$month=“09”&&&&&$month)您希望在单页访问期间随机更改背景,还是在每个页面重新加载时仅随机更改一个图像?您可以共享您的代码吗?到目前为止您有什么想法?@Paul:单页访问。可能仅将文件夹扫描限制为图像文件:)