'use strict';
functionZQuery(arg){
this.elements=[];
this.domString='';
switch(typeofarg){
case'string':
//元素 获取到
if(arg.indexOf('<')!=-1){
//存创建标签
this.domString=arg;
}else{
this.elements=getEle(arg);
this.length=this.elements.length;
}
break;
case'function':
DOMReady(arg);
break;
default:
this.elements.push(arg);
break;
}
}
//把new ZQuery变成$
function$(arg){
return newZQuery(arg);
}
//DOMReady
functionDOMReady(fn){
if(document.addEventListener){
document.addEventListener('DOMContentLoaded',function(){
fn&&fn();
},false);
}else{
document.attachEvent('onreadystatechange',function(){
if(document.readyState=='complete'){
fn&&fn();
}
});
}
}
//css
ZQuery.prototype.css=function(name,value){
if(arguments.length==2){
for(vari=0; i
this.elements[i].style[name]=value;
}
}else{
//{} 'width'
if(typeofname=='string'){
returngetStyle(this.elements[0],name);
}else{
varjson=name;
for(vari=0; i
for(varnameinjson){
this.elements[i].style[name]=json[name];
}
}
}
}
};
//attr
ZQuery.prototype.attr=function(name,value){
if(arguments.length==2){
for(vari=0; i
this.elements[i].setAttribute(name,value);
}
}else{
//{} 'width'
if(typeofname=='string'){
return this.elements[0].getAttribute(name);
}else{
varjson=name;
for(vari=0; i
for(varnameinjson){
this.elements[i].setAttribute(name,json[name]);
}
}
}
}
};
//html
ZQuery.prototype.html=function(str){
if(str||str==''){
for(vari=0; i
this.elements[i].innerHTML=str;
}
}else{
return this.elements[0].innerHTML;
}
};
//val
ZQuery.prototype.val=function(str){
if(str||str==''){
for(vari=0; i
this.elements[i].value=str;
}
}else{
return this.elements[0].value;
}
};
//addClass
ZQuery.prototype.addClass=function(sClass){
for(vari=0; i
if(this.elements[i].className){
varre=newRegExp('\\b'+sClass+'\\b','g');
if(this.elements[i].className.search(re)==-1){
this.elements[i].className+=' '+sClass;
}
}else{
this.elements[i].className=sClass;
}
this.elements[i].className=this.elements[i].className.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');
}
};
//removeClass
ZQuery.prototype.removeClass=function(sClass){
for(vari=0; i
if(this.elements[i].className){
varre=newRegExp('\\b'+sClass+'\\b','g');
this.elements[i].className=this.elements[i].className.replace(re,'');
this.elements[i].className=this.elements[i].className.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');
if(this.elements[i].className==''){
this.elements[i].removeAttribute('class');
}
}
}
};
//show
ZQuery.prototype.show=function(){
for(vari=0; i
this.elements[i].style.display='block';
}
return this;
};
//hide
ZQuery.prototype.hide=function(){
for(vari=0; i
this.elements[i].style.display='none';
}
return this;
};
//animate
ZQuery.prototype.animate=function(json,options){
for(vari=0; i
move(this.elements[i],json,options);
}
};
//eq
ZQuery.prototype.eq=function(n){
return this.elements[n];
};
//index
ZQuery.prototype.index=function(){
varaEle=this.elements[0].parentNode.children;
for(vari=0; i
if(aEle[i]==this.elements[0]){
returni;
}
}
};
//jq转原生
ZQuery.prototype.get=function(n){
return this.elements[n];
};
//事件
;'click mouseover mouseout mousedown mousemove mouseup mouseenter mouseleave keydown keyup contextmenu dblclick load error scroll resize'.replace(/\w+/g,function(str){
ZQuery.prototype[str]=function(fn){
for(vari=0; i
addEvent(this.elements[i],str,fn);
}
};
});
//hover
ZQuery.prototype.hover=function(fn1,fn2){
this.mouseenter(fn1);
this.mouseleave(fn2);
};
//toggle
ZQuery.prototype.toggle=function(){
vararg=arguments;
var_this=this;
for(vari=0; i
(function(count){
_this.elements[i].onclick=function(){
arg[count%arg.length]();
count++;
};
})(0);
}
};
//addEvent
functionaddEvent(obj,sEv,fn){
if(obj.addEventListener){
obj.addEventListener(sEv,function(ev){
varoEvent=ev||event;
if(fn(oEvent)==false){
oEvent.preventDefault();
oEvent.cancelBubble=true;
}
},false);
}else{
obj.attachEvent('on'+sEv,function(ev){
varoEvent=ev||event;
if(fn(oEvent)==false){
oEvent.cancelBubble=true;
return false;
}
});
}
}
//DOM插入
ZQuery.prototype.appendTo=function(str){
varaParent=getEle(str);
for(vari=0; i
//不为人知DOM操作
aParent[i].insertAdjacentHTML('beforeEnd',this.domString);
}
};
ZQuery.prototype.prependTo=function(str){
varaParent=getEle(str);
for(vari=0; i
//不为人知DOM操作
aParent[i].insertAdjacentHTML('afterBegin',this.domString);
}
};
ZQuery.prototype.insertAfter=function(str){
varaParent=getEle(str);
for(vari=0; i
//不为人知DOM操作
aParent[i].insertAdjacentHTML('afterEnd',this.domString);
}
};
ZQuery.prototype.insertBefore=function(str){
varaParent=getEle(str);
for(vari=0; i
//不为人知DOM操作
aParent[i].insertAdjacentHTML('beforeBegin',this.domString);
}
};
//remove
ZQuery.prototype.remove=function(){
for(vari=0; i
this.elements[i].parentNode.removeChild(this.elements[i]);
}
};
//插件
$.fn=ZQuery.prototype;
$.fn.extend=function(json){
for(varnameinjson){
ZQuery.prototype[name]=json[name];
}
};
$.ajax=function(json){
ajax(json);
};
$.jsonp=function(json){
jsonp(json);
};
functionjsonp(json){
varjson=json||{};
if(!json.url)return;
json.cbName=json.cbName||'cb';
json.data=json.data||{};
json.data[json.cbName]='show'+Math.random();
json.data[json.cbName]=json.data[json.cbName].replace('.','');
vararr=[];
for(varnameinjson.data){
arr.push(name+'='+json.data[name]);
}
//a=1&b=2
varstr=arr.join('&');
window[json.data[json.cbName]]=function(result){
json.success&&json.success(result);
oH.removeChild(oS);
};
varoH=document.head;
varoS=document.createElement('script');
oS.src=json.url+'?'+str;
oH.appendChild(oS);
}
//ajax
functionjson2url(json){
json.t=Math.random();
vararr=[];
for(varnameinjson){
arr.push(name+'='+json[name]);
}
returnarr.join('&');
}
functionajax(json){
varjson=json||{};
if(!json.url)return;
json.data=json.data||{};
json.type=json.type||'get';
if(window.XMLHttpRequest){
varoAjax=newXMLHttpRequest();
}else{
varoAjax=newActiveXObject('Microsoft.XMLHTTP');
}
switch(json.type.toLowerCase()){
case'get':
oAjax.open('GET',json.url+'?'+json2url(json.data),true);
oAjax.send();
break;
case'post':
oAjax.open('POST',json.url,true);
oAjax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
oAjax.send(json2url(json.data));
break;
}
oAjax.onreadystatechange=function(){
if(oAjax.readyState==4){
if(oAjax.status==200){
json.success&&json.success(oAjax.responseText);
}else{
json.error&&json.error(oAjax.status);
}
}
};
}
//getStyle
functiongetStyle(obj,sName){
return(obj.currentStyle||getComputedStyle(obj,false))[sName];
}
//move
functionmove(obj,json,options){
varoptions=options||{};
options.duration=options.duration||700;
options.easing=options.easing||'ease-out';
varstart={};
vardis={};
for(varnameinjson){
start[name]=parseFloat(getStyle(obj,name));
dis[name]=json[name]-start[name];
}
varcount=Math.floor(options.duration/30);
varn=0;
clearInterval(obj.timer);
obj.timer=setInterval(function(){
n++;
for(varnameinjson){
switch(options.easing){
case'linear':
vara=n/count;
varcur=start[name]+dis[name]*a;
break;
case'ease-in':
vara=n/count;
varcur=start[name]+dis[name]*Math.pow(a,3);
break;
case'ease-out':
vara=1-n/count;
varcur=start[name]+dis[name]*(1-Math.pow(a,3));
break;
}
if(name=='opacity'){
obj.style.opacity=cur;
obj.style.filter='alpha(opacity:'+cur*100+')';
}else{
obj.style[name]=cur+'px';
}
}
if(n==count){
clearInterval(obj.timer);
}
},16);
}
//选择器
functiongetByClass(obj,sClass){
if(obj.getElementsByClassName){
returnobj.getElementsByClassName(sClass);
}
varaResult=[];
varaEle=obj.getElementsByTagName('*');
varre=newRegExp('\\b'+sClass+'\\b','g');
for(vari=0; i
if(aEle[i].className.search(re)!=-1){
aResult.push(aEle[i]);
}
}
returnaResult;
}
functiongetByStr(aParent,str){
varaChild=[];
for(vari=0; i
switch(str.charAt(0)){
case'#':
varobj=document.getElementById(str.substring(1));
aChild.push(obj);
break;
case'.':
varaEle=getByClass(aParent[i],str.substring(1));
for(varj=0; j
aChild.push(aEle[j]);
}
break;
default:
if(/^\w+\.\w+$/.test(str)){
//li.on
vararr=str.split('.');
varaEle=aParent[i].getElementsByTagName(arr[0]);
varre=newRegExp('\\b'+arr[1]+'\\b','g');
for(varj=0; j
if(aEle[j].className.search(re)!=-1){
aChild.push(aEle[j]);
}
}
}else if(/^\w+\[\w+\=\w+\]$/.test(str)){
//input[type=text]
vararr=str.split(/\[|\=|\]/g);
varaEle=aParent[i].getElementsByTagName(arr[0]);
for(varj=0; j
if(aEle[j].getAttribute(arr[1])==arr[2]){
aChild.push(aEle[j]);
}
}
}else if(/^\w+\:\w+(\(\d+\))?$/.test(str)){
//li:first li:eq(0)
vararr=str.split(/\:|\(|\)/g);
varaEle=aParent[i].getElementsByTagName(arr[0]);
switch(arr[1]){
case'first':
aChild.push(aEle[0]);
break;
case'last':
aChild.push(aEle[aEle.length-1]);
break;
case'even':
for(varj=0; j
aChild.push(aEle[j]);
}
break;
case'odd':
for(varj=1; j
aChild.push(aEle[j]);
}
break;
case'eq':
aChild.push(aEle[arr[2]]);
break;
case'lt':
for(varj=0; j
aChild.push(aEle[j]);
}
break;
case'gt':
for(varj=arr[2]; j
aChild.push(aEle[j]);
}
break;
}
}else{
varaEle=aParent[i].getElementsByTagName(str);
for(varj=0; j
aChild.push(aEle[j]);
}
}
break;
}
}
returnaChild;
}
functiongetEle(str){
vararr=str.replace(/^\s+|\s+$/g,'').split(/\s+/g);
varaChild=[];
varaParent=[document];
for(vari=0; i
aChild=getByStr(aParent,arr[i]);
aParent=aChild;
}
returnaChild;
}
网友评论