Javascript图片滚动
2010-09-14 13:23:57 来源:WEB开发网这是第二个类,是SlideShow的一页,主要有switchTo方法,负责转换的具体实现
varSlideItem=Class.create({
//slideShow:SlideShow实例
//btn:按钮
//index:按钮的索引
initialize:function(slideShow,btn,index){
this.slideShow=slideShow;
this.btn=btn;
this.index=index;
this.position=-index*(this.slideShow.options.updown?this.slideShow.height:this.slideShow.width);
varself=this;
this.btn.observe(this.slideShow.options.mouseType,function(){self.switchTo.apply(self,arguments)});
},
switchTo:function(){
clearInterval(this.slideShow.intervalId);
if(this.slideShow.options.auto)
clearTimeout(this.slideShow.autoPlayTimeout);
if(this.slideShow.options.btnBlurHandler)
this.slideShow.options.btnBlurHandler(this.slideShow.items[this.slideShow.currentIndex].btn);
this.slideShow.currentIndex=this.index;
if(this.slideShow.options.btnFocusHandler)
this.slideShow.options.btnFocusHandler(this.btn);
this.slideShow.intervalId=setInterval(setPosition,10);
varself=this;
functionsetPosition(){
varcurrentPosition=parseInt(self.slideShow.content.getStyle(self.slideShow.options.updown?"top":"left"));
vartargetPosition=self.position;
varstep=(targetPosition-currentPosition)/self.slideShow.options.bufferStep;
if(Math.abs(step)<1&&step!=0){
step=(targetPosition-currentPosition)>0?1:-1;
}
currentPosition+=Math.round(step);
if(self.slideShow.options.updown)
self.slideShow.content.setStyle({"top":currentPosition+"px"});
else
self.slideShow.content.setStyle({"left":currentPosition+"px"});
if(targetPosition==currentPosition){
clearInterval(self.slideShow.intervalId);
if(self.slideShow.options.auto)
self.slideShow.autoPlay();
}
}
}
});
注:缓动效果的实现借鉴了cloudgamer所用的方法。即根据初始值和目标值计算出每一步的步长,初始值离目标值越大步长越大,初始值离目标值越小步长越小,从而实现缓动
Tags:Javascript 图片 滚动
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接