html中图片如何自动切换?

html中图片如何自动切换?,第1张

\x0d\x0avar curIndex=0\x0d\x0a//时间间隔(单位毫秒),每秒钟显示一张,数组共有5张图片放在Photos文件夹下。\x0d\x0avar timeInterval=1000\x0d\x0avar arr=new Array()\x0d\x0aarr[0]="photosǗ.jpg"\x0d\x0aarr[1]="photosǘ.jpg"\x0d\x0aarr[2]="photosǙ.jpg"\x0d\x0aarr[3]="photosǚ.jpg"\x0d\x0aarr[4]="photosǛ.jpg"\x0d\x0asetInterval(changeImg,timeInterval)\x0d\x0afunction changeImg()\x0d\x0a{\x0d\x0avar obj=document.getElementById("obj")\x0d\x0aif (curIndex==arr.length-1) \x0d\x0a{\x0d\x0acurIndex=0\x0d\x0a}\x0d\x0aelse\x0d\x0a{\x0d\x0acurIndex+=1\x0d\x0a}\x0d\x0aobj.src=arr[curIndex]\x0d\x0a}\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a自己修改一下就可以用了

一般是用脚本实现。示例:<!doctype html><html><head><meta charset="utf-8"><title>无标题文档</title><style type="text/css">#bg_body{background:url(1.jpg) no-repeat center #eee background-attachment:fixed height:1000px}</style><script type="text/javascript">window.onload=function(){var oBody=document.getElementById('bg_body') var oS=oBody.style function BgChenge(){oS.backgroundImage='url(2.jpg)' } function BgPosition(){oS.backgroundRepeat='no-repeat' oS.backgroundPosition='center' oS.backgroundAttachment='fixed' } function LoadMethod(){BgChenge() BgPosition()} setInterval(LoadMethod,1000)}</script></head><body id="bg_body"></body></html>你背景图片就设置了一个2.jpg再切换也是这一张图片啊如果有多张图片比如说1-10.jpg,那就把oS.backgroundImage='url(2.jpg)'改为oS.backgroundImage='url('+(parseInt(Math.random() * 10) + 1)+'.jpg)'

方法一:在html文件中设置

html的中有两个关于背景的属性,其中的background用来设置背景图片。示例如下:

如果背景图片小于网页显示窗口,那么这个背景图片会自动重复。

为保证浏览器载入网页的速度,建议尽量不要使用字节过大的图片作为背景图片。

可以参考这个网站的教程和示例:

http://www.blabla.cn/html_tutorials/045_html_bgcolor_background.html

方法二:在css文件中设置

css的背景图片属性

示例:

可以参考这个网站的教程和示例:

http://www.blabla.cn/css_tutorials/020_css_background.html

参考资料:

http://www.blabla.cn/html_tutorials/index.html


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

原文地址:https://54852.com/zaji/8282652.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存