这很简单,但我不是程序员。我只是用一般的解决问题的技巧蛮力代码。这不是工作。该代码被设置为随机化背景图像,并且它可以工作。但是,它已经被设置为等待页面完全加载,所以它的时间太不一致了。据我了解,document.ready会给我更一致(更快)的时间,但我似乎无法使它工作。这里的工作版本:准备好随机背景
<script type="text/javascript">
function changeImg(imgNumber) {
var myImages = [
"http://static.tumblr.com/0obftwk/u0Am8xfjf/streetarturbaninphilade.jpg",
"http://static.tumblr.com/0obftwk/Xmym8xfet/artmuseumarea.jpg",
"http://static.tumblr.com/0obftwk/znNm8xf9e/rowhouseswestphilly.jpg",
"http://static.tumblr.com/0obftwk/af1m8xf87/phillyvista.jpg",
"http://static.tumblr.com/0obftwk/ydIm8xf74/chinagatephiladelphia.jpg",
"http://static.tumblr.com/0obftwk/8kCm8xf41/broadritner.jpg"
];
var imgShown = document.body.style.backgroundImage;
var newImgNumber =Math.floor(Math.random()*myImages.length);
document.body.style.backgroundImage = 'url('+myImages[newImgNumber]+')';
}
window.onload=changeImg;
</script>
你可能想'document.onl oad'而不是'window.onload' – Endophage
也许你可以在body标签后面插入内联,并立即调用changeImg(),而不是将它附加到发生(太)迟的事件处理程序。酷图像,顺便说一句。 – chelmerich
谢谢,Endophage,但只是将'窗口'更改为'文档'不起作用。也许你的建议比我解释的还要多。 // Chelmerisch,首先,这些图片是“我正在建造'关于费城的(基于Tumblr的)网站的”Lorem ipsum“。他们不是我的,当我有我自己合适的图像时,我会替换他们。至于你的建议,我明白了这个概念,但我不确定我会如何执行它。在我发布之前,我尝试删除onLoad,但脚本完全停止工作。再次,我不是程序员,也没有背景知识来理解为什么。 – user1593984