JS实现 css加载随机背景图片

| | Comments (0)



JS实现 css加载随机背景图片,自己要的功能,在网上找了很多方法,比对完后,感觉此方法比较简单,经过调试后,一下代码可用,已经备注的很清楚了,不明白的想想逻辑,js自己也不懂,只是照抄修改优化了一下,。


<SCRIPT language=JavaScript>
bg = new Array(4); //设定图片数量,如果图片数为5,这个参数就设为4
bg[0] = 'url("/images/index_bg.jpg") repeat  scroll 220px center' //显示的图片路径,可用http://
bg[1] = 'url("/images/index_bg_1.jpg") repeat  scroll 220px center' //其中背景图片repeat 等css可按规设置
bg[2] = 'url("/images/index_bg_2.jpg") repeat  scroll 220px center'
bg[3] = 'url("/images/index_bg_3.jpg") repeat  scroll 220px center'
bg[4] = 'url("/images/index_bg_4.jpg") repeat  scroll 220px center'

index = Math.floor(Math.random() * bg.length);
document.body.style.background = ""+bg[index]+"";

</SCRIPT>

忙就到这里,结束。

单点日志:http://spoint.babyshoot.cn

Leave a comment


Type the characters you see in the picture above.

Archives

Ads by google

Pages

Powered by Movable Type 4.24-en

About this Entry

This page contains a single entry by 单点日志 published on August 29, 2011 11:14 AM.

CSS背景图片添加超链接 was the previous entry in this blog.

百度的广告展示方式终于改变了 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.