Learn how to create a jQuery plugin from scratch – the basics, options, compatibility and examples.
http://devheart.org/articles/tutorial-creating-a-jquery-plugin/
The Basics
A plugin is written as a method or function.
Creating a jQuery Function
Syntax
The function has to return this.each(..)
to maintain chainability – so that the function can be used with a single or several jQuery objects.
Example
Creating a jQuery Method
Syntax
Example
Options
Make your plugin as flexible and user friendly as possible using options. The $.extend()
method takes two or more objects as arguments and merges the contens of them into the first object.
Example
A function that set text color (red by default).
We can now choose use this function passing the settings parameter or not.
Compatibility
As the $
variable might be used by other plugins, use a alias technique to make your plugin forward-compatible.
We pass jQuery
to the function and can now use whatever alias for jQuery we like. So instead of $
you could also use any other valid JavaScript variable name.
The jQuery Plugin Checklist
This is a list of important points to remember when developing a jQuery plugin (from jQuery.com).
- Name your file jquery.[insert name of plugin].js, eg. jquery.debug.js
- All new methods are attached to the jQuery.fn object, all functions to the jQuery object.
- inside methods,
this
is a reference to the current jQuery object. - Any methods or functions you attach must have a semicolon (;) at the end – otherwise the code will break when compressed.
- Your method must return the jQuery object, unless explicity noted otherwise.
- Use
this.each
to iterate over the current set of matched elements. - Always attach the plugin to jQuery instead of
$
, so users can use a custom alias vianoConflict()
.
jQuery Plugin Templates
These are two good code templates to start from when developing a jQuery plugin.
Function Template
Method Template
Example: jQuery Slideshow Plugin
I have chosen to use very simple examples so far in order for you to get started. The following example is a bit more complex and might help to get your inspiration going.
It uses the function setInterval()
in combination with the jQuery effects fadeOut()
andfadeIn()
to cycle any number of images within a HTML-element.
The Setup
HTML
CSS
JavaScript
Usage
To enable slideshow on the #slideshow
div, we simply call it using the following JavaScript code:
Because we allow settings to change the behaviour of the slideshow, we could make it wait 5 seconds between images and set the “fade” duration to 200 ms using:
-
It’s not harder than that! Good luck in creating your own jQuery plugins!
Thanks for this post. I was looking for something like this.
=) thank you!
I worked around with jQuery and now wanted to start with Plugins
and I found out this as a great way to begin.
hey Viktor, great article,
just noticed: in your checklist the second bullet says: # All new methods are attached to the jQuery.fn object, all functions to the jQuery object. However, from your examples it seems the opposite (methods are att to $ and function to $.fn) is correct..
I’m really not sure which is the correct terming, please do tell.
pavel
Think Im going to try my first jQuery plugin later this week, if I can get through this mass of work piling up.
WoW so good, thanks you very much!
Thanks! my concern about jQuery plugins development were clarified with this tutorial