用JS实现随机显示N张图片

用JS实现随机显示N张图片,第1张

<script type="text/javascript">

function existDuo(a, t){

for(var i in a)

if(a[i]==t)

return true

return false

}

var a=[]

while(a.length<6){

var t=Math.floor(Math.random()*50+1)

if(!existDuo(a,t)) a.push(t)

}

for(var i in a)

document.write('<li><img src="'+a[i]+'.jpg"></li>\n')

</script>

var arr=["1.jpg","2.jpg","3.jpg","4.jpg".......]

var oImg=Math.floor(Math.random()*arr.length) //随机图

img.src="./img/"+arr[oImg] //img元素得到img文件夹下面的某张图片

你可以先加入以下的 function: function randomBG() { var bgArray = new Array("a"

"b"

"c") 先设定阵列bgArray,括号里面打番背景图片的名称,每个用引号引住,用逗号分隔 var i = Math.floor(Math.random() * 3) 设定一个随机乱数i,注意 Math.random() * 后面的数字是图片总数 document.body.background = bgArray[i] 设定背景为bgArray 阵列中的第 i 项 } 然后在 body onLoad="randomBG()" 即成 范例: rapidshare/files/46316444/Template.zip 2007-08-01 20:20:09 补充: 若要加在 Xanga

先到 Edit - Look and Feel找到 Website Stats (在最底)

在下面打番:script (前有 但我不打因为会 Show 唔到后面D 野)var bgArray = new Array("a"

"b"

"c")var i = Math.floor(Math.random() * 3)document.body.background = bgArray[i] /script 2007-08-01 20:24:25 补充: Blogger:右上角customize

再按 Edit HTML

就可以自己插入javascript. 2007-08-01 20:41:20 补充: But Wordpress does not allow user to add Javascripts

so the above is not practical.See what Wordpress saysfaq.wordpress/2006/05/07/javascript-can-i-use-that-on-my-blog/Sorry for that


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11228521.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-14
下一篇2023-05-14

发表评论

登录后才能评论

评论列表(0条)

    保存