首页 > 代码库 > Jquery Plugin模版

Jquery Plugin模版

1. [代码][JavaScript]代码     

01// remember to change every instance of "pluginName" to the name of your plugin!
02// the semicolon at the beginning is there on purpose in order to protect the integrity
03// of your scripts when mixed with incomplete objects, arrays, etc.
04;(function($) {
05 
06    // we need attach the plugin to jQuery‘s namespace or otherwise it would not be
07    // available outside this function‘s scope
08    // "el" should be a jQuery object or a collection of jQuery objects as returned by
09    // jQuery‘s selector engine
10    $.pluginName = function(el, options) {
11 
12        // plugin‘s default options
13        // this is private property and is accessible only from inside the plugin
14        var defaults = {
15 
16            propertyName: ‘value‘,
17 
18            // if your plugin is event-driven, you may provide callback capabilities
19            // for its events. call these functions before or after events of your
20            // plugin, so that users may "hook" custom functions to those particular
21            // events without altering the plugin‘s code
22            onSomeEvent: function() {}
23 
24        }
25 
26        // to avoid confusions, use "plugin" to reference the
27        // current instance of the  object
28        var plugin = this;
29 
30        // this will hold the merged default, and user-provided options
31       // plugin‘s properties will be accessible like:
32        // plugin.settings.propertyName from inside the plugin or
33        // myplugin.settings.propertyName from outside the plugin
34        // where "myplugin" is an instance of the plugin
35        plugin.settings = {}
36
37        // the "constructor" method that gets called when the object is created
38        // this is a private method, it can be called only from inside the plugin
39        var init = function() {http://www.huiyi8.com/moban/
40 
41            // the plugin‘s final properties are the merged default and
42            // user-provided options (if any)
网站源码
43            plugin.settings = $.extend({}, defaults, options);
44 
45            // make the collection of target elements available throughout the plugin
46            // by making it a public property
47           plugin.el = el;
48 
49            // code goes here
50 
51        }
52 
53        // public methods
54        // these methods can be called like:
55        // plugin.methodName(arg1, arg2, ... argn) from inside the plugin or
56        // myplugin.publicMethod(arg1, arg2, ... argn) from outside the plugin
57        // where "myplugin" is an instance of the plugin
58 
59       // a public method. for demonstration purposes only - remove it!
60        plugin.foo_public_method = function() {
61 
62            // code goes here
63 
64        }
65 
66        // private methods
67        // these methods can be called only from inside the plugin like:
68        // methodName(arg1, arg2, ... argn)
69 
70        // a private method. for demonstration purposes only - remove it!
71        var foo_private_method = function() {
72 
73            // code goes here
74 
75        }
76 
77        // call the "constructor" method
78        init();
79 
80    }
81 
82})(jQuery);