• each()
  • 插件机制
  • animation
  • ajax
//each()
//这里第一个参数指定将this指向每次循环到的那个元素身上,而第三个参数element其实就是this本身所以和第一个参数是一样的,而第二个参数是每次循环的下标
zQuery.prototype.each=function(fn){
for(var i=0;i<this.elements.length;i++){
fn.call(this.elements[i],i,this.elements[i]);
}
}; //插件机制
$.fn=zQuery.prototype; //引用,如果只绑定一个插件,就直接写$.fn.toRed = function(){}...,相当于直接将toRed函数挂到zQuery.prototype上,可以进行zQuery对象调用 //如果是挂多个插件,将函数参数中的json循环,挂在到zQuery.prototype中
$.fn.extend=function(json){
for(var key in json){
//key == tored/toYellow
//json[key] == fn
zQuery.prototype[key]=json[key];
}
}; $.ajax = ajax; //将ajax函数挂在到$.ajax上 //animation
zQuery.prototype.animate=function(json,options){
for(var i=0;i<this.elements.length;i++){
move(this.elements[i],json,options);
}
}; //ajax封装
function ajax(options){
//-1.整理options
options=options||{};
options.data=options.data||{};
options.type=options.type||'get';
options.timeout=options.timeout||0; //0.整理data json --> string
options.data.t=Math.random();
var arr=[];
for(var key in options.data){
arr.push(key+'='+encodeURIComponent(options.data[key]));
}
var str = arr.join('&'); //1.创建ajax
if(window.XMLHttpRequest){
var oAjax=new XMLHttpRequest();
}else{
var oAjax=new ActiveXObject('Microsoft.XMLHTTP');
}
if(options.type=='get'){//get
//2.建立连接
oAjax.open('get',options.url+'?'+str,true);
//3.发送
oAjax.send();
}else {//post
//2.建立连接
oAjax.open('post',options.url,true);
//设置请求头
oAjax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
oAjax.send(str);
} //4.接收
oAjax.onreadystatechange=function(){
if(oAjax.readyState==4){
clearTimeout(timer);
if(oAjax.status>=200 && oAjax.status<300 || oAjax.status==304){
options.success && options.success(oAjax.responseText);
}else{
options.error && options.error(oAjax.status);
}
}
} //5.超时
if(options.timeout){
var timer=setTimeout(function(){
//数据不要了 ,你别加载
oAjax.abort();//中断ajax请求,也会触发readyState==4
},options.timeout);
} } //运动函数的封装
function move(obj,json,options){
//整理可选参数
options = options || {};
options.duration = options.duration || 300;
options.complete = options.complete || null;
options.easing = options.easing || 'ease-out'; var start={};
var dis={};
for(var key in json){
start[key]=parseFloat(getStyle(obj,key)); if(isNaN(start[key])){
switch(key){
case 'width':
start[key]=obj.offsetWidth;
break;
case 'height':
start[key]=obj.offsetHeight;
break;
case 'opacity':
start[key]=1;
break;
case 'left':
start[key]=0;
break;
case 'top':
start[key]=0;
break;
//marginLeft/top... padding
}
} dis[key]=json[key]-start[key];
}
var count=Math.round(options.duration/30);
var n=0; clearInterval(obj.timer);
obj.timer=setInterval(function(){
n++; for(var key in json){//办事部分包起来
switch(options.easing){
case 'linear':
var a=n/count;// 匀速运动
var cur=start[key]+dis[key]*a;
break;
case 'ease-in':
var a=n/count;// 加速运动
var cur=start[key]+dis[key]*(a*a*a);
break;
case 'ease-out':
var a=1-n/count;// 减速运动
var cur=start[key]+dis[key]*(1-a*a*a);
break;
}
if(key=='opacity'){
obj.style.opacity=cur;
obj.style.filter='alpha(opacity='+cur*100+')';
}else{
obj.style[key]=cur+'px';
}
} if(n==count){
clearInterval(obj.timer);
options.complete && options.complete();
}
},30);
}

