use*_*028 1 html css php random image
我是css和html甚至编码的新手.我目前正在使用本地路径创建自己的起始页面,该路径将我的书签加载到html文件中.背景图像控件通过style.css文件.我已经搜索了一些解决方案,创建一个PHP脚本来加载css和html上的随机图片,我尝试了各种解决方案,但它似乎并不适合我.所以我们走了:
用于传递background-image的index.html文件:
<link rel="stylesheet" href="style.css" type="text/css" />
Run Code Online (Sandbox Code Playgroud)
和style.css:
body {
background-image: url("Wallpaper01.jpg") ;
Run Code Online (Sandbox Code Playgroud)
}
自从我下载代码后,我不知道如何更改它.此.jpg文件与html和css位于同一目录中.
有人可以帮我一个随机的图像吗?请提供代码来完成它,插入它或者我需要的任何东西,这将不仅仅是告诉我该怎么做.
很多,非常感谢有人可以帮助我.谢谢!
您可以尝试以下方式:
<?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
?>
Run Code Online (Sandbox Code Playgroud)
CSS(头脑中)
<html>
<head>
<title></title>
<style type="text/css">
<!--
body{
background: url(images/<?php echo $selectedBg; ?>) no-repeat;
}
-->
</style>
</head>
<body>
<!-- website -->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
编辑(用户评论)
<?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
?>
<html lang="en">
<head>
<meta charset=utf-8>
<link rel="stylesheet" href="style.css" type="text/css" />
<title>Start Page</title>
<style type="text/css">
<!--
body{
background: url(images/<?php echo $selectedBg; ?>) no-repeat;
}
-->
</style>
</head>
<body>
<div id="one">01 <div class="title">General</div>
<div class="links"> <a href="google.com">Google </a>
</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)