Php 背景图像赢得';t如果随机生成,则显示100%
我正在制作一个有两个背景图像的网站,其中一个始终相同,即:Php 背景图像赢得';t如果随机生成,则显示100%,php,html,css,zurb-foundation,Php,Html,Css,Zurb Foundation,我正在制作一个有两个背景图像的网站,其中一个始终相同,即: background-image: url("../images/centre-image.jpg"); 另一个通过php生成,随机显示背景图像: <?php $bg = array('large-top1.jpg', 'large-top2.jpg', 'large-top3.jpg', 'large-top4.jpg', 'large-top5.jpg', ); // array of filenames
background-image: url("../images/centre-image.jpg");
另一个通过php生成,随机显示背景图像:
<?php
$bg = array('large-top1.jpg', 'large-top2.jpg', 'large-top3.jpg', 'large-top4.jpg', 'large-top5.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
?>
<img src="images/background-images/<?php echo $selectedBg; ?>" class="bg">
但是,如果我尝试将类似的CSS添加到随机生成的图像中,它将不起作用(仅以完整的jpg大小显示,然后重复),并且不确定“显示”是否能解决问题。据我所知,生成的图像不是设置为背景,而是设置为要显示的图像,因此您无法对其应用背景样式 有两件事可以做,一是直接将样式应用于图像标记,即
img.bg{
width:100%;
height:100%;
}
或者创建一个元素并将生成的图像添加到其样式中
看起来像这样:
<?php
$bg = array('large-top1.jpg', 'large-top2.jpg', 'large-top3.jpg', 'large-top4.jpg', 'large-top5.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
?>
<style>
div.bg{
background-image: url("images/background-images/<?php echo $selectedBg; ?>");
background-size: 100% auto;
}
</style>
<div class="bg"></div>
分区背景{
背景图像:url(“图像/背景图像/”);
背景尺寸:100%自动;
}
最好将样式放在头标签之间css根本不关心image
src
属性是如何生成的。它永远不会看到php代码或知道它是php输出。什么是.bg类的CSS?什么是img.bg类的CSS?谢谢!问题是我第一次尝试在css文件中添加背景图像,但它不理解php回声。所以我把它放在了div里。
<?php
$bg = array('large-top1.jpg', 'large-top2.jpg', 'large-top3.jpg', 'large-top4.jpg', 'large-top5.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
?>
<style>
div.bg{
background-image: url("images/background-images/<?php echo $selectedBg; ?>");
background-size: 100% auto;
}
</style>
<div class="bg"></div>