html中不同背景使用同一张png图片

发布时间:2011年1月19日 作者:未知 查看次数:2251

html中不同背景使用同一张png图片


html中不同背景使用同一张png图片(一张背景图片的不同切换)

<html>

<head>
<meta http-equiv="Content-Type"
content="text/html; charset=gb_2312-80">
<title>一张背景图片的不同切换</title>
</head>

<body>

<div  style=" width:50px; height:38px;background-image:url(http://127.0.0.1/143439085.p.png)" onmouseover="touch(this,52,0)" onMouseOut="restore(this)" index=0></div>
<div  style=" width:50px; height:38px;background-image:url(http://127.0.0.1/143439085.p.png); background-position:0px -35" onmouseover="touch(this,52,-35)" onMouseOut="restore(this)" index=1></div>
<div  style=" width:50px; height:38px;background-image:url(http://127.0.0.1/143439085.p.png); background-position:0px -70" onmouseover="touch(this,52,-70)" onMouseOut="restore(this)" index=2></div>
<div  style=" width:50px; height:38px;background-image:url(http://127.0.0.1/143439085.p.png); background-position:0px -107" onmouseover="touch(this,52,-107)" onMouseOut="restore(this)" index=3></div>


<script>
var timer=[null,null,null,null]
var g=[0,0,0,0]
var sty=[new Array(),new Array(),new Array(),new Array()] 
// http://www.knowsky.com/ 记录图片位置的数组

function touch(obj,a1,a2){
var f=0
for(var i=0;i<5;i++)
{
sty[obj.index][i]="-"+f+"px"+" "+a2+"px";
f=f+a1
}                        //循环得出图片的位置
change(obj)
}

function change(obj){
clearTimeout(timer[obj.index])             //=======
if(g[obj.index]==sty[obj.index].length-1)
{
obj.style.backgroundPosition=sty[obj.index][g[obj.index]];
timer[obj.index]=setTimeout(function(){change(obj)},100)
}
else
{
obj.style.backgroundPosition=sty[obj.index][g[obj.index]]
g[obj.index]++
timer[obj.index]=setTimeout(function(){change(obj)},100)
}
}

function restore(obj){
clearTimeout(timer[obj.index])        //================
if(g[obj.index]<1)
{clearTimeout(timer[obj.index]);
obj.style.backgroundPosition=sty[obj.index][0]
}
else
{
obj.style.backgroundPosition=sty[obj.index][g[obj.index]]
g[obj.index]--
timer[obj.index]=setTimeout(function(){restore(obj)},100)
}
}
</script>

 

</body>
</html>



版权所有!www.sieye.cn
E.Mail:sieye@sohu.com QQ:66697110