Sunday, August 19, 2012

How to create your first jQuery plugin



In this article I will show you how to create a simple jQuery plugin menu.
First of all, check this demo so you know where we are heading to: smoothymenu plugin.

As you can see, we will create a plugin to render a menu with fading effect on its items.
The first step to create your plugin is to download the latest jQuery library at jquery.com.

Now you can create a new JavaScript file, this file will contain the main function of our plugin (our plugin will also use a CSS file). To declare a plugin function in jQuery you have to use this syntax:


jQuery.fn.smoothymenu = function() {}



Before we get started with the Javascript let’s take a look at an HTML example file. By doing this we can define the basic structure of our menu:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SmoothyMenu Jquery plugin</title>
<link href="css/smoothymenu.css" rel="stylesheet" type="text/css" media="screen"></link>
<script type="text/javascript" src="lib/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="lib/jquery/smoothymenu.jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('ul#menu').smoothymenu();
});
</script>
</head>
<body>
<h1>SmoothyMenu Jquery plugin</h1>

<ul id="menu">
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">Contact</a>
</li>
<li>
<a href="#">About</a>
</li>
</ul>
</body>
</html>



As you can see I apply my plugin on a list with an id. Now we can write our plugin, the first step is to retrieve the element we will work with. In or case the list, to do that you can create a variable:


var menu = $(this);



After that, we want to apply some styles to our menu so we will programmatically add a class to the menu. This class is in our CSS plugin file and will be provide with the JavaScript file (of course the end user will be able to customize the CSS to match his needs).


menu.attr('class', 'smoothymenu');



Here is the trick to create the fade in/out: we add a block to each menu item, if the mouse if out of the item it will be not visible, when the user will hover an item we will fade it in and vice-versa.


/* Append the specific effect block to each menu items. */
menu.find('li').append('<div></div>');

/* Apply an effect on the hover of an element of the menu. */
menu.find('li').bind('mouseover', function(){
$(this).find('div.hover-effect').stop().fadeTo('fast', 1);
});

/* Apply an effect when the mouse is out of an element of the menu. */
menu.find('li').bind('mouseout', function(){
$(this).find('div.hover-effect').stop().fadeTo('fast', 0);
})



Ok we are done, of course you can add parameters to your function and do more complexes operations.
I tried to keep it it simple in this example, to sum up here is the whole smoothymenu plugin function:


jQuery.fn.smoothymenu = function()
{
var menu = $(this);

/* Define the class of the menu */
menu.attr('class', 'smoothymenu');

/* Append the specific effect block to each menu items. */
menu.find('li').append('<div></div>');

/* Apply an effect on the hover of an element of the menu. */
menu.find('li').bind('mouseover', function(){
$(this).find('div.hover-effect').stop().fadeTo('fast', 1);
});

/* Apply an effect when the mouse is out of an element of the menu. */
menu.find('li').bind('mouseout', function(){
$(this).find('div.hover-effect').stop().fadeTo('fast', 0);
})
};



You can download the sources right here: smoothymenu jquery plugin sources (see the demo).

No comments:

Post a Comment