Create a simple jquery plug-in.

Source: Internet
Author: User

Create a simple jquery plug-in.

Http://mp.weixin.qq.com/s? _ Biz = MzAxMzgwNDU3Mg ==& mid = 401571467 & idx = 1 & sn = tags & scene = 1 & srcid = tags & from = singlemessage & isappinstalled = 0 # wechat_redirect


Http://www.queness.com/post/112/a-really-simple-jquery-plugin-tutorial:

1. Introduction

For beginners of jquery, making a jquery plug-in is still a relatively advanced topic. In the last month, jquery was released. Therefore, I learned how to separate javascript code from html files. I am not satisfied with the code I have written. Therefore, I decided to learn more about how to create a jquery plug-in to make javascript files more concise.

The plug-in created in this article is based on my last Tutorial: Navigation List menu + jQuery Animate Effect Tutorial (http://www.queness.com/post/68/navigation-list-menujquery-animate-effect-tutorial) Change in the Tutorial I put all js Code to document. in the ready callback function, it is like this:


But this time, the Code is as follows:

It looks much more concise and easy to reuse in other projects.

2. Plug-in Structure

The official jquery website provides a detailed description of the jquery plug-in. However, I think it is too difficult to understand.

In order to make our programming life better and simpler, I studied related documents online. The code below is an elegant jquery plug-in structure.

    1. // You need an anonymous function to wrap around your function to avoid conflict

    2. The anonymous package function avoids global variables.

    3. (Function ($ ){

    4.  

    5. // Attach this new method to jQuery

    6. $. Fn. extend ({

    7. // This is where you write your plugin's name

    8. // Jquery plug-in name

    9. Pluginname: function (){

    10.  

    11. // Iterate over the current set of matched elements

// The dom element selected by the iteration Selector

    1. Return this. each (function (){

    2. // Code to be inserted here

// Jquery plug-in code

  1. });

  2. }

  3. });

  4. // Pass jQuery to the function,

  5. // So that we will able to use any valid Javascript variable name

  6. // To replace "$" SIGN. But, we'll stick to $ (I like dollar sign :))

  7. }) (JQuery );

3. Plugin With Options

If you have read the first section, the padding value is configurable. It is necessary for users to pass some parameters to the plug-in according to their own business needs. Ensuring that each variable has a default value is a good programming practice. Now, you need the following code:

  1. (Function ($ ){

  2.  

  3. $. Fn. extend ({

  4. // Pass the options variable to the function

  5. Pluginname: function (options ){

  6.  

  7.  

  8. // Set the default values, use comma to separate the settings, example:

  9. Var defaults = {

  10. Padding: 20,

  11. MouseOverColor: '#000000 ',

  12. MouseOutColor: '# ffff'

  13. }

  14. Var options = $. extend (defaults, options );

  15.  

  16. Return this. each (function (){

  17. Var o = options;

  18. // Code to be inserted here

  19. // You can access the value like this

  20. Alert (o. padding );

  21. });

  22. }

  23. });

  24. }) (JQuery );

     

4. The animateMenu Plugin

Now you know the basic structure of the jquery plug-in. The following plug-in is based on a previous tutorial. It receives four parameters:

  • AnimatePadding:Set the padding value for the animate effect

  • DefaultPadding:Set the default padding value

  • EvenColor:Set the color this color if index value is even number

  • OddColor:Set the color this color if index value is odd numbe

  1. (Function ($ ){

  2. $. Fn. extend ({

  3. // Plugin name-animatemenu

  4. AnimateMenu: function (options ){

  5.  

  6. // Settings list and the default values

  7. Var defaults = {

  8. AnimatePadding: 60,

  9. DefaultPadding: 10,

  10. EvenColor: '# ccc ',

  11. OddColor: '# eee'

  12. };

  13. Var options = $. extend (defaults, options );

  14. Return this. each (function (){

  15. Var o = options;

  16. // Assign current element to variable, in this case is UL element

  17. Var obj = $ (this );

  18. // Get all LI in the UL

  19. Var items = $ ("li", obj );

  20. // Change the color according to odd and even rows

  21. $ ("Li: even", obj).css ('background-color', o. evenColor );

  22. $ ("Li: odd", obj).css ('background-color', o. oddColor );

  23. // Attach mouseover and mouseout event to the LI

  24. Items. mouseover (function (){

  25. $ (This). animate ({paddingLeft: o. animations}, 300 );

  26. }). Mouseout (function (){

  27. $ (This). animate ({paddingLeft: o. defaultPadding}, 300 );

  28. });

  29. });

  30. }

  31. });

  32. }) (JQuery );

Note: For web Front-end practitioners, using jquery plug-ins can naturally improve our development efficiency. But we need to know not only about it, but also about it. We need to know not only how it is used, but also how it works. On the one hand, looking at the source code of some excellent jquery plug-ins in the industry can effectively improve our coding capabilities; on the other hand, we have mastered the method for compiling jquery plug-ins, common components in our project can be written as jquery plug-ins in this way to facilitate code reuse and make the code more concise and maintainable. The translator is also a front-end Tom Mo, and his career is not long enough. If something is wrong in this article, I would like to point out that I am very grateful.

Note: If you are interested, you can follow the author's public account and push a dry front-end article every week.

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.