jQuery Plugin Structure


/ Published in: jQuery
Save to your folder(s)



Copy this code and paste it in your HTML
  1. (function( $ ){
  2.  
  3. // Default options
  4. var defaults = {
  5. // OPTIONS
  6. // ...
  7. };
  8. var methods = {
  9. init : function( options ) {
  10.  
  11. // Merge defaults with user options
  12. options = $.extend({}, defaults, options);
  13.  
  14. // Return jQuery object to maintain chainability
  15. return this.each(function() {
  16. var $this = $(this);
  17.  
  18. });
  19. },
  20. method2 : function( ) {
  21. // Return jQuery object to maintain chainability
  22. return this.each(function() {
  23. var $this = $(this);
  24.  
  25. });
  26. },
  27. method3 : function( ) {
  28. // Return jQuery object to maintain chainability
  29. return this.each(function() {
  30. var $this = $(this);
  31.  
  32. });
  33. },
  34. method4 : function( options ) {
  35. // Merge defaults with user options
  36. var options = $.extend(defaults, options);
  37.  
  38. // Return jQuery object to maintain chainability
  39. return this.each(function() {
  40. var $this = $(this);
  41.  
  42. });
  43. }
  44. };
  45.  
  46. $.fn.PLUGINNAME= function( method ) {
  47. // Method calling logic
  48. if ( methods[method] ) {
  49. return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
  50. } else if ( typeof method === 'object' || ! method ) {
  51. return methods.init.apply( this, arguments );
  52. } else {
  53. $.error( 'Method ' + method + ' does not exist on jQuery.PLUGINNAME' );
  54. }
  55. };
  56. })( jQuery );

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.