【问题标题】:Match Text Colour to Randomly Selected Background Colour (PHP Array)将文本颜色与随机选择的背景颜色匹配(PHP 数组)
【发布时间】:2014-01-19 19:33:22
【问题描述】:

我正在构建一个具有随机选择背景颜色的 Wordpress 网站。

The site can be seen here.

我完成随机 bg 颜色的方式是使用 php 数组(函数见下文)。

function random_bg() {
    $bgs = array ('FF8400', 'F7F7F7', '00C6FF', 'FF3D3D', '00E4B6', 'FFE063', 'AE00FF', '888888', '91d8ac', '7ce5ee', 'eeca7c', 'eea27c', 'e28989', 'f79090', '94ecd4', 'ffa92e', 'aaedb4', '5dbee2', 'e24343', 'e2ac43', '70acef');
    $n = rand( 0, 20);
    echo $bgs[$n];
}

现在,我想让页脚中的链接更改颜色,以匹配随机选择的任何背景颜色。有谁知道有没有办法做到这一点?

提前非常感谢!

最大。

【问题讨论】:

  • 将带有选定背景的内联样式添加到您的 div 中。
  • @Rikesh 嗨,Rikesh,谢谢!抱歉,我不太清楚你的意思,我知道那个页脚等的 div 选择器,但是我将如何指定它来检测通过 css 选择了哪种随机 bg 颜色?你有机会提供那个css的例子吗?干杯!

标签: php arrays wordpress colors background


【解决方案1】:
function random_colors() {
    $bgs = array ('FF8400', 'F7F7F7', '00C6FF', 'FF3D3D', '00E4B6', 'FFE063', 'AE00FF', '888888', '91d8ac', '7ce5ee', 'eeca7c', 'eea27c', 'e28989', 'f79090', '94ecd4', 'ffa92e', 'aaedb4', '5dbee2', 'e24343', 'e2ac43', '70acef');
    $fgs = array (etc...)
    $n = rand( 0, 20);
    return array($bgs[$n], $fgs[$n];
}

$color = random_colors();

$background = $color[0];
$foreground = $color[1];

现在在 HTML 中

<body style="background:<?php echo $background; ?>">

    <footer style="background:<?php echo $foreground; ?>">Footer</footer>
</body>

【讨论】:

  • 感谢您的帮助佩斯。尽管该 php 函数产生错误:' Parse error: syntax error, unexpected T_VARIABLE in /home/brokencu/public_html/ChecktheRhyme/wp-content/themes/twentythirteen/functions.php on line 553'
  • 我要让你解决这个问题!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-14
  • 1970-01-01
  • 2017-09-11
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
  • 2021-03-28
相关资源
最近更新 更多