In the previous notebook, we explained how to use jquery to extend global functions and objects: Dom Note (vi): How do I do a jquery extension?
In this notebook, a simple animation plugin, named Example-plugin, is developed to implement a simple function: The cursor moves to the target label, the label moves, the background color is changed, and the label reverts to the original style when the cursor leaves.
Because it is an extension of the jquery object, it is extended with JQuery.fn.extend (), the plug-in name is DWQS, and the closure is used:
/* * Sample Plugin Features: * The cursor moves to the Target tab, the label moves, changes the background color, and when the cursor leaves, the label reverts to its original style . */ (function ($) { ///$.fn.extend () for object expansion $.fn.extend ({ //Plugin feature implementation DWQS: function (options) { //Subject }} );}) (JQuery);
Define default parameters for the plug-in and expand with $.extend ():
//Set default values for plug-in Parameters var defaults = { padding:20, //move distance time:300, //move time color: "Red" //Background color //Use $.extend () to overwrite default values in plug-ins var options = $.extend (defaults,options);
Add function code
//Convert the DOM element referenced by this to a jquery object var obj = $ (this); //When the mouse passes, add animation obj.mouseover (function() { obj.animate ({paddingLeft:options.padding}, Options.time); Obj.css ("BackgroundColor", Options.color);}); //The mouse leaves the restore obj.mouseout (function() { obj.animate ({paddingleft:0},options.time); Obj.css ("BackgroundColor", "");});
The file name of the plugin is named Example-plugin.js save, the complete code is as follows:
/** Sample Plugin Features:* The cursor moves to the Target tab, the label moves, changes the background color, and when the cursor leaves, the label reverts to its original style .*/(function($) {///$.fn.extend () for object expansion$.fn.extend ({//Plug-in function implementationDWQS:function(options) {//Set default values for plug-in Parameters varDefaults = {padding:20,//Moving distanceTIME:300,//Move TimeColor"Red" //Background color};//Use $.extend () to overwrite default values in plug-ins varOptions = $.extend (defaults,options);return This. each (function() {//Convert the DOM element referenced by this to a jquery object varobj = $ ( This);//Add animation when mouse passesObj.mouseover (function() {obj.animate ({paddingleft:options.padding},options.time); OBJ.CSS ("BackgroundColor", Options.color); });//The mouse resumes when leftObj.mouseout (function() {obj.animate ({paddingleft:0},options.time); OBJ.CSS ("BackgroundColor",""); }); }); }//Do not have the number or error});}) (JQuery);
Test Plug-ins:
<! DOCTYPE html>function () {$ ("Div>div"). Dwqs ({padding:35,time : 500,colo R: "#ccc"}); }); </script></body>
Effect Demo: http://jqplugin.sinaapp.com/test.html
Code Download: http://download.csdn.net/detail/u011043843/8235387
Original starting: http://www.ido321.com/1333.html
Dom Notes (vii): developing jquery Plugins