原文:http://mp.weixin.qq.com/s?__biz=MzAxMzgwNDU3Mg==&mid=401571467&idx=1&sn=08cb00963e6efd3881d3397903e84752&scene=1&srcid=0125cT8n9FJMI1u2faaQgjcS&from=singlemessage&isappinstalled=0#wechat_redirect


英文原文链接:http://www.queness.com/post/112/a-really-simple-jquery-plugin-tutorial

1,简介

对于jquery初学者来说,制作一个jquery插件还是一个比较高级的话题。最近一个月一致在倒腾jquery。因此学些了怎么把javascript代码从html文件中分离出来,对于现在所写的代码并不满意。因此,我决定去深入了解如何做一个jquery插件来使得javascript文件更加的简洁。

本文中制作的插件是基于我上一次的教程:Navigation List menu + jQuery Animate Effect Tutorial(http://www.queness.com/post/68/navigation-list-menujquery-animate-effect-tutorial)改教程中我把所有的js代码都放到了document.ready回调函数中,就像这样:


但是这一次,代码是这样的:

它看起来简洁多了,而且也容易复用到别的项目当中。

2.插件结构

关于制作jquery插件,jquery官网提供了一份非常详细的说明文档。但是,我觉得它实在是太难理解了。

为了是我们的编程生活更美好一些,简单一些,我在线研究了相关的文档。下买呢的这段代码是一段优雅的jquery插件结构。

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

    2. 匿名包裹函数,避免了全局变量

    3. (function($){

    4. //Attach this new method to jQuery

    5. $.fn.extend({

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

    7. // jquery插件名称

    8. pluginname: function() {

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

// 迭代选择器选中的dom元素

    1. return this.each(function() {

    2. //code to be inserted here

// jquery插件的代码

  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

如果你看过了第一部分的简介,padding值是可配置的。让用户根据自己的业务需求传递一些参数给插件是很有必要的。保证每一个变量都有一个默认值是一个良好的编程实践。现在,你需要下面的这段代码:

  1. (function($){

  2. $.fn.extend({

  3. //pass the options variable to the function

  4. pluginname: function(options) {

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

  6. var defaults = {

  7. padding: 20,

  8. mouseOverColor : '#000000',

  9. mouseOutColor : '#ffffff'

  10. }

  11. var options =  $.extend(defaults, options);

  12. return this.each(function() {

  13. var o = options;

  14. //code to be inserted here

  15. //you can access the value like this

  16. alert(o.padding);

  17. });

  18. }

  19. });

  20. })(jQuery);

4. The animateMenu Plugin

现在您已经知道了jquery插件的基本结构。接下来的这个插件就是基于我之前的一个教程。它接收4个参数:

  • 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. //Settings list and the default values

  6. var defaults = {

  7. animatePadding: 60,

  8. defaultPadding: 10,

  9. evenColor: '#ccc',

  10. oddColor: '#eee'

  11. };

  12. var options = $.extend(defaults, options);

  13. return this.each(function() {

  14. var o =options;

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

  16. var obj = $(this);

  17. //Get all LI in the UL

  18. var items = $("li", obj);

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

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

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

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

  23. items.mouseover(function() {

  24. $(this).animate({paddingLeft: o.animatePadding}, 300);

  25. }).mouseout(function() {

  26. $(this).animate({paddingLeft: o.defaultPadding}, 300);

  27. });

  28. });

  29. }

  30. });

  31. })(jQuery);

译者注:对于web前端从业者来说,使用jquery插件自然可以提升我们的开发效率。但是我们不仅是要知其然也要知其所以然。不仅要做到会使用,也要知道其实现原理。一方面看一些业内优秀的jquery插件的源代码可以有效的提升我们的编码能力;另一方面,掌握了编写jquery插件的方法,可以把我们项目中常用的组件通过这种方式写成jquery插件,便于代码的复用,使代码更加的简洁和可维护。译者也是一个前端小沫沫,从业时间也不长,文中如果有不对的地方,还望指出,不胜感激。

注:感兴趣的可以关注笔者微信公众号,每周推送一篇前端干货

制作一个简洁的jquery插件的更多相关文章

  1. 【前端】制作一个handlebars的jQuery插件

    (function($) { var compiled = {}; $.fn.handlebars = function($srcNode, data) { // 取出模版内容 var src = $ ...

  2. Swift 制作一个新闻通知中心插件1

    使用 Swift 制作一个新闻通知中心插件(1) 随着 iOS 8 的发布,苹果为开发者们开放了很多新的 API,而在这些开放的接口中 通知中心插件 无疑是最显眼的一个.通知中心就不用过多介绍了,相信 ...

  3. 推荐一个内容滚动jquery插件

    myslider是一个内容滚动jquery插件,版本0.1.2的每次滚动内容是一行内容,可以是文字,可以是一个链接,还可以是图片. 官方网址:http://keleyi.com/jq/myslider ...

  4. 编写一个简单的Jquery插件

    1.实现内容 定义一个简单的jquery插件,alert传递进来的参数 2.插件js文件(jquery.showplugin.js) (function ($) { //定义插件中的方法 var me ...

  5. 手把手制作一个简单的IDEA插件(环境搭建Demo篇)

    新建IDEA插件File --> new --> Project--> Intellij PlatForm Plugin-->Next-->填好项目名OK 编写插件新建工 ...

  6. 使用 Swift 制作一个新闻通知中心插件(1)

    input[type="date"].form-control,.input-group-sm>input[type="date"].input-grou ...

  7. How to Create a Basic Plugin 如何写一个基础的jQuery插件

    How to Create a Basic Plugin Sometimes you want to make a piece of functionality available throughou ...

  8. 使用 Swift 制作一个新闻通知中心插件(2)

    我们在第一部分的文章中详细讲解了创建一个通知中心插件的整体过程.我们成功的在通知中心里面显示了新闻列表.但是截止到目前,我们还不能从通知中心的列表中查看新闻的详细内容.在这次的教程中,我们就以上次的教 ...

  9. 学了一个封装的jquery插件,感觉还成

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

随机推荐

  1. ASP.Net上传大文件解决方案之IIS7.0下的配置

    开源的Brettle.Web.NeatUpload.在公司IIS6.0使用正常,但是在Windows 2008 server IIS7上使用不正常.在网上看到一个解决办法但是没有效果 IIS 7 默认 ...

  2. centos 安装 python2.7 运行webpy 项目

    1.服务器是centos5,在virtualbox里装的.网络选择桥接,ip与主机在一个网段类.主机ip为xxx.xxx.xxx.69,服务器ip定义为xxx.xxx.xxx.66,GATEWAY与N ...

  3. webpy 访问局域网共享资源

    遇到一个问题: 在python shell 中调用局域网远程共享文件时,没问题.但是在webpy中调用时,报错:没有权限.那一定是apache设置问题. 网上找不到类似的方法,于是换个思路搜了一下“p ...

  4. CentOS 7 用户怎样安装 LNMP(Nginx+PHP+MySQL)

    关于 Nginx (发音 “engine x”)这是一款免费.开源.高效的 HTTP 服务器,Nginx是以稳定著称,丰富的功能,结构简单,低资源消耗.本教程演示如何在CentOS 6.5服务器(适用 ...

  5. (Collection)350. Intersection of Two Arrays II

    /* Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2 ...

  6. 【HOW】在InfoPath中如何为浏览和编辑模式设置不同的视图

    1. 在SharePoint Designer中打开要自定义视图的列表.并点击菜单:列表设置 > 在 InfoPath 中设计表单 > {要自定义表单的内容类型},则会自动打开InfoPa ...

  7. PHP表单数据验证

    背景: 在上次项目的时候,一直不明白为什么要对数据验证,我能保证我每次请求的数据都是合法的,但是在后面的时候,原来“用户”并不是那样听话,他总是要给我们找麻烦,然后可能让我们的服务器崩掉.但是只对单个 ...

  8. ORA-15025: could not open disk "/dev/asm***"--转载

    Symptoms: 打完补丁后,数据库报错ORA-15025,数据库无法启动. alert日志信息: Wed Jul22 16:26:57 2015 ORA-15025:could not open ...

  9. 搞定迅雷固件在TP-LINK WR720N,127.0.0.1 9000 获取不到激活码

    本帖最后由 exzzzipad 于 2014-7-2 22:33 编辑 基本情况:设备:TP-LINK WR720N目前固件:[antclan][20120825]720N-4M-NAS-withSA ...

  10. 天地图应用ArcGIS发布的服务

    本文包含三个部分:利用ArcMap将Excel的数据转化为ArcGIS MXD文件.利用ArcMap发布服务.天地图添加ArcGIS发布的服务. 一 MXD文件的生成 假设在Excel中存有两个点的坐 ...