(function($){ $.fn.slide=function(options){ $.fn.slide.defaults={ effect:"fade", autoplay:false, delaytime:500, intertime:2500, triggertime:150, defaultindex:0, titcell:".hd li", maincell:".bd", targetcell:null, trigger:"mouseover", scroll:1, vis:1, titonclassname:"on", autopage:false, prevcell:".prev", nextcell:".next", pagestatecell:".pagestate", opp: false, pnloop:true, easing:"linear", startfun:null, endfun:null, switchload:null }; return this.each(function() { var opts = $.extend({},$.fn.slide.defaults,options); var effect = opts.effect; var prevbtn = $(opts.prevcell, $(this)); var nextbtn = $(opts.nextcell, $(this)); var pagestate = $(opts.pagestatecell, $(this)); var navobj = $(opts.titcell, $(this));//导航子元素结合 var navobjsize = navobj.size(); var conbox = $(opts.maincell , $(this));//内容元素父层对象 var conboxsize=conbox.children().size(); var sload=opts.switchload; if(opts.targetcell!=null){ var tarobj = $(opts.targetcell, $(this)) }; /*字符串转换为数字*/ var index=parseint(opts.defaultindex); var delaytime=parseint(opts.delaytime); var intertime=parseint(opts.intertime); var triggertime=parseint(opts.triggertime); var scroll=parseint(opts.scroll); var vis=parseint(opts.vis); var autoplay = (opts.autoplay=="false"||opts.autoplay==false)?false:true; var opp = (opts.opp=="false"||opts.opp==false)?false:true; var autopage = (opts.autopage=="false"||opts.autopage==false)?false:true; var loop = (opts.pnloop=="false"||opts.pnloop==false)?false:true; var slideh=0; var slidew=0; var selfw=0; var selfh=0; var easing=opts.easing; var inter=null;//setinterval名称 var oldindex = index; //处理分页 if( navobjsize==0 )navobjsize=conboxsize; if( autopage ){ var temps = conboxsize-vis; navobjsize=1+parseint(temps%scroll!=0?(temps/scroll+1):(temps/scroll)); if(navobjsize<=0)navobjsize=1; navobj.html(""); for( var i=0; i"+(i+1)+"") } var navobj = $("li", navobj);//重置导航子元素对象 } conbox.children().each(function(){ //取最大值 if( $(this).width()>selfw ){ selfw=$(this).width(); slidew=$(this).outerwidth(true); } if( $(this).height()>selfh ){ selfh=$(this).height(); slideh=$(this).outerheight(true); } }); if(conboxsize>=vis){ //当内容个数少于可视个数,不执行效果。 switch(effect) { case "fold": conbox.css({"position":"relative","width":slidew,"height":slideh}).children().css( {"position":"absolute","width":selfw,"left":0,"top":0,"display":"none"} ); break; case "top": conbox.wrap('
').css( { "position":"relative","padding":"0","margin":"0"}).children().css( {"height":selfh} ); break; case "left": conbox.wrap('
').css( { "width":conboxsize*slidew,"position":"relative","overflow":"hidden","padding":"0","margin":"0"}).children().css( {"float":"left","width":selfw} ); break; case "leftloop": case "leftmarquee": conbox.children().clone().appendto(conbox).clone().prependto(conbox); conbox.wrap('
').css( { "width":conboxsize*slidew*3,"position":"relative","overflow":"hidden","padding":"0","margin":"0","left":-conboxsize*slidew}).children().css( {"float":"left","width":selfw} ); break; case "toploop": case "topmarquee": conbox.children().clone().appendto(conbox).clone().prependto(conbox); conbox.wrap('
').css( { "height":conboxsize*slideh*3,"position":"relative","padding":"0","margin":"0","top":-conboxsize*slideh}).children().css( {"height":selfh} ); break; } } var dostartfun=function(){ if ( $.isfunction( opts.startfun) ){ opts.startfun( index,navobjsize ); } }; var doendfun=function(){ if ( $.isfunction( opts.endfun ) ){ opts.endfun( index,navobjsize ); } }; var doswitchload=function(objs){ objs.eq(index).find("img").each(function(){ if ( typeof($(this).attr(sload))!="undefined"){ $(this).attr("src",$(this).attr(sload)).removeattr(sload) } }) } //效果函数 var doplay=function(isfirst){ if( oldindex==index && !isfirst && effect!="leftmarquee" && effect!="topmarquee" ) return; // 当前页状态不触发效果 switch(effect) { case "fade": case "fold": case "top": case "left": if ( index >= navobjsize) { index = 0; } else if( index < 0) { index = navobjsize-1; } break; case "leftmarquee":case "topmarquee": if ( index>= 1) { index=1; } else if( index<=0) { index = 0; } break; case "leftloop": case "toploop": var tempnum = index - oldindex; if( navobjsize>2 && tempnum==-(navobjsize-1) ) tempnum=1; if( navobjsize>2 && tempnum==(navobjsize-1) ) tempnum=-1; var scrollnum = math.abs( tempnum*scroll ); if ( index >= navobjsize) { index = 0; } else if( index < 0) { index = navobjsize-1; } break; } dostartfun(); //处理切换加载 if( sload!=null ){ doswitchload( conbox.children() ) } //处理targetcell if(tarobj){ if( sload!=null ){ doswitchload( tarobj ) } tarobj.hide().eq(index).animate({opacity:"show"},delaytime,function(){ if(!conbox[0])doendfun() }); } if(conboxsize>=vis){ //当内容个数少于可视个数,不执行效果。 switch (effect) { case "fade":conbox.children().stop(true,true).eq(index).animate({opacity:"show"},delaytime,easing,function(){doendfun()}).siblings().hide(); break; case "fold":conbox.children().stop(true,true).eq(index).animate({opacity:"show"},delaytime,easing,function(){doendfun()}).siblings().animate({opacity:"hide"},delaytime,easing);break; case "top":conbox.stop(true,false).animate({"top":-index*scroll*slideh},delaytime,easing,function(){doendfun()});break; case "left":conbox.stop(true,false).animate({"left":-index*scroll*slidew},delaytime,easing,function(){doendfun()});break; case "leftloop": if(tempnum<0 ){ conbox.stop(true,true).animate({"left":-(conboxsize-scrollnum )*slidew},delaytime,easing,function(){ for(var i=0;i= 0){ for(var i=0;i= 0){ for(var i=0;i"+(index+1)+"/"+navobjsize); }; //初始化执行 doplay(true); //自动播放 if (autoplay) { if( effect=="leftmarquee" || effect=="topmarquee" ){ if(opp){ index-- }else{ index++ } inter = setinterval(doplay, intertime); conbox.hover(function(){if(autoplay){clearinterval(inter); }},function(){if(autoplay){clearinterval(inter);inter = setinterval(doplay, intertime);}}); }else{ inter=setinterval(function(){ if(opp){ index-- }else{ index++ } ; doplay() }, intertime); $(this).hover(function(){if(autoplay){clearinterval(inter); }},function(){if(autoplay){clearinterval(inter); inter=setinterval(function(){if(opp){ index-- }else{ index++ }; doplay() }, intertime); }}); } } //鼠标事件 var mst; if(opts.trigger=="mouseover"){ navobj.hover(function(){ index=navobj.index(this); mst = window.settimeout(doplay,opts.triggertime); }, function(){ cleartimeout(mst); }); }else{ navobj.click(function(){index=navobj.index(this); doplay(); }) } nextbtn.click(function(){ if ( loop==true || index!=navobjsize-1 ){ index++; doplay(); } }); prevbtn.click(function(){ if ( loop==true || index!=0 ){ index--; doplay(); } }); });//each end };//slide end })(jquery);