封装jquery插件很简单

其实,封装jquery插件很简单。
先看看,封装模板是什么?看下这个light-weight模式:

   
   
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
         propertyName  :  "value"
     }
     //
     function  Plugin ( element , options ) {
         //domjq$(element)
         this . element  =  element ;
         //
         this . options  =  $ . extend ({ } , defaults , options ) ;
         //
         this . _defaults  =  defaults ;
         //
         this . _name  =  pluginName ;
         //
         this . init ( ) ;
     }
     Plugin . prototype . init  =   function ( ) {
         //
     }
    
     //fnprototype
     $ . fn [ pluginName ]  =  function ( options ) {
         //eachreturn this
         return  this . each ( function ( ) {
              // if new
              if ( !$ . data ( this , 'plugin_' + pluginName )) {
                   //
                   $ . data ( this , 'plugin_' + pluginName , new  Plugin ( this , options )) ;
              }
         }) ;
     }
}) ( jQuery , window , document ) ; //

第一步 ,先写出简单demo,这里不写了,直接找一个吧。这里以一个tab选项卡为例
http://www.cnblogs.com/web-xiaobai/archive/2012/09/17/2689067.html
运行效果如下:

   
   
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
< script   type = "text/javascript" >
//http://www.cnblogs.com/web-xiaobai/archive/2012/09/17/2689067.html
$ ( function ( ) {
     $ ( document ) . ready ( function ( ) {
         $ ( ".tab li" ) . click ( function ( ) {
              $ ( ".tab li" ) . eq ( $ ( this ) . index ( )) . addClass ( "cur" ) . siblings ( ) . removeClass ( 'cur' ) ;
              $ ( "div" ) . hide ( ) . eq ( $ ( this ) . index ( )) . show ( ) ;
              //: $("div").eq($(".tab li").index(this)).addClass("on").siblings().removeClass('on'); 
         }) ;
     }) ;
}) ;
</ script >
< style >
div { margin: 0; padding: 0; width: 184 px; height: 200 px; border: 1 px  solid  #ccc; display: none; }
.tab { margin: 0; padding: 0; list-style: none; width: 200 px; overflow: hidden; }
.tab  li { float: left; width: 60 px; height: 30 px; background: #ccc; color: #fff; border: 1 px  solid  redtext-align: center; line-height: 30 px; cursor: pointer}
.on { display: block; }
.tab  li .cur { background: blue; }
</ style >
</ head >
< body >
     < ul   class = "tab" >
         < li > </ li >
         < li   class = "cur" > </ li >
         < li > </ li >
     </ ul >
     < div > 11 </ div >
     < div   class = "on" > 22 </ div >
     < div > 33 </ div >
</ body >
</ html >

假设是自己写的,先看看核心逻辑是什么?

   
   
1
2
3
4
5
6
7
$ ( document ) . ready ( function ( ) {
         $ ( ".tab li" ) . click ( function ( ) {
              $ ( ".tab li" ) . eq ( $ ( this ) . index ( )) . addClass ( "cur" ) . siblings ( ) . removeClass ( 'cur' ) ;
              $ ( "div" ) . hide ( ) . eq ( $ ( this ) . index ( )) . show ( ) ;
              //: $("div").eq($(".tab li").index(this)).addClass("on").siblings().removeClass('on'); 
         }) ;
     }) ;

很简单,不做过多说明了。
第二步 ,改写代码为封装插件做准备,原文中的是index为顺序的,我想不要这种顺序,改用data属性来指明那个tab对应那个div。
先给内容区域,总体用div包装一下,同样要改变样式。
tab页和内容区域做关联,改用data属性关联。

   
   
1
2
3
4
5
6
7
8
9
$ ( document ) . ready ( function ( ) {
         $ ( ".tab li" ) . click ( function ( ) {
              var  $this  =  $ ( this ) ;
              var  c  =  $this . data ( "tab" ) ;
              alert ( c )
              $this . addClass ( "cur" ) . siblings ( ) . removeClass ( 'cur' ) ;
              $ ( ".tabContent" ) . find ( '>[data-tab=' + c + ']' ) . addClass ( "on" ) . siblings ( ) . removeClass ( 'on' ) ; 
         }) ;
     }) ;



   
   
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
$ ( document ) . ready ( function ( ) {
         $ ( ".tab li" ) . click ( function ( ) {
              var  $this  =  $ ( this ) ;
              var  c  =  $this . data ( "tab" ) ;
              $this . addClass ( "cur" ) . siblings ( ) . removeClass ( 'cur' ) ;
              $ ( ".tabContent" ) . find ( '>[data-tab=' + c + ']' ) . addClass ( "on" ) . siblings ( ) . removeClass ( 'on' ) ; 
         }) ;
     }) ;
</ script >
< style >
.tabContent  div { margin: 0; padding: 0; width: 184 px; height: 200 px; border: 1 px  solid  #ccc; display: none; }
.tab { margin: 0; padding: 0; list-style: none; width: 200 px; overflow: hidden; }
.tab  li { float: left; width: 60 px; height: 30 px; background: #ccc; color: #fff; border: 1 px  solid  redtext-align: center; line-height: 30 px; cursor: pointer}
.tab  li .cur { background: blue; }
div .on { display: block; }
</ style >
</ head >
< body >
     < ul   id = "tab"   class = "tab" >
         < li   data-tab = "tab1" > </ li >
         < li   data-tab = "tab2"   class = "cur" > </ li >
         < li   data-tab = "tab3" > </ li >
     </ ul >
     < div   id = "tabContent"   class = "tabContent" >
          < div   data-tab = "tab1" > 11 </ div >
          < div   data-tab = "tab2"   class = "on" > 22 </ div >
          < div   data-tab = "tab3" > 33 </ div >
     </ div >
</ body >
</ html >

第三步 ,用模板来封装插件,套用就行了,我做一个可以支持默认激活某一项的配置。

   
   
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
         this . element  =  element ;
         this . options  =  $ . extend ({ } , defaults , options ) ;
         this . _defaults  =  defaults ;
         this . _name  =  pluginName ;
         this . init ( ) ;
     }
     Plugin . prototype . init  =   function ( ) {
         var  self  =  this ;
         $ ( this . element ) . find ( 'li' ) . click ( function ( ) {
              var  $this  =  $ ( this ) ;
              var  name  =  $this . data ( "tab" ) ;
              $this . addClass ( "cur" ) . siblings ( ) . removeClass ( 'cur' ) ;
              $ ( self . options . contentWrapper ) . find ( '>[data-tab=' + name + ']' ) . addClass ( "on" ) . siblings ( ) . removeClass ( 'on' ) ;
         }) ;
         var  activeName  =  this . options . activeTabName ;
         alert ( activeName ) ;
         if ( activeName  ==  null ) {
              $ ( this . element ) . find ( 'li' ) . eq ( 0 ) . click ( ) ;
         } else {
              $ ( this . element ) . find ( '>[data-tab=' + activeName + ']' ) . click ( ) ;
         }
        
     }
     $ . fn [ pluginName ]  =  function ( options ) {
         return  this . each ( function ( ) {
              if ( !$ . data ( this , 'plugin_' + pluginName )) {
                   $ . data ( this , 'plugin_' + pluginName , new  Plugin ( this , options )) ;
              }
         }) ;
     }
}) ( jQuery , window , document ) ;

使用方式:

   
   
1
$ ( '#tab' ) . myTab ({ contentWrapper : '#tabContent' , activeTabName  :  'tab2' }) ;


第四步 ,代码优化,每个li都有click,太浪费了。改用委托(原先用的delegate,经网友提醒,包括bind,我全改成on了)。

   
   
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
         var  self  =  this ;
         var  $element  =  $ ( this . element ) ;
         $element . on ( 'click' , 'li' , function ( ) {
              var  $this  =  $ ( this ) ;
              var  c  =  $this . data ( "tab" ) ;
              $element . trigger ( 'tab/change' , c ) ;
         }) ;
         $element . on ( 'tab/change' , function ( e , tabName ) {
              $element . find ( 'li' ) . removeClass ( 'cur' ) ;
              $element . find ( '>[data-tab=' + tabName + ']' ) . addClass ( "cur" ) ;
         }) ;
         var  $contentWrapper  =  $ ( this . options . contentWrapper ) ;
         $element . on ( 'tab/change' , function ( e , tabName ) {
              $contentWrapper . find ( '>[data-tab]' ) . removeClass ( 'on' ) ;
              $contentWrapper . find ( '>[data-tab=' + tabName + ']' ) . addClass ( "on" ) ;
         }) ;
         var  activeName  =  this . options . activeTabName ;
         if ( activeName  ==  null ) {
              $element . find ( 'li' ) . eq ( 0 ) . click ( ) ;
         } else {
              $element . trigger ( 'tab/change' , this . options . activeTabName ) ;
         }
     }
     $ . fn [ pluginName ]  =  function ( options ) {
         return  this . each ( function ( ) {
              if ( !$ . data ( this , 'plugin_' + pluginName )) {
                   $ . data ( this , 'plugin_' + pluginName , new  Plugin ( this , options )) ;
              }
         }) ;
     }
}) ( jQuery , window , document ) ;

