我必须使用外部 CSS 文件随机更改类的背景图片。 图像必须从定义路径中随机选择。
我知道如何在 php 中处理它,我必须在外部 css 文件中实现它。
寻找任何 css 技巧。
这是 css 类属性。
.birthday
{
background:url(../images/Bday.jpg) no-repeat center;
font: bold 25px "trebuchet ms", Tahoma, Verdana, Arial, Helvetica, sans-serif;
color: #930204;
padding: 25px 0px 0px 200px;
height: 195px;
}
路径根据文件结构给出。
最佳答案
使用纯 CSS
是不可能的。
您还可以使用 javascript 来随机设置您网站的背景图片。
请参阅本教程..
Impossibly simple image randomize with jQuery
或
试试这个方法
首先创建一个图像数组
var images = ['img1.jpg', 'img2.jpg', 'img3.jpg', 'img4.jpg', 'img5.jpg'];
现在,设置一个随机图像作为背景图像:
$('body').css({'background-image': 'url(images/' + images[Math.floor(Math.random() * images.length)] + ')'});
我希望这段代码对你有所帮助, 谢谢..
关于html - 通过外部 CSS 文件随机背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21754798/