gpt4 book ai didi

php - 随机背景图片 PHP

转载 作者:行者123 更新时间:2023-11-28 06:56:55 26 4
gpt4 key购买 nike

我使用下面的 PHP 代码在每次刷新页面时显示随机标题图像。这工作正常,但我需要向某些图像添加特定的 background-position css 属性。

例如 headerimage3.jpg 需要 background-position:20px 而 headerimage1.jpg 需要 background-position:50px。

是否可以使用下面的 PHP 代码或者是否需要使用 javascript?

<style type="text/css">
#header-image{background:url("/files/headerimage<?php echo rand(1,4)?>.jpg");background-position:center center;background-size:cover}}
</style>

最佳答案

添加 php header :

<?php
$rnd = rand (1,4);
$pos = ($rnd == 1) ? "50px 0;" : ($rnd == 3) ? "20px 0;" : "top left";
?>

然后在 css 中:

<style type="text/css">
#header-image
{
background-image: url("/files/headerimage<?php echo $rnd ?>.jpg");
background-position: <?php echo $pos ?>;
background-size: cover;
}
</style>

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

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