Ok, let’s start with a very simple plugin. I have very little imagination, so my plugin will do very stupid work – it will change the font size and weight of an element. I’ll call it … demoPlugin. Original, isn’t it?
Let’s define it:
$.fn.demoPlugin = function(settings) { }
$("div").demoPlugin();
var config = {'fontSize' : '30px', 'fontWeight' : 'bolder'};
if (settings) $.extend(config, settings);
this.each(function() { $(this).css({ 'font-size':config.fontSize, 'font-weight':config.fontWeight}); });
(function($) { $.fn.demoPlugin = function(settings) { var config = { 'fontSize' : '30px', 'fontWeight' : 'bolder'}; if (settings) $.extend(config, settings); this.each(function() { $(this).css({ 'font-size':config.fontSize, 'font-weight':config.fontWeight}); }); return this; }; })(jQuery);
Don't forget to return this at the end. It is very important in order to use chain calls.
I’ll continue with less trivial plugin example later.
Next plugin article.
No comments:
Post a Comment