gpt4 book ai didi

PHP/CSS 随机背景

转载 作者:搜寻专家 更新时间:2023-10-31 21:27:06 25 4
gpt4 key购买 nike

尝试在两张图片之间获取随机背景。

PHP(在 index.php 中的 Doctype 之前):

<?php
$bg = array('back1.jpg', 'back2.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
?>

CSS:

body {
height: 100%;
margin: 0;
color: #474747;
font: 13px/23px 'Exo 2', sans-serif;
min-width: 1186px;
background: url( ../img/<?php echo $selectedBg; ?>) no-repeat 50% 50%;
background-size: cover;
background-attachment: fixed;
}

问题是没有为任何一个图像设置背景!

编辑:

我已经转向了一个完整的 PHP 方法,尽管它有错误:

Line : 5,   Error type : 4
Message : syntax error, unexpected '(', expecting variable (T_VARIABLE) or '$'

代码:

<?php
$bg = array('back1.jpg', 'back2.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
$('body').css( { 'background-image': 'url( img / ' + echo $selectedBg + ')' } );
?>

最佳答案

你要找的是

array_rand();

PHP 手册:http://php.net/manual/en/function.array-rand.php

对于您的 PHP:

<?php
$bg = array('back1.jpg', 'back2.jpg' ); // array of filenames
$i = array_rand($bg); // generate random number size of the array
$selectedBg = $bg[$i]; // set variable equal to which random filename was chosen
?>

关于PHP/CSS 随机背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34445658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com