Hello everyone, welcome my today's post on "Create a fading effect jquery plugins". In my last tutorial we learned "How to create a Unordered list sequential fade in and fade out effect in jquery". Hope you enjoyed that tutorial. Today we are going to learn how to convert simple jquery code in to a jQuery plugins. So, that we can easily use this plugins anywhere in our application or website.
After reading this tutorial you can convert any jQuery scripts in to a plugins and use them anywhere you want. That means, it's also a tutorial about "How to create you first jQuery plugins". I'm going to divide this tutorial in to 4 sections.
- Write the body section for fading plugins.
- How to integrate plugins in our application.
- How to use fading plugins.
- Finally, Add some styling and body scripts.
So, lets start -
(function($){
$.fn.fadeeffect = function(options){
var defaults = {
delay_time: 100,
fade_time: 250,
background: "#cfe9b6"
};
var options = $.extend(defaults, options);
$(this).each(function(index) {
$(this).delay(options.delay_time*index).fadeIn(options.fade_time).css({
"background":options.background
});
});
return this;
};
})(jQuery);
First of all we need to learn the convention how to write a plugins. Always start code with following codes.
(function($){
// Write you plugins code in here.
})(jQuery);
Now you need to give a name of plugins. As for example we give our plugins name "fadeeffect". Remember plugins is a function that uses globally. After setting name of plugins we write some default property of our plugins like delay time of animation, fade time and background color. You can set this property when you call this "fadeeffect" function. We will see at the bottom of the tutorial.
Then we use extend for override our default behavior. If you set delay time, fade time and background color then extend will override plugins default behavior. Finally we create a fading effects and then return it.
$(function(){
$("ul#my_unordered_lists li").fadeeffect({
delay_time: 100,
fade_time: 1500,
background: "lightpink"
});
});
You can see that, I've only set an unordered list where fading effect will apply and then set delay time, fade time and background colors.
Now we need to add some css code for styling.
ul#my_unordered_lists{
list-style: none;
}
ul#my_unordered_lists li{
width: 400px;
display: none;
border: 1px solid green;
line-height: 2.5em;
margin-bottom: 2px;
text-indent: 5px;
font-family: verdana;
font-size: 10px;
}
Finally add some ul and li in body section.
We are done! Our plugins now ready for use and you can check output live in below :)
Demo: http://demos.coolajax.net/jquery/fadein_fadeout_plugins/
Download: http://demos.coolajax.net/jquery/fadein_fadeout_plugins/plugins.zip
Hope you enjoyed my tutorial. Was this information useful? What other tips would you like to read about in the future? Share your comments, feedback and experiences with us by commenting below!
You might also like...
4 jQuery snippets you can use to manipulate select input
Mega Collection of jQuery Social Networking Plugins
5 most wanted jQuery snippet for web developers
Md Mahbub Alam Khan is the Editor and Founder of CoolAJAX Web Blog. You can follow CoolAJAX on Twitter , on Facebook or you can subscribe via RSS .
Topics