示例:
<!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>
如果有多张图片比如说1-10.jpg,那就把oS.backgroundImage='url(2.jpg)'改为oS.backgroundImage='url('+(parseInt(Math.random() * 10) + 1)+'.jpg)'
使用JavaScript实现:<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />
<title>无标题文档</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<style type="text/css">
*{margin:0pxpadding:0px}
#web_bg{background-image:url(jietu/bxbg.png)border:1px solid redwidth:700pxheight:700px}
</style>
<script type="text/javascript">
let bgs = ["bxbg.png","bxbg1.png","bxbg2.png","bxbg3.png","bxbg4.png"]
window.onload=function(){
//$("#web_bg").css({"backgroundColor":"jietu/" + bgs[parseInt(Math.random()*5)]})
$("#web_bg").css({"backgroundImage":"url(jietu/" + bgs[parseInt(Math.random()*5)] + ")"})
}
</script>
</head>
<body>
<div id="web_bg" style="">
</div>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)