jquery 擴充外掛程式方法

來源:互聯網
上載者:User

標籤:

分析外掛程式jquery.countdown.js

 1 (function($) { 2     $.fn.countdown = function(options) { 3         // default options 4         var defaults = { 5             attrName : ‘data-diff‘, 6             tmpl : ‘<span class="hour">%{h}</span>小時<span class="minute">%{m}</span>分鐘<span class="second">%{s}</span>秒‘, 7             end : ‘已結束‘, 8             afterEnd : null 9         };10 11         console.log(options);12         options = $.extend(defaults, options);13         console.log(options);14         15 16         // trim zero17         function trimZero(str) {18             return parseInt(str.replace(/^0/g, ‘‘));19         }20         // convert string to time21         function getDiffTime(str) {22             var m;23             if ((m = /^(\d{4})[^\d]+(\d{1,2})[^\d]+(\d{1,2})\s+(\d{2})[^\d]+(\d{1,2})[^\d]+(\d{1,2})$/.exec(str))) {24                 var year = trimZero(m[1]),25                     month = trimZero(m[2]) - 1,26                     day = trimZero(m[3]),27                     hour = trimZero(m[4]),28                     minute = trimZero(m[5]),29                     second = trimZero(m[6]);30                 return Math.floor((new Date(year, month, day, hour, minute, second).getTime() - new Date().getTime()) / 1000);31             }32             return parseInt(str);33         }34         // format time35         function format(diff) {36             var tmpl = options.tmpl, day, hour, minute, second;37             day = /%\{d\}/.test(tmpl) ? Math.floor(diff / 86400) : 0;38             hour = Math.floor((diff - day * 86400) / 3600);39             minute = Math.floor((diff - day * 86400 - hour * 3600) / 60);40             second = diff - day * 86400 - hour * 3600 - minute * 60;41             tmpl = tmpl.replace(/%\{d\}/ig, day)42                 .replace(/%\{h\}/ig, hour)43                 .replace(/%\{m\}/ig, minute)44                 .replace(/%\{s\}/ig, second);45             return tmpl;46         }47         // main48         return this.each(function() {49             var el = this,50                 diff = getDiffTime($(el).attr(options.attrName));51             function update() {52                 if (diff <= 0) {53                     $(el).html(options.end);54                     if (options.afterEnd) {55                         options.afterEnd();56                     }57                     return;58                 }59                 $(el).html(format(diff));60                 setTimeout(function() {61                     diff--;62                     update();63                 }, 1000);64             }65             update();66         });67     };68 })(jQuery);
1、$.fn.countdown 為擴充jquery方法,函數名為countdown
2、 var defaults 相當於類裡成員變數, 

3、defaults = {
attrName : ‘data-diff‘,
tmpl : ‘<span class="hour">%{h}</span>小時<span class="minute">%{m}</span>分鐘<span class="second">%{s}</span>秒‘,
end : ‘已結束‘,
afterEnd : null
};  

  defaluts裡的索引值對提供屬性,相當於類裡成員函數的參數。

4、options = $.extend(defaults, options); 將options值合并到defaults,並返回合并結果
5、return this.each(function() );  擷取網頁的的id或name值的並修改的函數操作


網頁調用範例:
<!doctype html><html>    <head>        <meta charset="utf-8" />        <title>Countdown Demo</title>        <style>        </style>        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>        <script src="jquery.countdown.js"></script>        <script>            $(function() {                $(‘.J_countdown1‘).countdown();                $(‘.J_countdown2‘).countdown({                    tmpl : ‘<span>%{d}</span>天<span>%{h}</span>小時<span>%{m}</span>分<span>%{s}</span>秒‘                });                $(‘.J_countdown3‘).countdown({                    tmpl : ‘<span>%{d}</span>天, <span>%{h}</span>小時, <span>%{m}</span>分, <span>%{s}</span>秒‘                });            });        </script>    </head>    <body>        <div class="J_countdown1" data-diff="10"></div>        <div class="J_countdown1" data-diff="70"></div>        <div class="J_countdown1" data-diff="3610"></div>        <div class="J_countdown1" data-diff="86410"></div>        <div class="J_countdown2" data-diff="21234567890"></div>        <div class="J_countdown3" data-diff="21234567890"></div>    </body></html>

 

 


jquery 擴充外掛程式方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.