javascript+css一张背景图片的不同切换

方与圆之父

方与圆之父

2016-02-19 14:14

有一种朋友不在生活里,却在生命力;有一种陪伴不在身边,却在心间。图老师即在大家的生活中又在身边。这么贴心的服务你感受到了吗?话不多说下面就和大家分享javascript+css一张背景图片的不同切换吧。

div  style=" width:50px; height:38px;background-image:url(http://album.hi.csdn.net/app_uploads/wtcsy/20081108/143439085.p.png)" onmouseover="touch(this,52,0)" onMouseOut="restore(this)" index=0/div
div  style=" width:50px; height:38px;background-image:url(http://album.hi.csdn.net/app_uploads/wtcsy/20081108/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://album.hi.csdn.net/app_uploads/wtcsy/20081108/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://album.hi.csdn.net/app_uploads/wtcsy/20081108/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;i5;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)
}
}

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/webkaifa/)

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

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/webkaifa/)
展开更多 50%)
分享

猜你喜欢

javascript+css一张背景图片的不同切换

Web开发
javascript+css一张背景图片的不同切换

css 背景图片的定位问题

Web开发
css 背景图片的定位问题

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

PPT用宏插入不同背景图片

电脑入门
PPT用宏插入不同背景图片

网页CSS背景图片使用的测试结果

Web开发
网页CSS背景图片使用的测试结果

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

CSS基础教程: CSS的Background Images 背景图片

Web开发
CSS基础教程: CSS的Background Images 背景图片

PPT中用宏快速插入不同背景图片

电脑入门
PPT中用宏快速插入不同背景图片

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

使用JQUERY Tabs插件宿主IFRAMES

使用JQUERY Tabs插件宿主IFRAMES

Struts开发指南之MVC架构

Struts开发指南之MVC架构
下拉加载更多内容 ↓