Dom Notes (vii): developing jquery Plugins

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.