cto*_*ife 7 jquery jquery-plugins
我正在编写我的第一个jQuery插件,并且我有一些关于我应该放置东西的问题.我试着四处寻找,但似乎有不同的方法来构建插件,所以我迷路了.我通过关注jQuerys网站上的Authoring Plugins文档得到了这一点,但不确定下一步该怎么做.
我在哪里放一个共同的功能?以hello()为例.假设我打算在整个插件中使用该功能,它应该在哪里生活?现在我把它放在顶部,这似乎有效,但我不确定这是正确的做法.
我在哪里放一个全局变量?以我的变量"imgs"数组为例.我应该在现在或我的init方法或其他地方声明这个吗?
(function($){
function hello(){
alert("hello world");
}
var imgs = new Array();
var methods = {
init: function(options){
if(options){
$.extend({
width: 200,
height: 200,
selectedColor: '#123456'
}, options);
// for every image do something
this.find('img').each(function(){
var $this = $(this);
var width = $this.width();
var height = $this.height();
console.log("width: " + width + " height: " + height);
selection = function() {
console.log($this.attr('src'));
hello();
};
$this.bind('click', selection);
});
}
return this;
},
test : function( string ) {
console.log("Test: " + string);
//return "here";
}
};
$.fn.Thumbnails = function(method) {
// Method calling logic
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.Thumbnails' );
}
};
})(jQuery);
$thumbnails = $('#imagescontainer').Thumbnails({
height: 150,
});
Run Code Online (Sandbox Code Playgroud)谢谢您的帮助!