效果演示:源代码: 复制代码 代码如下: Single images <BR>var MoveBKimg=new Class({ <BR>initialize:function(){ <BR>this.$L=0; <BR>this.$T=0; <BR>}, <BR>Todo:function(i,opt){ <BR>this.opt={ <BR>bw:opt.bw || 0, //容器宽度 <BR>bh:opt.bh || 0, <BR>iw:opt.iw || 0, //图像宽度 <BR>ih:opt.ih || 0, <BR>X:opt.X || 0, //鼠标的clientX坐标 <BR>Y:opt.Y || 0 <BR>};o=this.opt; <BR>if(!i || !o.iw || !o.ih || !o.bw || !o.bh){return false;} <BR>o.iw=o.iw-o.bw; //图像实际可被移动的x轴范围 <BR>o.ih=o.ih-o.bh; <BR>var P=i.getStyle('background-position');P=P.split(" ");//取得目前背景图的position值,并拆分转为Int型 <BR>P[0]=P[0].toInt();P[0]=(P[0].abs()>o.iw)?((P[0]<0)?-o.iw:o.iw):P[0];//背景图目前的X偏移,并对值进行2次修正 <BR>P[1]=P[1].toInt();P[1]=(P[1].abs()>o.ih)?((P[1]<0)?-o.ih:o.ih):P[1]; <BR>console.log(o.X+'|'+o.Y); <BR>if(o.X>this.$L){//往右移鼠标图片往左跑 <BR> this.$L=o.X; <BR>P[0]=(o.iw<(P[0]-10))?o.iw:(P[0]-10); <BR>} <BR>if(o.X<this.$L){//往左移鼠标图片往右跑 <BR> this.$L=o.X; <BR>P[0]=((P[0]+10)>0)?0:(P[0]+10); <BR>} <BR>if(o.Y>this.$T){//往下移鼠标图片往上跑 <BR> this.$T=o.Y; <BR>P[1]=(o.ih<(P[1]-10))?o.ih:(P[1]-10); <BR>} <BR>if(o.Y<this.$T){//往上移鼠标图片往下跑 <BR> this.$T=o.Y; <BR>P[1]=((P[1]+10)>0)?0:(P[1]+10); <BR>} <BR>i.setStyle('background-position',''+P[0]+'px '+P[1]+'px');//重新给背景图的position赋值 <BR>} <BR>}); <BR>El=$('bsfdimg'); <BR>var MoveBKimg=new MoveBKimg(); <BR>El.addEvent('mousemove',function(event){ <BR>MoveBKimg.Todo(El,{bw:260,bh:200,iw:392,ih:600,X:event.client.x,Y:event.client.y}); <BR>}) <BR>