JSプロトタイプを使用した画像スクロールクラス
3531 ワード
var counter={};// ,
var indexSlider=function(settings){
this.settings=settings;
this.init();//
};
indexSlider.prototype.init=function(){//
var _this=this;
if(_this.settings.ID===undefined){
throw new Error(' , ID');
}
_this.imgCounts=$('#'+_this.settings.ID).find('.sideImages').children('ul').children('li').length;
if(_this.imgCounts>_this.settings.maxImages){
throw new Error(' , ');
}
if(_this.imgCounts<=_this.settings.maxImages){
_this.settings.maxImages=_this.imgCounts;
}
initThum(_this.settings.ID)
_this.current=0;
$('#'+_this.settings.ID).find('.sideControl').find('.currentShow').css({left:_this.settings.baseWidth});
/**------ ------**/
$('#'+_this.settings.ID).find('.sideControl').children('span').bind('click',function(event){
//event.preventDefault();
var _that=$(this);
_this.goTo(_that.attr('rel'));
});
/**------ ------**/
_this.autoSlide();
}
indexSlider.prototype.autoSlide=function(){//
var _this=this;
var _speed=_this.settings.speed;
counter.time1=setInterval(function(){_this.current++;if(_this.current>=_this.settings.maxImages){_this.current=0;}_this.sideTo(_this.current)},_speed);
}
indexSlider.prototype.sideTo=function(index){//
if(index===null || index===undefined){
throw new Error(' ');
}
var _this=this;
_left=index*_this.settings.step+_this.settings.baseWidth*(index*2+1);
$('#'+_this.settings.ID).find('.sideControl').find('.currentShow').animate({left:_left},_this.settings.slideSpeed);
_this.slideImg(index);
}
indexSlider.prototype.slideImg=function(index){//
if(index===null || index===undefined){
throw new Error(' ');
}
var _this=this;
_left=-index*_this.settings.imgStep;
$('#'+_this.settings.ID).find('.sideImages').find('ul').animate({left:_left},_this.settings.slideSpeed);
}
indexSlider.prototype.goTo=function(index){//
if(index===null || index===undefined){
throw new Error(' ');
}
var _this=this;
if(index<0 || index>_this.settings.maxImages){
throw new Error(' ');
}
if(counter.time1){
clearInterval(counter.time1);
}
_this.sideTo(index);
_this.current=index;
_this.autoSlide();
}
var initThum=function(ID){//
var _bigImgHandle=$('#'+ID).find('.sideImages').children('ul').children('li');
var _len=_bigImgHandle.length;
for(var i=0;i<_len;i++){
_src=_bigImgHandle.eq(i).find('img').attr('src');
$('#'+ID).find('.sideControl').append('<span rel="'+i+'"><img src="'+getThumByBigImage(_src)+'" width="124" height="60"/></span>')
}
return true;
}
var getThumByBigImage=function(bigImageSrc){//
if(bigImageSrc==='' || bigImageSrc===undefined){
throw new Error(' ');
}
var _arr1=bigImageSrc.split('/');
var _len=_arr1.length;
var _image=_arr1[_len-1];
var _arr2=_image.split('.');
var _thumImage=_arr2[0]+'_x.'+_arr2[1];
var _thum='';
for(var i=0;i<_len;i++){
if(i<_len-1){
_thum+=_arr1[i]+'/'
}
}
_thum+=_thumImage;
return _thum;
}