2个回答
2013-04-19
展开全部
给你举个简单的例子哈,你需要把var pic_arr=new Array("silkroad_1.jpg","silkroad_2.jpg","silkroad_3.jpg");这个里面存储的图片换成自己图片的路径就可以了=========================<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
#pic_div{
width:400px;
height:267px;
}
</style>
</head><body>
<div id="pic_div" style="background:url(silkroad_1.jpg) no-repeat;"></div>
<input type="button" value="切换背景图" onclick="test();" />
<script type="text/javascript">
var count=0;
var pic_arr=new Array("silkroad_1.jpg","silkroad_2.jpg","silkroad_3.jpg");
function test(){
var pic_div=document.getElementById("pic_div");
pic_div.style.background="url("+pic_arr[count+1]+") no-repeat";
count++;
if(count>=3)
{
count=0;
pic_div.style.background="url("+pic_arr[count]+") no-repeat";
}
}
</script>
</body>
</html>
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
#pic_div{
width:400px;
height:267px;
}
</style>
</head><body>
<div id="pic_div" style="background:url(silkroad_1.jpg) no-repeat;"></div>
<input type="button" value="切换背景图" onclick="test();" />
<script type="text/javascript">
var count=0;
var pic_arr=new Array("silkroad_1.jpg","silkroad_2.jpg","silkroad_3.jpg");
function test(){
var pic_div=document.getElementById("pic_div");
pic_div.style.background="url("+pic_arr[count+1]+") no-repeat";
count++;
if(count>=3)
{
count=0;
pic_div.style.background="url("+pic_arr[count]+") no-repeat";
}
}
</script>
</body>
</html>
展开全部
切换背景图,主要是修改背景的url:下面是一个小例子:
<body>
<div id="div1" style="width:100px; height:100px; background:"url(../1.png)"></div>
</body>
<script>
var oDiv = document.getElementById('div1');
oDiv.onclick = function(){
oDiv.style.background = 'url(../2.png)'; //直接切换背景色。
};
</script>
<body>
<div id="div1" style="width:100px; height:100px; background:"url(../1.png)"></div>
</body>
<script>
var oDiv = document.getElementById('div1');
oDiv.onclick = function(){
oDiv.style.background = 'url(../2.png)'; //直接切换背景色。
};
</script>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询