非常不错的一个javascript 类
复制代码 代码如下:
/*
*Author:aoao
*Homepage:http://www.loaoao.com
*Email:loaoao@gmail.com/QQ:2222342
*Copyright(c)2006aoao
*LicensedunderaCreativeCommonsAttribution2.5License(http://creativecommons.org/licenses/by/2.5/)
*/
varjscc=newObject();
varloaoao=newObject();//^_^//
jscc={
init:function(){/*_*/},
path:"/scripts/jscc/",
include:function(file){
varj=document.createElement("script");
j.setAttribute('type','text/javascript');
j.setAttribute('src',jscc.path+file+'.js');
document.getElementsByTagName("head")[0].appendChild(j);
}
};
jscc.addEvent=function(obj,type,fn){
if(obj.attachEvent){
obj['e'+type+fn]=fn;
obj[type+fn]=function(){obj['e'+type+fn](window.event);}
obj.attachEvent('on'+type,obj[type+fn]);
}else{
obj.addEventListener(type,fn,false);
}
};
//Cookies
jscc.cookie={
create:function(name,value,days){
if(days){
vardate=newDate();
date.setTime(date.getTime()+(days*24*60*60*1000));
varexpires=";expires="+date.toGMTString();
}
elseexpires="";
document.cookie=name+"="+value+expires+";path=/";
},
read:function(name){
varnameEQ=name+"=";
varca=document.cookie.split(';');
for(vari=0;i<ca.length;i++){
varc=ca[i];
while(c.charAt(0)=='')c=c.substring(1,c.length);
if(c.indexOf(nameEQ)==0)returnc.substring(nameEQ.length,c.length);
}
returnnull;
}
};
jscc.getPageSize=function(){
//referencelightbox(http://www.huddletogether.com/projects/lightbox/lightbox.js)
vartheWidth,theHeight;
if(window.innerHeight&&window.scrollMaxY){
theWidth=window.innerWidth;
theHeight=window.innerHeight+window.scrollMaxY;
}
elseif(document.body){
theWidth=((document.body.scrollWidth>document.body.offsetWidth)?document.body.scrollWidth:document.body.offsetWidth);
theHeight=((document.body.scrollHeight>document.body.offsetHeight)?document.body.scrollHeight:document.body.offsetHeight);
}
varwinWidth,winHeight;
if(self.innerHeight){
winWidth=self.innerWidth;
winHeight=self.innerHeight;
}elseif(document.documentElement&&document.documentElement.clientHeight){
winWidth=document.documentElement.clientWidth;
winHeight=document.documentElement.clientHeight;
}elseif(document.body){
winWidth=document.body.clientWidth;
winHeight=document.body.clientHeight;
}
varpWidth=(theWidth>winWidth?theWidth:winWidth)
varpHeight=(theHeight>winHeight?theHeight:winHeight)
varyScroll;
if(self.pageYOffset){
yScroll=self.pageYOffset;
}elseif(document.documentElement&&document.documentElement.scrollTop){//Explorer6Strict
yScroll=document.documentElement.scrollTop;
}elseif(document.body){//allotherExplorers
yScroll=document.body.scrollTop;
}
vargetPageSize=newArray(pWidth,pHeight,winWidth,winHeight,yScroll)
returngetPageSize;
}
jscc.widgets={/**/};
jscc.widgets.fixedsidebar={
fixedHeight:0,
oldScrolltop:0,
init:function(){
if(!document.getElementById("sidebar"))return;
jscc.addEvent(window,"scroll",function(e){jscc.widgets.fixedsidebar.setFixed()});
jscc.addEvent(window,"resize",function(e){jscc.widgets.fixedsidebar.setFixed()});
this.fixedHeight=document.getElementById("sidebar").offsetTop;
this.setFixed();
},
setFixed:function(){
varsidebar=document.getElementById("sidebar");
varref=document.getElementById("main");
vars_t=sidebar.offsetTop;
vars_h=sidebar.offsetHeight;
vars_th=s_t+s_h;
varr_th=ref.offsetTop+ref.offsetHeight;
vard_st;
if(document.documentElement.scrollTop){
d_st=document.documentElement.scrollTop;
}
else{
d_st=document.body.scrollTop
}
if(r_th>(s_t+s_h)){
varseHeight=0;
if(self.innerHeight){seHeight=self.innerHeight;}elseif(document.documentElement&&document.documentElement.clientHeight){seHeight=document.documentElement.clientHeight;}elseif(document.body){seHeight=document.body.clientHeight;}
try{
if(seHeight>s_h){
varnewtop=(d_st<this.fixedHeight?0:d_st-this.fixedHeight-42);
}
else{
//Thankyoudron(http://www.ucren.com/)
switch(d_st-this.oldScrolltop>0)
{
casetrue:
if(d_st-this.fixedHeight>s_t-seHeight){
varnewtop=d_st-this.fixedHeight-(s_h-seHeight)-68;
}
break;
casefalse:
if(d_st-this.fixedHeight<parseInt(sidebar.style.marginTop,10)){
varnewtop=d_st-this.fixedHeight;
}
break;
}
}
this.oldScrolltop=d_st;
if(newtop==undefined){return;};
newtop=(newtop<0?0:newtop);
sidebar.style.marginTop=newtop+"px";
}
catch(e){
//alert(e.description);
}
}
else{
sidebar.style.marginTop="0px";
}
}
};
//jscc.addEvent(window,"load",function(e){jscc.widgets.fixedsidebar.init()});
jscc.widgets.wraphandler={
//jscc.widgets.wraphandlerreferencehttp://www.collylogic.com/includes/resizer.js(http://www.collylogic.com/?/comments/redesign-notes-1-width-based-layout/)
init:function(){
//if(!document.getElementById)return;
if(document.getElementById('wrapper').className=="col3"){
jscc.wraphandler.setWrapper();
jscc.addEvent(window,"resize",jscc.wraphandler.setWrapper);
}
else{return}
},
setWrapper:function(){
vargetPS=jscc.getPageSize();
var_width=getPS[2];
if(_width<940){
document.getElementById('wrapper').className='col2';
}else{
document.getElementById('wrapper').className='col3';
}
if(document.all){
if(_width<780){
document.getElementById('wrapper').style.width="750px"
}else{
document.getElementById('wrapper').style.width="auto"
}
}
}
};
//jscc.addEvent(window,"load",function(e){jscc.widgets.wraphandler.init()});
jscc.widgets.styleSwitcher={
//jscc.widgets.styleSwitcherreferenceStyleSwitcherfunctions(writtenbyPaulSowdenhttp://www.idontsmoke.co.uk/ss/.Forthedetails,visitALA:http://www.alistapart.com/stories/alternate/)
init:function(e){
varcookie=jscc.cookie.read("style");
vartitle=cookie?cookie:this.getPreferred();
title=(title==null?"left":title);
this.setActive(title);
varoperactioner=document.getElementById("themes").getElementsByTagName("A");
operactioner[0].onclick=function(){
jscc.widgets.styleSwitcher.setActive("left");
returnfalse;
};
operactioner[1].onclick=function(){
jscc.widgets.styleSwitcher.setActive("right");
returnfalse;
};
this.switcher(title);
},
setActive:function(title){
vari,a,main;
for(i=0;(a=document.getElementsByTagName("link")[i]);i++){
if(a.getAttribute("rel").indexOf("style")!=-1&&a.getAttribute("title")){
a.disabled=true;
if(a.getAttribute("title")==title)a.disabled=false;
}
}
this.switcher(title);
//jscc.cookie.create("style",title,"365");
},
getActive:function(){
vari,a;
for(i=0;(a=document.getElementsByTagName("link")[i]);i++){
if(a.getAttribute("rel").indexOf("style")!=-1&&a.getAttribute("title")&&!a.disabled)returna.getAttribute("title");}
returnnull;
},
getPreferred:function(){
vari,a;
for(i=0;(a=document.getElementsByTagName("link")[i]);i++){
if(a.getAttribute("rel").indexOf("style")!=-1
&&a.getAttribute("rel").indexOf("alt")==-1
&&a.getAttribute("title")
)returna.getAttribute("title");
}
returnnull;
},
switcher:function(theme){
if(!document.getElementById(["theme_"+theme])){return;}
varnotheme=(theme=="left"?"right":"left");
document.getElementById(["theme_"+notheme]).style.display="inline";
document.getElementById(["theme_"+theme]).style.display="none";
},
end:function(e){
vartitle=this.getActive();
jscc.cookie.create("style",title,"365");
}
};
//jscc.addEvent(window,"load",function(e){jscc.widgets.styleSwitcher.init()});
jscc.addEvent(window,"unload",function(e){jscc.widgets.styleSwitcher.end()});
jscc.widgets.toTop={
init:function(){
document.getElementById("toTop").onclick=function(e){
jscc.widgets.toTop.set();
returnfalse;
}
},
waitTimer:null,
set:function(){
vard_st=document.documentElement.scrollTop;
if(window.navigator.userAgent.indexOf("MSIE")>=1){
for(vari=d_st;i>10;i-=Math.floor(i/6)){
window.scrollTo(0,i);
}
window.scrollTo(0,10);
}
else{
window.scrollTo(0,Math.floor(d_st/2));
if(d_st>10){
waitTimer=setTimeout("jscc.widgets.toTop.set()",20);
}
else{
clearTimeout(waitTimer);
}
}
}
}
jscc.api={
google:{
init:function(){
try{
varsearchControl=newGSearchControl();
}
catch(e){
return;
}
varoptions=newGdrawOptions();
options.setSearchFormRoot(document.getElementById("googleSearchForm"));
options.setDrawMode(GSearchControl.DRAW_MODE_TABBED);
searchControl.setLinkTarget(GSearch.LINK_TARGET_SELF);
searchControl.setResultSetSize(GSearch.LARGE_RESULTSET);
varsiteSearch=newGwebSearch();
siteSearch.setUserDefinedLabel("站内搜索");
siteSearch.setUserDefinedClassSuffix("siteSearch");
siteSearch.setSiteRestriction("www.loaoao.com");
searchControl.addSearcher(siteSearch);
varlabSearch=newGwebSearch();
labSearch.setUserDefinedLabel("嗷嗷的实验室搜索");
labSearch.setUserDefinedClassSuffix("labSearch");
labSearch.setSiteRestriction("lab.loaoao.com");
searchControl.addSearcher(labSearch);
varwwwSearch=newGwebSearch();
wwwSearch.setUserDefinedLabel("Google搜索");
labSearch.setUserDefinedClassSuffix("wwwSearch");
searchControl.addSearcher(wwwSearch);
searchControl.draw(document.getElementById("googleSearchResults"),options);
//searchControl.execute("css");
}
}
}
loaoao.com=function(){
if(!document.getElementById)return;
jscc.widgets.styleSwitcher.init();
jscc.widgets.fixedsidebar.init();
jscc.widgets.toTop.init();
jscc.api.google.init();
};
jscc.addEvent(window,"load",loaoao.com);