Tutorial: Creating a jQuery plugin
来源:互联网 发布:淘宝开店运费怎么弄 编辑:程序博客网 时间:2024/06/04 19:57
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:
View ExampleDownload Example
-
It’s not harder than that! Good luck in creating your own jQuery plugins!
Comments: 7
Feel free to share your thoughts on this article.
- Tutorial: Creating a jQuery plugin
- A Really Simple jQuery Plugin Tutorial
- How to Make a jQuery Plugin - Tutorial for Beginners
- Tutorial: Creating a custom button
- MFC Tutorial Part 1 - Creating a Window
- iPhone Tutorial for Creating a Splash Screen
- Tutorial 2: Creating a Framework and Window
- Creating a Static Library in iOS Tutorial
- Creating a Static Library in iOS Tutorial
- Creating a Static Library in iOS Tutorial
- Creating a Static Library in iOS Tutorial
- iPhone Programming Tutorial – Creating a ToDo List Using SQLite
- Creating, compiling and linking MATLAB executables (MEX files): a tutorial
- The Java EE 6 Tutorial Creating and Initializing a Servlet
- 【DirectX11-Tutorial】编写第一个窗口程序creating a window
- Unity Official Tutorial --- CREATING A BREAKOUT GAME FOR BEGINNERS
- Creating a Web App with jQuery UI
- Creating an Xcode4 Plugin
- IE下COOKIE失效的一种特殊情况
- VS调试技巧之----Attach to Process
- PhoneGap plugins for Android
- Windows server 2008 添加新磁盘的方法(二)
- 经典逻辑推理题(答案)
- Tutorial: Creating a jQuery plugin
- Perfect Panda Global Protection 2012/2013 Uninstall
- c运行库、c标准库、windows API都是什么玩意
- String,无所不在的数据类型
- 读书笔记--孩子,把你的手给我
- linux下shell重定向
- 嵌套if-else语句的妙用
- VIM---参数配置文件.exrc的详细配置
- 查看redhat的版本
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