jquery Extension Plugin method

Source: Internet
Author: User

Analysis Plugin Jquery.countdown.js

1(function($) {2$.fn.countdown =function(options) {3         //Default Options4         varDefaults = {5Attrname: ' Data-diff ',6Tmpl: ' <span class= "hour" >%{h}</span> hours <span class= "minute" >%{m}</span> minutes <span class= " Second ">%{s}</span> s",7End: ' Closed ',8Afterend:NULL9         };Ten  one Console.log (options); aOptions =$.extend (defaults, options); - Console.log (options); -          the  -         //Trim Zero -         functionTrimzero (str) { -             returnparseint (str.replace (/^0/g, ")); +         } -         //Convert string to time +         functiongetdifftime (str) { a             varm; at             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))) { -                 varYear = Trimzero (m[1]), -month = Trimzero (m[2])-1, -Day = Trimzero (m[3]), -hour = Trimzero (m[4]), -minute = Trimzero (m[5]), inSecond = Trimzero (m[6]); -                 returnMath.floor ((NewDate (year, month, day, hour, minute, second). getTime ()-NewDate (). GetTime ())/1000); to             } +             returnparseint (str); -         } the         //format time *         functionformat (diff) { $             varTmpl =options.tmpl, day, hour, minute, second;Panax NotoginsengDay =/%\{d\}/.test (tmpl)? Math.floor (diff/86400): 0; -hour = Math.floor ((diff-day * 86400)/3600); theminute = Math.floor ((diff-day * 86400-hour * 3600)/60); +Second = Diff-day * 86400-hour * 3600-minute * 60; aTmpl = Tmpl.replace (/%\{d\}/ig, Day) the. Replace (/%\{h\}/ig, Hour) +. Replace (/%\{m\}/ig, Minute) -. Replace (/%\{s\}/ig, second); $             returntmpl; $         } -         //Main -         return  this. each (function() { the             varEl = this, -diff =getdifftime ($ (el). attr (options.attrname));Wuyi             functionUpdate () { the                 if(diff <= 0) { - $ (el). HTML (options.end); wu                     if(options.afterend) { - options.afterend (); about                     } $                     return; -                 } - $ (el). HTML (format (diff)); -SetTimeout (function() { adiff--; + Update (); the}, 1000); -             } $ Update (); the         }); the     }; the}) (jQuery);
1, $.fn.countdown to extend the jquery method, the function is named Countdown

3. Defaults = {
Attrname: ' Data-diff ',
Tmpl: ' <span class= "hour" >%{h}</span> hours <span class= "minute" >%{m}</span> minutes <span class= " Second ">%{s}</span> s",
End: ' Closed ',
Afterend:null
};

The Key-value pair in Defaluts provides the property, which is equivalent to the parameter of the member function in the class.

4. Options = $.extend (defaults, options); merges the options values into defaults and returns the merge results
5, return This.each (function());  Gets the ID or name value of the Web page and modifies the function action


Web page Invocation sample:
<!DOCTYPE HTML><HTML>    <Head>        <MetaCharSet= "utf-8" />        <title>Countdown Demo</title>        <style>        </style>        <Scriptsrc= "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></Script>        <Scriptsrc= "jquery.countdown.js"></Script>        <Script>            $(function() {                $('. J_countdown1'). Countdown (); $('. J_COUNTDOWN2'). Countdown ({tmpl:'<span>%{d}</span> days <span>%{h}</span> hours <span>%{m}</span> minute <span>%{s }</span> sec'                }); $('. J_countdown3'). Countdown ({tmpl:'<span>%{d}</span> days, <span>%{h}</span> hours, <span>%{m}</span> points, <span> %{s}</span> sec'                });        }); </Script>    </Head>    <Body>        <Divclass= "j_countdown1"Data-diff= "ten"></Div>        <Divclass= "j_countdown1"Data-diff= "the "></Div>        <Divclass= "j_countdown1"Data-diff= "3610"></Div>        <Divclass= "j_countdown1"Data-diff= "86410"></Div>        <Divclass= "j_countdown2"Data-diff= "21234567890"></Div>        <Divclass= "j_countdown3"Data-diff= "21234567890"></Div>    </Body></HTML>



jquery Extension Plugin method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.