无法实现随机背景图像


Having trouble implementing random background images

我想让这种随机背景图像编码工作起来:http://css-tricks.com/snippets/php/randomize-background-image/

我仔细检查了文件名、路径、所有代码和说明。我想有些事情我不明白。

您可以在此处查看页面:http://agentboris.com/newwebsite/index.html

谢谢你的帮助。

您的文件位于http://agentboris.com/newwebsite/index.html在.html中,它应该在.php中,以便您能够使用教程中的php代码。

尝试使用以下简单代码(不要忘记将索引文件保存为php扩展名):

  <?php
  $bg = array('bg-01.jpg', 'bg-02.jpg', 'bg-03.jpg', 'bg-04.jpg', 'bg-05.jpg', 'bg-06.jpg', 'bg-07.jpg' ); // array of filenames
  $i = rand(0, count($bg)-1); // generate random number size of the array
  $selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
?>

 <!DOCTYPE html>
<html>
    <head>
        <title>Tests purposes</title>
        <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
        <style type="text/css">
        <!--
        body{
        background: url(images/<?php echo $selectedBg; ?>) no-repeat;
        }
        -->
        </style>
    </head>
    <body>

    </body>
</html>