
css+js实现背景图片按时间变换出错。
<!DOCTYPEhtml><html><head><metacharset="utf-8"><title></title><styletype="text/css">b...
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
body{
background: url(img/fstpage1.jpg);
}
</style>
<script type="text/javascript">
var imgs = ["fstpage1.jpg", "fstpage2.jpg", "fstpage3.jpg"];
var x = 0;
function time1() {
randomBgIndex = Math.round( Math.random() * 1 );
document.write('<style>body{background:url(img/' + imgs[x] +'</style>')
}
setInterval("time1()", 5000);
</script>
</head>
<body>
</body>
</html>
代码如上,新手刚学js。
这样设置后5秒背景图片就会变成空白,不知道是不是数组储存有问题,
还是重复写入'<style>body{background:url(img/' + imgs[x] +'</style>'导致了冲突。
第二个我删去了css部分,但打开直接为白色背景。求助。
三个图片都是存在的。
另外 var x = 0;
和里面的randomBgIndex = Math.round( Math.random() * 1 );
x就是randomBgIndex。。
我已经改了 但是不会生效 展开
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
body{
background: url(img/fstpage1.jpg);
}
</style>
<script type="text/javascript">
var imgs = ["fstpage1.jpg", "fstpage2.jpg", "fstpage3.jpg"];
var x = 0;
function time1() {
randomBgIndex = Math.round( Math.random() * 1 );
document.write('<style>body{background:url(img/' + imgs[x] +'</style>')
}
setInterval("time1()", 5000);
</script>
</head>
<body>
</body>
</html>
代码如上,新手刚学js。
这样设置后5秒背景图片就会变成空白,不知道是不是数组储存有问题,
还是重复写入'<style>body{background:url(img/' + imgs[x] +'</style>'导致了冲突。
第二个我删去了css部分,但打开直接为白色背景。求助。
三个图片都是存在的。
另外 var x = 0;
和里面的randomBgIndex = Math.round( Math.random() * 1 );
x就是randomBgIndex。。
我已经改了 但是不会生效 展开
展开全部
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
body{
background: url(img/fstpage1.jpg);
}
</style>
<script type="text/javascript">
var imgs=["fstpage1.jpg","fstpage2.jpg","fstpage3.jpg"];
setInterval(function(){
document.getElementsByTagName("body")[0].style.backgroundImage="url(img/"+imgs[parseInt(Math.random()*3)]+")";
},5000);
</script>
</head>
<body>
</body>
</html>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |