Created
December 15, 2016 07:01
-
-
Save z4none/11ae0c1ce3ece8bb3ebca7d7e2d7fa43 to your computer and use it in GitHub Desktop.
jquery plugin template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;(function ($, window, document, undefined) { | |
| var pluginName = 'pluginName', //自定义一个插件名称 | |
| defaults = { //定义插件的默认属性 | |
| }; | |
| function Plugin(element, options) { | |
| this.element = element; //缓存element,让原型链上的方法都可以访问 | |
| this.options = $.extend({}, defaults, options); //默认属性和自定义熟悉合并处理 | |
| this._defaults = defaults; | |
| this._name = pluginName; | |
| this.init(); //插件初始化(在里面可以做dom构造,事件绑定等操作) | |
| } | |
| Plugin.prototype.init = function() { | |
| }; | |
| $.fn[pluginName] = function (options) { | |
| return this.each(function () { | |
| //将实例化后的插件暂存,避免重复渲染 | |
| if (!$.data(this, 'plugin_' + pluginName)) { | |
| $.data(this, 'plugin_' + pluginName, new Plugin(this, options)); | |
| } | |
| }); | |
| } | |
| })(jQuery, window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment