非常不错的一个javascript 类
复制代码 代码如下:
/*
* author:aoao
* homepage:http://www.loaoao.com
* email:loaoao@gmail.com / qq:2222342
* copyright (c) 2006 aoao
* licensed under a creative commons attribution 2.5 license (http://creativecommons.org/licenses/by/2.5/)
*/
var jscc = new object();
var loaoao = new object();//^_^//
jscc = {
init:function(){/*_*/},
path:/scripts/jscc/,
include:function (file){
var j=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) {
var date = new date();
date.settime(date.gettime()+(days*24*60*60*1000));
var expires = ; expires=+date.togmtstring();
}
else expires = ;
document.cookie = name+=+value+expires+; path=/;
},
read:function(name) {
var nameeq = name + =;
var ca = document.cookie.split(';');
for(var i=0;i var c = ca[i];
while (c.charat(0)==' ') c = c.substring(1,c.length);
if (c.indexof(nameeq) == 0) return c.substring(nameeq.length,c.length);
}
return null;
}
};
jscc.getpagesize=function(){
// reference lightbox (http://www.huddletogether.com/projects/lightbox/lightbox.js)
var thewidth,theheight;
if (window.innerheight&&window.scrollmaxy) {
thewidth = window.innerwidth;
theheight = window.innerheight+ window.scrollmaxy;
}
else if (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 );
}
var winwidth ,winheight;
if (self.innerheight) {
winwidth = self.innerwidth;
winheight = self.innerheight;
} else if (document.documentelement && document.documentelement.clientheight) {
winwidth = document.documentelement.clientwidth;
winheight = document.documentelement.clientheight;
} else if (document.body) {
winwidth = document.body.clientwidth;
winheight = document.body.clientheight;
}
var pwidth=(thewidth>winwidth?thewidth:winwidth)
var pheight=(theheight>winheight?theheight:winheight)
var yscroll;
if (self.pageyoffset) {
yscroll = self.pageyoffset;
} else if (document.documentelement && document.documentelement.scrolltop){ // explorer 6 strict
yscroll = document.documentelement.scrolltop;
} else if (document.body) {// all other explorers
yscroll = document.body.scrolltop;
}
var getpagesize = new array(pwidth,pheight,winwidth,winheight,yscroll)
return getpagesize;
}
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(){
var sidebar=document.getelementbyid(sidebar);
var ref=document.getelementbyid(main);
var s_t=sidebar.offsettop;
var s_h=sidebar.offsetheight;
var s_th=s_t+s_h;
var r_th=ref.offsettop+ref.offsetheight;
var d_st;
if(document.documentelement.scrolltop){
d_st=document.documentelement.scrolltop;
}
else{
d_st=document.body.scrolltop
}
if(r_th>(s_t+s_h)){
var seheight=0;
if (self.innerheight) { seheight = self.innerheight;} else if (document.documentelement && document.documentelement.clientheight) { seheight = document.documentelement.clientheight;} else if (document.body) {seheight = document.body.clientheight;}
try{
if(seheight>s_h){
var newtop=(d_st }
else{
// thank you dron (http://www.ucren.com/)
switch (d_st-this.oldscrolltop>0)
{
case true:
if (d_st-this.fixedheight > s_t -seheight){
var newtop= d_st -this.fixedheight - (s_h-seheight)-68;
}
break;
case false:
if (d_st-this.fixedheight var newtop= d_st-this.fixedheight;
}
break;
}
}
this.oldscrolltop = d_st;
if(newtop==undefined){ return;};
newtop=(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.wraphandler reference http://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() {
var getps=jscc.getpagesize();
var _width=getps[2];
if (_width document.getelementbyid('wrapper').classname = 'col2';
} else {
document.getelementbyid('wrapper').classname = 'col3';
}
if(document.all) {
if (_width 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.styleswitcher reference styleswitcher functions( written by paul sowden http://www.idontsmoke.co.uk/ss/ . for the details, visit ala: http://www.alistapart.com/stories/alternate/)
init:function(e) {
var cookie = jscc.cookie.read(style);
var title = cookie ? cookie : this.getpreferred();
title = (title==null?left:title);
this.setactive(title);
var operactioner = document.getelementbyid(themes).getelementsbytagname(a);
operactioner[0].onclick=function() {
jscc.widgets.styleswitcher.setactive(left);
return false;
};
operactioner[1].onclick=function() {
jscc.widgets.styleswitcher.setactive(right);
return false;
};
this.switcher(title);
},
setactive:function(title){
var i, 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(){
var i, a;
for(i=0; (a = document.getelementsbytagname(link)[i]); i++) {
if(a.getattribute(rel).indexof(style) != -1 && a.getattribute(title) && !a.disabled) return a.getattribute(title);}
return null;
},
getpreferred:function (){
var i, 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)
) return a.getattribute(title);
}
return null;
},
switcher:function(theme){
if(!document.getelementbyid([theme_+theme])) { return;}
var notheme=(theme==left?right:left);
document.getelementbyid([theme_+notheme]).style.display=inline;
document.getelementbyid([theme_+theme]).style.display=none;
},
end:function(e){
var title = 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();
return false;
}
},
waittimer:null,
set:function(){
var d_st=document.documentelement.scrolltop;
if(window.navigator.useragent.indexof(msie)>=1){
for (var i=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{
var searchcontrol = new gsearchcontrol();
}
catch(e){
return;
}
var options = new gdrawoptions();
options.setsearchformroot(document.getelementbyid(googlesearchform));
options.setdrawmode(gsearchcontrol.draw_mode_tabbed);
searchcontrol.setlinktarget(gsearch.link_target_self);
searchcontrol.setresultsetsize(gsearch.large_resultset);
var sitesearch = new gwebsearch();
sitesearch.setuserdefinedlabel(站内搜索);
sitesearch.setuserdefinedclasssuffix(sitesearch);
sitesearch.setsiterestriction(www.loaoao.com);
searchcontrol.addsearcher(sitesearch);
var labsearch = new gwebsearch();
labsearch.setuserdefinedlabel(嗷嗷的实验室搜索);
labsearch.setuserdefinedclasssuffix(labsearch);
labsearch.setsiterestriction(lab.loaoao.com);
searchcontrol.addsearcher(labsearch);
var wwwsearch = new gwebsearch();
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);