点击(此处)折叠或打开

  1. <!DOCTYPE html>
  2. <html>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>jQuery - rTabs选项卡插件</title>
  5. <head>
  6. <script id="jquery_172" type="text/javascript" class="library" src="http://www.codefans.net/ajaxjs/jquery-1.6.2.min.js"></script>
  7. <script type="text/javascript">
  8. (function($){
  9.     $.fn.rTabs = function(options){
  10.         //默认值
  11.         var defaultVal = {
  12.         btnClass:'.j-tab-nav',    /*按钮的父级Class*/
  13.         conClass:'.j-tab-con',    /*内容的父级Class*/
  14.         bind:'hover',    /*事件参数 click,hover*/
  15.         animation:'0',    /*动画方向 left,up,fadein,0 为无动画*/
  16.         speed:300,     /*动画运动速度*/
  17.         delay:200,    /*Tab延迟速度*/
  18.         auto:true,    /*是否开启自动运行 true,false*/
  19.         autoSpeed:3000    /*自动运行速度*/
  20.         };
  21.         //全局变量
  22.         var obj = $.extend(defaultVal, options),
  23.         evt = obj.bind,
  24.         btn = $(this).find(obj.btnClass),
  25.         con = $(this).find(obj.conClass),
  26.         anim = obj.animation,
  27.         conWidth = con.width(),
  28.         conHeight = con.height(),
  29.         len = con.children().length,
  30.         sw = len * conWidth,
  31.         sh = len * conHeight,
  32.         i = 0,
  33.         len,t,timer;
  34.         return this.each(function(){
  35.         //判断动画方向
  36.         function judgeAnim(){
  37.             var w = i * conWidth,
  38.             h = i * conHeight;
  39.         btn.children().removeClass('current').eq(i).addClass('current');
  40.             switch(anim){
  41.             case '0':
  42.             con.children().hide().eq(i).show();
  43.             break;
  44.             case 'left':
  45.             con.css({position:'absolute',width:sw}).children().css({float:'left',display:'block'}).end().stop().animate({left:-w},obj.speed);
  46.             break;
  47.             case 'up':
  48.             con.css({position:'absolute',height:sh}).children().css({display:'block'}).end().stop().animate({top:-h},obj.speed);
  49.             break;
  50.             case 'fadein':
  51.             con.children().hide().eq(i).fadeIn();
  52.             break;
  53.             }
  54.         }
  55.         //判断事件类型
  56.         if(evt == "hover"){
  57.             btn.children().hover(function(){
  58.                 var j = $(this).index();
  59.                 function s(){
  60.                 i = j;
  61.                 judgeAnim();
  62.                 }
  63.                 timer=setTimeout(s,obj.delay);
  64.                 }, function(){
  65.                     clearTimeout(timer);
  66.                 })
  67.             }else{
  68.                 btn.children().bind(evt,function(){
  69.                     i = $(this).index();
  70.                     judgeAnim();
  71.                 })
  72.             }
  73.             //自动运行
  74.             function startRun(){
  75.                 t = setInterval(function(){
  76.                     i++;
  77.                     if(i>=len){
  78.                         switch(anim){
  79.                             case 'left':
  80.                             con.stop().css({left:conWidth});
  81.                             break;
  82.                             case 'up':
  83.                             con.stop().css({top:conHeight});
  84.                         }    
  85.                         i=0;
  86.                     }
  87.                     judgeAnim();
  88.                 },obj.autoSpeed)
  89.             }
  90.             //如果自动运行开启,调用自动运行函数
  91.             if(obj.auto){
  92.                 $(this).hover(function(){
  93.                     clearInterval(t);
  94.                 },function(){
  95.                     startRun();
  96.                 })
  97.                 startRun();
  98.             }
  99.         })
  100.     }
  101. })(jQuery);
  102. </script>
  103. <script type="text/javascript">
  104.             $(function() {
  105.                 $("#tab").rTabs();
  106.                 $("#tab2").rTabs({
  107.                     bind : 'click',
  108.                     animation : 'left'
  109.                 });
  110.                 $("#tab3").rTabs({
  111.                     bind : 'hover',
  112.                     animation : 'up'
  113.                 });
  114.                 $("#tab4").rTabs({
  115.                     bind : 'hover',
  116.                     animation : 'fadein'
  117.                 });
  118.             })
  119.         </script>
  120. <style>
  121. body{background:#fff;}h2{width: 400px;margin: 0 auto 10px auto;font-size: 18px;font-family: "微软雅黑";color: #333;}.tab{position: relative;width: 400px;height: 230px;overflow: hidden;margin: 0 auto 20px auto;font-family: Arial;}.tab-nav{height: 30px;overflow: hidden;background: #f5f5f5;}.tab-nav a{display: block;float: left;width: 80px;height: 30px;line-height: 30px;text-align: center;text-decoration: none;color: #999;}.tab-nav a.current{background: #80b600;color: #fff;}.tab-con{position: relative;width: 400px;height: 200px;overflow: hidden;background: #80b600;}.tab-con-item{display: none;width: 400px;height: 180px;line-height: 180px;text-align: center;color: #fff;}
  122. </style>
  123. </head>
  124. <body>
  125. <h1>如果初次打开网页运行有错误看不到效果,请按F5或刷新网页重新载入即可。</h1></br>
  126. <h2>默认样式:自动运行、无动画效果、Hover事件</h2>
  127. <div class="tab" id="tab">
  128.     <div class="tab-nav j-tab-nav">
  129.         <a href="javascript:void(0);" class="current">Tab1</a>
  130.         <a href="javascript:void(0);">Tab2</a>
  131.         <a href="javascript:void(0);">Tab3</a>
  132.         <a href="javascript:void(0);">Tab4</a>
  133.         <a href="javascript:void(0);">Tab5</a>
  134.     </div>
  135.     <div class="tab-con">
  136.         <div class="j-tab-con">
  137.             <div class="tab-con-item" style="display:block;">111111</div>
  138.                     <div class="tab-con-item">222222</div>
  139.                     <div class="tab-con-item">333333</div>
  140.                     <div class="tab-con-item">444444</div>
  141.                     <div class="tab-con-item">555555</div>
  142.             </div>
  143.         </div>
  144.     </div>
  145. <h2>自动运行、向左滚动、点击事件</h2>
  146. <div class="tab" id="tab2">
  147.     <div class="tab-nav j-tab-nav">
  148.         <a href="javascript:void(0);" class="current">Tab1</a>
  149.         <a href="javascript:void(0);">Tab2</a>
  150.         <a href="javascript:void(0);">Tab3</a>
  151.         <a href="javascript:void(0);">Tab4</a>
  152.         <a href="javascript:void(0);">Tab5</a>
  153.     </div>
  154. <div class="tab-con">
  155. <div class="j-tab-con">
  156. <div class="tab-con-item" style="display:block;">111111</div>
  157. <div class="tab-con-item">222222</div>
  158. <div class="tab-con-item">333333</div>
  159. <div class="tab-con-item">444444</div>
  160. <div class="tab-con-item">555555</div>
  161. </div>
  162. </div>
  163. </div>
  164. <h2>自动运行、向上滚动、Hover事件</h2>
  165.     <div class="tab" id="tab3">
  166.         <div class="tab-nav j-tab-nav">
  167.             <a href="javascript:void(0);" class="current">Tab1</a>
  168.             <a href="javascript:void(0);">Tab2</a>
  169.             <a href="javascript:void(0);">Tab3</a>
  170.             <a href="javascript:void(0);">Tab4</a>
  171.             <a href="javascript:void(0);">Tab5</a>
  172.         </div>
  173.     <div class="tab-con">
  174.         <div class="j-tab-con">
  175.             <div class="tab-con-item" style="display:block;">111111</div>
  176.             <div class="tab-con-item">222222</div>
  177.             <div class="tab-con-item">333333</div>
  178.             <div class="tab-con-item">444444</div>
  179.             <div class="tab-con-item">555555</div>
  180.             </div>
  181.     </div>
  182. </div>
  183. <h2>自动运行、渐入、Hover事件</h2>
  184.         <div class="tab" id="tab4">
  185.             <div class="tab-nav j-tab-nav">
  186.                 <a href="javascript:void(0);" class="current">Tab1</a>
  187.                 <a href="javascript:void(0);">Tab2</a>
  188.                 <a href="javascript:void(0);">Tab3</a>
  189.                 <a href="javascript:void(0);">Tab4</a>
  190.                 <a href="javascript:void(0);">Tab5</a>
  191.             </div>
  192.             <div class="tab-con">
  193.                 <div class="j-tab-con">
  194.                 <div class="tab-con-item" style="display:block;">111111</div>
  195.                 <div class="tab-con-item">222222</div>
  196.                 <div class="tab-con-item">333333</div>
  197.                 <div class="tab-con-item">444444</div>
  198.                 <div class="tab-con-item">555555</div>
  199.             </div>
  200.         </div>
  201.     </div>
  202. </body>
  203. </html>

12-17 22:09