制作一个简洁的jquery插件
原文: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插件结构。
//You need an anonymous function to wrap around your function to avoid conflict
匿名包裹函数,避免了全局变量
(function($){
//Attach this new method to jQuery
$.fn.extend({
//This is where you write your plugin's name
// jquery插件名称
pluginname: function() {
//Iterate over the current set of matched elements
// 迭代选择器选中的dom元素
return this.each(function() {
//code to be inserted here
// jquery插件的代码
});
}
});
//pass jQuery to the function,
//So that we will able to use any valid Javascript variable name
//to replace "$" SIGN. But, we'll stick to $ (I like dollar sign: ) )
})(jQuery);
3. Plugin With Options
如果你看过了第一部分的简介,padding值是可配置的。让用户根据自己的业务需求传递一些参数给插件是很有必要的。保证每一个变量都有一个默认值是一个良好的编程实践。现在,你需要下面的这段代码:
(function($){
$.fn.extend({
//pass the options variable to the function
pluginname: function(options) {
//Set the default values, use comma to separate the settings, example:
var defaults = {
padding: 20,
mouseOverColor : '#000000',
mouseOutColor : '#ffffff'
}
var options = $.extend(defaults, options);
return this.each(function() {
var o = options;
//code to be inserted here
//you can access the value like this
alert(o.padding);
});
}
});
})(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
(function($){
$.fn.extend({
//plugin name - animatemenu
animateMenu: function(options) {
//Settings list and the default values
var defaults = {
animatePadding: 60,
defaultPadding: 10,
evenColor: '#ccc',
oddColor: '#eee'
};
var options = $.extend(defaults, options);
return this.each(function() {
var o =options;
//Assign current element to variable, in this case is UL element
var obj = $(this);
//Get all LI in the UL
var items = $("li", obj);
//Change the color according to odd and even rows
$("li:even", obj).css('background-color', o.evenColor);
$("li:odd", obj).css('background-color', o.oddColor);
//Attach mouseover and mouseout event to the LI
items.mouseover(function() {
$(this).animate({paddingLeft: o.animatePadding}, 300);
}).mouseout(function() {
$(this).animate({paddingLeft: o.defaultPadding}, 300);
});
});
}
});
})(jQuery);
译者注:对于web前端从业者来说,使用jquery插件自然可以提升我们的开发效率。但是我们不仅是要知其然也要知其所以然。不仅要做到会使用,也要知道其实现原理。一方面看一些业内优秀的jquery插件的源代码可以有效的提升我们的编码能力;另一方面,掌握了编写jquery插件的方法,可以把我们项目中常用的组件通过这种方式写成jquery插件,便于代码的复用,使代码更加的简洁和可维护。译者也是一个前端小沫沫,从业时间也不长,文中如果有不对的地方,还望指出,不胜感激。
注:感兴趣的可以关注笔者微信公众号,每周推送一篇前端干货

制作一个简洁的jquery插件的更多相关文章
- 【前端】制作一个handlebars的jQuery插件
(function($) { var compiled = {}; $.fn.handlebars = function($srcNode, data) { // 取出模版内容 var src = $ ...
- Swift 制作一个新闻通知中心插件1
使用 Swift 制作一个新闻通知中心插件(1) 随着 iOS 8 的发布,苹果为开发者们开放了很多新的 API,而在这些开放的接口中 通知中心插件 无疑是最显眼的一个.通知中心就不用过多介绍了,相信 ...
- 推荐一个内容滚动jquery插件
myslider是一个内容滚动jquery插件,版本0.1.2的每次滚动内容是一行内容,可以是文字,可以是一个链接,还可以是图片. 官方网址:http://keleyi.com/jq/myslider ...
- 编写一个简单的Jquery插件
1.实现内容 定义一个简单的jquery插件,alert传递进来的参数 2.插件js文件(jquery.showplugin.js) (function ($) { //定义插件中的方法 var me ...
- 手把手制作一个简单的IDEA插件(环境搭建Demo篇)
新建IDEA插件File --> new --> Project--> Intellij PlatForm Plugin-->Next-->填好项目名OK 编写插件新建工 ...
- 使用 Swift 制作一个新闻通知中心插件(1)
input[type="date"].form-control,.input-group-sm>input[type="date"].input-grou ...
- How to Create a Basic Plugin 如何写一个基础的jQuery插件
How to Create a Basic Plugin Sometimes you want to make a piece of functionality available throughou ...
- 使用 Swift 制作一个新闻通知中心插件(2)
我们在第一部分的文章中详细讲解了创建一个通知中心插件的整体过程.我们成功的在通知中心里面显示了新闻列表.但是截止到目前,我们还不能从通知中心的列表中查看新闻的详细内容.在这次的教程中,我们就以上次的教 ...
- 学了一个封装的jquery插件,感觉还成
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
随机推荐
- My Interface
一.创建Myinterface接口 public interface Myinterface { static final String MyifName="我的接口"; stat ...
- JAVA源码走读(一) HashMap与ArrayList
HashMap 一.HashMap基本概念: HashMap是基于哈希表的Map接口的实现.此实现提供所有可选的映射操作,并允许使用null值和null键.此类不保证映射的顺序,特别是它不保证该顺序恒 ...
- 关于Spring事务回滚的问题
在spring的配置文件中,如果数据源的defaultAutoCommit设置为True了,那么方法中如果自己捕获了异常,事务是不会回滚的,如果没有自己捕获异常则事务会回滚,如下例比如配置文件里有这么 ...
- Emergency(山东省第一届ACM省赛)
Emergency Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Kudo’s real name is not Kudo. H ...
- 文本XSS攻击过滤
在FCK或百度编辑器等常用富文本编辑器中,通常是会被XSS攻击 处理方法: 文本框模拟输入了以下文本 <span style="dispaly:none" onclick=& ...
- 1、B2BUA
链接1:proxy和B2BUA的区别和联系:http://www.cnblogs.com/gnuhpc/archive/2012/12/11/2813499.html 链接2:http://blog. ...
- Oracle中将查询出的多条记录的某个字段拼接成一个字符串的方法
11g里面用listagg: select listagg(name,',') within (order by id) from table 10g里面用wm_concat:select wm_co ...
- 【原创】安装LoadRunner12.53 版本时出现Critical error的解决方法
步骤: 1.在官网上下载LoadRunner12.53正版,只不过要注册,然后官网会给个序列号. 2.安装成功之后,快捷键已创建,打开Virtual User Generator时,提示如下错误: 此 ...
- leetcode-【hard】4. Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- c代码读取目录信息
#include <stdio.h> #include <sys/types.h> #include <dirent.h> int main(void) { DIR ...