使用 PHP 数组分配随机 CSS 伪类
目标:当用户将鼠标悬停在 Wordpress 上时,将不同颜色的 CSS 背景随机分配给 Wordpress 生成的标签云。
我的方法(到目前为止不成功):
<?php
$colors = array( 0 => "#645676", "#427048", "#654335", "#ab2805", "#a59d57", "#302f2f", "#81510d" );
$tags = wp_tag_cloud('smallest=10&largest=10&unit=px&number=40&format=array&echo=0');
shuffle($tags);
foreach ($tags as $tag) {
shuffle($colors);
echo "<li style=\"a:hover=background:".$colors[0].";\"> $tag \n </li>";
}
?>
我认为 foreach PHP 循环将是最简单的方法。我创建了两个数组($colors 和 $tags)。我用我想要使用的颜色的十六进制标签手动填充 $colors 。 $tags 由 wordpress 标签云数组填充(如果感兴趣,请在此处了解更多信息:http://codex.wordpress。 org/Function_Reference/wp_tag_cloud),我已将其设置为获取此博客上 40 个最受欢迎的标签。
然后,我在 $tags 上使用 PHP shuffle 函数来混合前 40 个标签,通过 foreach 循环运行这 40 个标签,然后将其中一个打乱的 $colors 随机分配给 CSS 伪类,该伪类会被回显。
该代码有效,这里是输出示例:
<li style="a:hover=background:#302f2f;"> <a href='http://localhost:8888/nutritionwonderland_2/tag/truvia/' class='tag-link-49' title='3 topics' style='font-size: 10px;'>truvia</a>
</li><li style="a:hover=background:#81510d;"> <a href='http://localhost:8888/nutritionwonderland_2/tag/cancer/' class='tag-link-8' title='11 topics' style='font-size: 10px;'>cancer</a>
</li><li style="a:hover=background:#427048;"> <a href='http://localhost:8888/nutritionwonderland_2/tag/antioxidants/' class='tag-link-93' title='4 topics' style='font-size: 10px;'>antioxidants</a>
</li>
问题:当我将鼠标悬停在任何标签上时,背景永远不会显示。这让我觉得 CSS 可能很糟糕。
花生画廊的几个问题:
1) 如何通过 PHP 随机应用 CSS 伪类?
2)即使这有效,这里的 CSS 输出是否符合标准?
3)我是否应该担心这里的标准?
有兴趣听到任何回复 - 提前感谢您的宝贵时间。
The goal: to randomly assign different color CSS backgrounds to a Wordpress-generated tag cloud on a mouseover by the user.
My method (so far unsuccessful):
<?php
$colors = array( 0 => "#645676", "#427048", "#654335", "#ab2805", "#a59d57", "#302f2f", "#81510d" );
$tags = wp_tag_cloud('smallest=10&largest=10&unit=px&number=40&format=array&echo=0');
shuffle($tags);
foreach ($tags as $tag) {
shuffle($colors);
echo "<li style=\"a:hover=background:".$colors[0].";\"> $tag \n </li>";
}
?>
I figured a foreach PHP loop would be the easiest way to do this. I created two arrays ($colors and $tags). I manually filled $colors with the hex tags of the colors I want to use. $tags is populated by the wordpress tag cloud array (more info here if interested: http://codex.wordpress.org/Function_Reference/wp_tag_cloud) which I have set to grab the 40 most popular tags on this blog.
I then use the PHP shuffle function on $tags to mix up the top 40 tags, run those 40 tags through a foreach loop which then randomly assigns one of the shuffled $colors to a CSS pseudo class which gets echoed out.
The code works, here is a sample of the output:
<li style="a:hover=background:#302f2f;"> <a href='http://localhost:8888/nutritionwonderland_2/tag/truvia/' class='tag-link-49' title='3 topics' style='font-size: 10px;'>truvia</a>
</li><li style="a:hover=background:#81510d;"> <a href='http://localhost:8888/nutritionwonderland_2/tag/cancer/' class='tag-link-8' title='11 topics' style='font-size: 10px;'>cancer</a>
</li><li style="a:hover=background:#427048;"> <a href='http://localhost:8888/nutritionwonderland_2/tag/antioxidants/' class='tag-link-93' title='4 topics' style='font-size: 10px;'>antioxidants</a>
</li>
The problem: when I mouseover any of the tags, the background never shows up. This makes me think the CSS may be bad.
A couple of questions for the peanut gallery:
1) How do you apply CSS pseudo classes randomly via PHP?
2) Even if this worked, would the CSS output here by standards compliant?
3) Should I even worry about standards here?
Interested to hear any responses - thanks in advance for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先跳出来的东西。如果您只是更改背景颜色而不更改其余与背景相关的项目,则最好使用背景颜色作为要更改的目标元素。
其次,我可能会在样式表中渲染 css,即使是随机化的,并为每个 a 标签提供适当的类
和
first thing that jumps out. if you're just changing the background-color an dnot the rest of the background-associated items, you might be better off using background-color as your targeted element to change.
second, I would probably render the css out in the stylesheet, even with your randomization, and give each of the a tags the appropriate class
and
如何在内联CSS中编写a:hover?
How to write a:hover in inline CSS?
我想你需要 javascript 来完成这个。 PHP 是服务器端的,因此您无法在鼠标悬停时选择随机颜色。我可能完全误解了这个问题;我假设您不想在页面加载时简单地选择随机颜色并在每次鼠标悬停时显示相同的颜色。
I think you'll need javascript for this one. PHP is server-side, so you couldn't pick a random color on mouse over. I may have completely misinterpreted the question; I assume that you don't want to simply pick a random color on page load and display that same color on every mouse over.