jQuery插件开发学习

1、jQuery插件编写准备

要使用jQuery进行自定义插件的编写,首先应该的是了解jQuery的插件机制或者说是通过jQuery库本身提供的哪些函数进行插件的编写,主要涉及的两个函数是:jQuery.extend(object)和jQuery.fn.extend(object),其具体作用可以通过查找jQuery的API文档 得到,这里也把API简单转过来:

A、 jQuery.extend(object)

扩展jQuery对象本身,用来在jQuery命名空间上增加新函数。
例如:
Js代码   收藏代码
  1. jQuery.extend({  
  2.   min: function(a, b) { return a < b ? a : b; },  
  3.   max: function(a, b) { return a > b ? a : b; }  
  4. });   
使用方法:
Js代码   收藏代码
  1. jQuery.min(2,3);  //=> 2  
  2. $.max(4,5);         //=>5  

B、jQuery.fn.extend(object)
扩展 jQuery 元素集来提供新的方法(通常用来制作插件)。具体实例将在下面进行一个简单的示例来进行了解。

 

2、简单的jQuery插件的编写

A、jQuery插件的基本格式

Js代码   收藏代码
  1. /* 
  2.  * digitClocker 
  3.  * @author: zhoucl 
  4.  * @date  : 08/11/2011 
  5.  */  
  6. (function($, undefined){      
  7.       ...  
  8.       //code here  
  9.       ...  
  10. })(jQuery);  

B、将原来直接javascript编写的一个电子时钟改写为jQuery插件使用,代码如下:

Java代码   收藏代码
  1. /* 
  2.  * digitClocker 
  3.  * @author: zhoucl 
  4.  * @date  : 03/11/2011 
  5.  */  
  6. (function($, undefined){      
  7.     $.extend($.fn, {  
  8.         digitclocker: function(settings){  
  9.             var params = $.extend({  
  10.                 baseURL: '../js/jquery/custom_plugins/',  
  11.                 imgPath: 'digits/'  
  12.             }, settings);  
  13.               
  14.             for(var i = 1; i < 9; i++) {  
  15.                 if(i % 3 == 0) $(this).append("<img alt='0' src='" + params.baseURL + params.imgPath + "colon.gif'>");  
  16.                 else $(this).append("<img class='clockImage' alt='0' src='" + params.baseURL + params.imgPath + "0.gif'>");  
  17.             }  
  18.               
  19.             new DigitClock(params, $(this));  
  20.         }  
  21.     });  
  22.       
  23.     setInterval(function(){  
  24.         DigitClock.reDraw();  
  25.     }, 1000);  
  26.       
  27.     DigitClock = function(params, container) {  
  28.         this.params = params;  
  29.         this.container = container;  
  30.           
  31.         DigitClock.clocks.push(this);  
  32.         DigitClock.reDraw();  
  33.     }  
  34.       
  35.     DigitClock.clocks=[];  
  36.       
  37.     DigitClock.reDraw = function() {  
  38.         var d=new Date();  
  39.         for (var i = 0; i < this.clocks.length; i++) {  
  40.             this.clocks[i].setTime(d.getHours(),d.getMinutes(),d.getSeconds());  
  41.         }  
  42.     }  
  43.       
  44.     DigitClock.preZero = function(n, pos) {  
  45.         return ("0"._digitClockRepeat(pos-1)+n).slice(-pos);  
  46.     }  
  47.       
  48.     DigitClock.prototype = {  
  49.         setTime : function(h, i ,s) {  
  50.             this.setImage(  
  51.                 DigitClock.preZero(h,2) + DigitClock.preZero(i,2) + DigitClock.preZero(s,2)  
  52.             )  
  53.         },  
  54.           
  55.         setImage: function(s) {  
  56.             s = s.split("");  
  57.             var baseURL = this.params.baseURL;  
  58.             var imgPath = this.params.imgPath;  
  59.             var i = 0;  
  60.             $(".clockImage"this.container).each(function(){  
  61.                 $(this).attr("src", baseURL + imgPath + s[i++] +".gif");  
  62.             });  
  63.         }  
  64.     }  
  65.       
  66.     String.prototype._digitClockRepeat = function(n) {  
  67.         return new Array(n+1).join(this);  
  68.     }  
  69. })(jQuery);  

 引用上述js文件,在页面中得调用代码如下:

Html代码   收藏代码
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  2. <html>  
  3.   <head>  
  4.     <title>simple_test.html</title>  
  5.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  6.     <meta http-equiv="description" content="this is my page">  
  7.     <meta http-equiv="content-type" content="text/html; charset=UTF-8">  
  8.     <!--调用jQuery的库:jquery-1.5.2.js-->  
  9.     <script src="../js/jquery/jquery-1.5.2.js" type="text/javascript"></script>  
  10.     <script src="../js/jquery/custom_plugins/jquery.clzps.digitclock.js" type="text/javascript"></script>  
  11.     <script>  
  12.       $(function(){  
  13.           $('#digitClock').digitclocker({  
  14.         baseURL: '../js/jquery/custom_plugins/',  
  15.         imgPath: 'digits/'  
  16.       });  
  17.       });  
  18.     </script>  
  19.   </head>  
  20.     
  21.   <body>  
  22.      <div id="digitClock"></div>  
  23.   </body>  
  24. </html>  

效果图如下: 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值