jQuery 插件格式 规范
2016-01-28 15:36
561 查看
方式一(自定义对象):
(function($, window, document) {
var Plugin, defaults, pluginName;
调用时的函数名:
pluginName = "slidesjs";
默认配置:
defaults= {
width: 940,
height: 528,
callback: {
loaded: function() {},
start: function() {},
complete: function() {}
}
};
构建自定义对象:
Plugin = (function() {
function Plugin(element, options) {
this.element = element;
this.options = $.extend(true, {}, defaults, options); //拓展用户自定义参数
this._defaults = defaults;
this._name = pluginName;
this.init();
}
return Plugin;
})();
拓展一系列方法:
Plugin.prototype.init = function() { ... }
Plugin.prototype.next = function() { ... }
...
拓展到jQuery的fn上:
return $.fn[pluginName] = function(options) {
//把选中的每个元素都进行实例化
return this.each(function() {
if (!$.data(this, "plugin_" + pluginName)) {
return $.data(this, "plugin_" + pluginName, new Plugin(this, options));
}
});
};
})(jQuery, window, document);
使用:
$(function() {
$('#slides').slidesjs({
width: 940,
height: 528
});
});
或者这样扩展进jQuery也可以:
$.fn.Swipe = function(params) {
return this.each(function() {
$(this).data('Swipe', new Swipe($(this)[0], params));
});
}
方式2(简单点的):
(function($) {
"use strict";
$.fn.boxRefresh = function(options) {
var _option= $.extend({
trigger: ".refresh-btn",
onLoadStart: function(box) {},
onLoadDone: function(box) {}
}, options);
return this.each(function() { ... });
};
})(jQuery);
另一种方式,使用extend:
(function(f) {
jQuery.fn.extend({slimScroll: function(h) {
...
}});
jQuery.fn.extend({slimscroll: jQuery.fn.slimScroll})
})(jQuery);
![](file:///C:/Users/ADMINI~1/AppData/Local/Temp/enhtmlclip/EN10eed2d1.png)
(function($, window, document) {
var Plugin, defaults, pluginName;
调用时的函数名:
pluginName = "slidesjs";
默认配置:
defaults= {
width: 940,
height: 528,
callback: {
loaded: function() {},
start: function() {},
complete: function() {}
}
};
构建自定义对象:
Plugin = (function() {
function Plugin(element, options) {
this.element = element;
this.options = $.extend(true, {}, defaults, options); //拓展用户自定义参数
this._defaults = defaults;
this._name = pluginName;
this.init();
}
return Plugin;
})();
拓展一系列方法:
Plugin.prototype.init = function() { ... }
Plugin.prototype.next = function() { ... }
...
拓展到jQuery的fn上:
return $.fn[pluginName] = function(options) {
//把选中的每个元素都进行实例化
return this.each(function() {
if (!$.data(this, "plugin_" + pluginName)) {
return $.data(this, "plugin_" + pluginName, new Plugin(this, options));
}
});
};
})(jQuery, window, document);
使用:
$(function() {
$('#slides').slidesjs({
width: 940,
height: 528
});
});
或者这样扩展进jQuery也可以:
$.fn.Swipe = function(params) {
return this.each(function() {
$(this).data('Swipe', new Swipe($(this)[0], params));
});
}
方式2(简单点的):
(function($) {
"use strict";
$.fn.boxRefresh = function(options) {
var _option= $.extend({
trigger: ".refresh-btn",
onLoadStart: function(box) {},
onLoadDone: function(box) {}
}, options);
return this.each(function() { ... });
};
})(jQuery);
另一种方式,使用extend:
(function(f) {
jQuery.fn.extend({slimScroll: function(h) {
...
}});
jQuery.fn.extend({slimscroll: jQuery.fn.slimScroll})
})(jQuery);
![](file:///C:/Users/ADMINI~1/AppData/Local/Temp/enhtmlclip/EN10eed2d1.png)
相关文章推荐
- jquery图片切换插件jquery.cycle.js参数详解
- 【转】HTML5 jQuery图片上传前预览
- jQuery对象和DOM对象互转
- JQuery取消和注册对象Click事件同时防止多播委托
- jQuery获取checkbox选中的值
- Jquery基础学习(一)
- jQuery的deferred对象详解
- 新时代网站必备jQuery插件
- jquery优化写法
- jQuery解决IE6、7、8不能使用 JSON.stringify 函数的问题
- 总是容易忘记 jquery ajax POST
- jquery ajax生成Select
- jQuery中$.fn的用法示例介绍
- JQuery里的原型prototype分析
- jquery 中多条件选择器,相对选择器,层次选择器的区别
- 基于jQuery实现以手风琴方式展开和折叠导航菜单
- jQuery Form 表单提交插件-----formSerialize,fieldSerialize,fieldValue,resetForm,clearForm,clearFields的 应用
- JQuery 中比较有趣的几个函数
- jQuery自定义插件简单介绍
- jQuery中this与$(this)的区别