(function($){ $.fn.rtabs = function(options){ //默认值 var defaultval = { btnclass:'.j-tab-nav', /*按钮的父级class*/ conclass:'.j-tab-con', /*内容的父级class*/ bind:'hover', /*事件参数 click,hover*/ animation:'0', /*动画方向 left,up,fadein,0 为无动画*/ speed:300, /*动画运动速度*/ delay:200, /*tab延迟速度*/ auto:false, /*是否开启自动运行 true,false*/ autospeed:3000 /*自动运行速度*/ }; //全局变量 var obj = $.extend(defaultval, options), evt = obj.bind, btn = $(this).find(obj.btnclass), con = $(this).find(obj.conclass), anim = obj.animation, conwidth = con.width(), conheight = con.height(), len = con.children().length, sw = len * conwidth, sh = len * conheight, i = 0, len,t,timer; return this.each(function(){ //判断动画方向 function judgeanim(){ var w = i * conwidth, h = i * conheight; btn.children().removeclass('current').eq(i).addclass('current'); switch(anim){ case '0': con.children().hide().eq(i).show(); break; case 'left': con.css({position:'absolute',width:sw}).children().css({float:'left',display:'block'}).end().stop().animate({left:-w},obj.speed); break; case 'up': con.css({position:'absolute',height:sh}).children().css({display:'block'}).end().stop().animate({top:-h},obj.speed); break; case 'fadein': con.children().hide().eq(i).fadein(); break; } } //判断事件类型 if(evt == "hover"){ btn.children().hover(function(){ var j = $(this).index(); function s(){ i = j; judgeanim(); } timer=settimeout(s,obj.delay); }, function(){ cleartimeout(timer); }) }else{ btn.children().bind(evt,function(){ i = $(this).index(); judgeanim(); }) } //自动运行 function startrun(){ t = setinterval(function(){ i++; if(i>=len){ switch(anim){ case 'left': con.stop().css({left:conwidth}); break; case 'up': con.stop().css({top:conheight}); } i=0; } judgeanim(); },obj.autospeed) } //如果自动运行开启,调用自动运行函数 if(obj.auto){ $(this).hover(function(){ clearinterval(t); },function(){ startrun(); }) startrun(); } }) } })(jquery);