完整案例

   
   
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
              }
         }) ;
     }
}) ( jQuery , window , document ) ;
</ script >
< script   type = "text/javascript" >
$ ( function ( ) {
     $ ( '#tab' ) . myTab ({ contentWrapper : '#tabContent' , activeTabName  :  'tab2' }) ;
})
</ script >
< style >
.tabContent  div { margin: 0; padding: 0; width: 184 px; height: 200 px; border: 1 px  solid  #ccc; display: none; }
.tab { margin: 0; padding: 0; list-style: none; width: 200 px; overflow: hidden; }
.tab  li { float: left; width: 60 px; height: 30 px; background: #ccc; color: #fff; border: 1 px  solid  redtext-align: center; line-height: 30 px; cursor: pointer}
.tab  li .cur { background: blue; }
div .on { display: block; }
</ style >
</ head >
< body >
     < ul   id = "tab"   class = "tab" >
         < li   data-tab = "tab1" > </ li >
         < li   data-tab = "tab2" > </ li >
         < li   data-tab = "tab3" > </ li >
     </ ul >
     < div   id = "tabContent"   class = "tabContent" >
          < div   data-tab = "tab1" > 11 </ div >
          < div   data-tab = "tab2" > 22 </ div >
          < div   data-tab = "tab3" > 33 </ div >
     </ div >
</ body >
</ html >

如你所料,封装插件过程很简单,难的还是业务逻辑。
一般插件还要支持传入字符串当方法调用的,这里面没写,以后可能会补充进去。
比如有方法支持让第几个激活。还有,还要支持AMD的,这里只是一个简单例子,没有写进去。

本文完。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值