//getStyle
1 function getStyle(obj,attr){ 2 if(obj.currentStyle){ 3 return obj.currentStyle[attr]; 4 } 5 else{ 6 return getComputedStyle(obj,false)[attr]; 7 } 8 }
//getByClass
1 function getByClass(oParent,sClass){ 2 var arr = []; 3 var aEle = oParent.getElementsByTagName("*"); 4 for(var i = 0;i < aEle.length;i ++){ 5 if(aEle[i].className == sClass){ 6 arr.push(aEle[i]) 7 } 8 } 9 return arr; 10 }
//myAddEvent
1 function myAddEvent(obj,sEv,fn){ 2 //改造后return false 能同时具有阻止冒泡和默认事件的作用 3 if(obj.attachEvent){ 4 obj.attachEvent("on"+sEv,function(){ 5 if(false == fn.call(obj)){ 6 event.cancelBubble = true; 7 return false; 8 } 9 }); 10 } 11 else{ 12 obj.addEventListener(sEv,function(ev){ 13 if(false == fn.call(obj)){ 14 ev.cancelBubble = true; 15 ev.preventDefault(); 16 } 17 },false); 18 } 19 }
//元素获取
1 function jQuery(vArg){ 2 this.elements = []; 3 switch(typeof vArg){//typeof 4 case "function": 5 myAddEvent(window,"load",vArg); 6 break; 7 case "string": 8 switch(vArg.charAt(0)){ 9 case "#": 10 var obj = document.getElementById(vArg.substring(1)); 11 this.elements.push(obj); 12 break; 13 case ".": 14 this.elements = getByClass(document,vArg.substring(1)); 15 break; 16 default: 17 this.elements = document.getElementsByTagName(vArg); 18 } 19 break; 20 case "object": 21 this.elements.push(vArg); 22 } 23 }
//$拥有jQuery功能
1 function $(vArg){ 2 return new jQuery(vArg); 3 }
//click()
1 jQuery.prototype.click = function(fn){ 2 for(var i = 0;i < this.elements.length;i ++){ 3 myAddEvent(this.elements[i],'click',fn); 4 } 5 return this;//支持链式操作 6 };
//css()
1 jQuery.prototype.css = function(attr,value){ 2 if(arguments.length == 2){ 3 for(var i = 0;i < this.elements.length;i ++){ 4 this.elements[i].style[attr] = value; 5 } 6 } 7 else{ 8 if(typeof attr == "string"){//typeof 9 return getStyle(this.elements[0],attr);//获取时不能链式操作,因为到这里就返回了。带单位 10 } 11 else{ 12 for(var i = 0;i < this.elements.length;i ++){ 13 var k = ""; 14 for(k in attr){ 15 this.elements[i].style[k] = attr[k]; 16 } 17 } 18 } 19 } 20 return this; 21 };
//toggle() >> 0>fn1; 1>fn2; 2>fn3
1 jQuery.prototype.toggle = function(){ 2 var _arguments = arguments; 3 for(var i = 0;i < this.elements.length;i ++){ 4 addToggle(this.elements[i]); 5 } 6 function addToggle(obj){ 7 var count = 0; 8 myAddEvent(obj,"click",function(){ 9 _arguments[count++ % _arguments.length].call(obj); 10 }); 11 } 12 return this; 13 };
//attr()
1 jQuery.prototype.attr = function(name,value){ 2 if(arguments.length == 2){ 3 for(var i = 0;i < this.elements.length;i ++){ 4 this.elements[i][name] = value; 5 } 6 } 7 else{ 8 return this.elements[0][name]; 9 } 10 return this; 11 };
//eq()
1 jQuery.prototype.eq = function(num){ 2 return $(this.elements[num]);//普通的dom对象变成jQuery对象。单一dom对象的处理方式 3 };
//find()
1 jQuery.prototype.find = function(str){ 2 var aResult = []; 3 for(var i = 0;i < this.elements.length;i ++){ 4 switch(str.charAt(0)){ 5 case ".": 6 var aEle = getByClass(this.elements[i],str.substring(1)); 7 aResult = aResult.concat(aEle); 8 default: 9 var aEle = this.elements[i].getElementsByTagName(str); 10 appendArr(aResult,aEle); 11 } 12 } 13 var newJquery = $(); 14 newJquery.elements = aResult;//数组dom对象的处理方式 15 return newJquery; 16 }; 17 18 function appendArr(arr1,arr2){ 19 for(var i = 0;i < arr2.length;i ++){ 20 arr1.push(arr2[i]); 21 } 22 }
//index()
1 jQuery.prototype.index = function(){ 2 return getIndex(this.elements[0]); 3 };
//getIndex()
1 function getIndex(obj){ 2 var aBrother = obj.parentNode.children; 3 for(var i = 0;i < aBrother.length;i ++){ 4 if(aBrother[i] == obj){ 5 return i; 6 } 7 } 8 }
//bind()
1 jQuery.prototype.bind = function(sEv,fn){ 2 for(var i = 0;i < this.elements.length; i++){ 3 myAddEvent(this.elements[i],sEv,fn); 4 } 5 };
//extend()
1 jQuery.prototype.extend = function(name,fn){ 2 jQuery.prototype[name] = fn; 3 };
//size()
1 $().extend("size",function(){ 2 return this.elements.length; 3 });
//animate()插件
1 $().extend("animate",function(json){ 2 for(var i = 0;i < this.elements.length;i ++){ 3 startMove(this.elements[i],json); 4 } 5 function startMove(obj,json,fnEnd){ 6 clearInterval(obj.timer); 7 obj.timer = setInterval(function(){ 8 var oStop = true; 9 for( var attr in json){ 10 var cur = 0; 11 if(attr == "opacity"){ 12 cur = Math.round( parseFloat( getStyle(obj,attr) ) * 100 ); 13 } 14 else{ 15 cur = parseInt( getStyle(obj,attr) ); 16 } 17 18 var speed = (json[attr] - cur) / 7; 19 speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed); 20 21 if(cur != json[attr]){ 22 oStop = false; 23 } 24 if(attr == "opacity"){ 25 obj.style.filter = 'alpha(opacity:'+(cur+speed)+')'; 26 obj.style.opacity = (cur + speed) / 100; 27 } 28 else{ 29 obj.style[attr] = cur + speed + "px"; 30 } 31 } 32 if(oStop){ 33 clearInterval(obj.timer); 34 fnEnd&&fnEnd(); 35 } 36 },30); 37 } 38 });
//drag()插件
1 $().extend('drag',function(){ 2 for(var i = 0;i < this.elements.length;i++){ 3 drag(this.elements[i]); 4 } 5 function drag(obj){ 6 var disX = 0; 7 var disY = 0; 8 obj.onmousedown = function(ev){ 9 //这里位置的计算一定是mousedown的时候 10 var oEvent = ev || event; 11 disX = oEvent.clientX - parseInt( getStyle(obj,"left") ); 12 disY = oEvent.clientY - parseInt( getStyle(obj,"top") ); 13 14 if(obj.setCapture){ 15 obj.onmousemove = mouseMove; 16 obj.onmouseup = mouseUp; 17 obj.setCapture(); 18 } 19 else{ 20 document.onmousemove = mouseMove; 21 document.onmouseup = mouseUp; 22 } 23 function mouseMove(ev){ 24 var oEvent = ev || event; 25 26 obj.style.top = oEvent.clientY - disY + "px"; 27 obj.style.left = oEvent.clientX - disX + "px"; 28 } 29 function mouseUp(){ 30 this.onmousemove = this.onmouseup = null; 31 if(obj.releaseCapture){ 32 obj.releaseCapture(); 33 } 34 } 35 return false; 36 }; 37 } 38 });