【学】jQuery的源码思路6——增加each,animaion,ajax以及插件机制的更多相关文章

  1. 【学】jQuery的源码思路4——增加一些功能

    本文说一些简单的jQuery实现原理 eq() get() hide() show() index() find() //返回找到的一组元素中的第n个 zQuery.prototype.eq=func ...

  2. 【学】jQuery的源码思路5——增加class的操作

    hasClass, addClass, removeClass, toggleClass //addClass,加入class会对元素,利用正则,将class中多余的空格去掉 zQuery.proto ...

  3. 【学】jQuery的源码思路1——后代选择器

    jQuery的源码思路1--后代选择器 这里探讨一下jQuery中后代选择器的封装原理,并自己写一下 getEle('#div1 ul li .box');接受的参数就是个后代选择器,类似于这样: # ...

  4. 【学】jQuery的源码思路2——$符号是如何封装的

    jQuery中的$符号功能很强大,原因在于对函数参数的个数以及种类的控制,还有对于面向对象思想的运用 function jQuery(args){ //接受参数,并对其判断 this.elements ...

  5. 【学】jQuery的源码思路3——添加事件及其他

    这段添加的方法有: 各类事件函数 css() addEvent() toggle() //添加各种事件,将常用的事件名称放入数组,然后循环着加入到zQuery对象的原型上 var eventArr = ...

  6. 【深入浅出jQuery】源码浅析--整体架构

    最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...

  7. jQuery.Callbacks 源码解读二

    一.参数标记 /* * once: 确保回调列表仅只fire一次 * unique: 在执行add操作中,确保回调列表中不存在重复的回调 * stopOnFalse: 当执行回调返回值为false,则 ...

  8. jQuery.attributes源码分析(attr/prop/val/class)

    回顾 有了之前的几篇对于jQuery.attributes相关的研究,是时候分析jQuery.attr的源码了 Javascript中的attribute和property分析 attribute和p ...

  9. 【深入浅出jQuery】源码浅析2--奇技淫巧

    最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...

随机推荐

  1. 佛祖保佑,永无bug

    /* _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : | ...

  2. Android自定义权限和使用权限

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 自定义权限,主要用于保护被赋予权限的组件.如无权限与有权限,正如public与private的对类保 ...

  3. HDU5840 (分块+树链剖分)

    Problem This world need more Zhu 题目大意 给一颗n个点的有点权的树,有m个询问,对于每个询问u,v,k,首先将点u到点v的最短路径上的所有点按顺序编号,u的编号为1, ...

  4. MongoDB初学笔记

    http://www.cnblogs.com/huangxincheng/archive/2012/02/18/2356595.html

  5. Android DownloadProvider学习 (二)

    DownloadManager.Request用来请求一个下载,DownloadManager.Query用来查询下载信息,这两个类的具体功能会在后面穿插介绍.DownloadManager的源码可见 ...

  6. Django project structure: how does static folder, STATIC_URL, STATIC_ROOT work

    So I've been messing up with Django(1.6+) project setting for quite sometime, this is what i finally ...

  7. RealProxy实现AOP编程(1)

    Program.cs class Program { static void Main(string[] args) { User user = " }; var processor = T ...

  8. JDBC连接数据库(PreparedStatement)

    PreparedStatement是在数据库端防止SQL注入漏洞的SQL方法这里演示了一些基本使用方法同样使用Oracle数据库,之前已经手动建立了一张t_account表数据库代码参见上一篇< ...

  9. setup notifier actions in aodh alarm

    Aodh alarm NOTIFIER ==> alarm_actions URL: http://<host>/<action> NOTIFIER will resol ...

  10. Oracle笔记3-高级查询

    高级查询 1.关联查询 作用:可以跨多表查询 --查询出员工的名字和他所在部门的名字 //错误//select first_name,name from s_emp,s_dept; //错误原因:产生 ...