A very good javascript class _ javascript Technique

Source: Internet
Author: User
Tags creative commons attribution
A very good javascript class. A very good javascript class.

The Code is as follows:


/*
* 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 (); // pai_^ //
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 * 24x60*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 <ca. length; 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.doc umentElement & document.doc umentElement. clientHeight ){
WinWidth = document.doc umentElement. clientWidth;
WinHeight = document.doc umentElement. 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.doc umentElement & document.doc umentElement. scrollTop) {// Explorer 6 Strict
YScroll = document.doc umentElement. scrollTop;
} Else if (document. body) {// all other referers
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.doc umentElement. scrollTop ){
D_st=document.doc umentElement. 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.doc umentElement & document.doc umentElement. clientHeight) {seHeight = document.doc umentElement. 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 <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. 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 <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. styleSwitcher reference StyleSwitcher funcher (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 ("");
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,;
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,;
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.doc umentElement. 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 ("intra-Site Search ");
SiteSearch. setUserDefinedClassSuffix ("siteSearch ");
SiteSearch. setSiteRestriction ("www.loaoao.com ");
SearchControl. addSearcher (siteSearch );
Var labSearch = new GwebSearch ();
LabSearch. setUserDefinedLabel ("Laboratory search for zookeeper ");
LabSearch. setUserDefinedClassSuffix ("labSearch ");
LabSearch. setSiteRestriction ("lab.loaoao.com ");
SearchControl. addSearcher (labSearch );
Var wwwSearch = new GwebSearch ();
WwwSearch. setUserDefinedLabel ("Google Search ");
LabSearch. setUserDefinedClassSuffix ("wwwSearch ");
SearchControl. addSearcher (wwwSearch );

SearchControl. draw (document. getElementById ("googleSearchResults"), options );
// SearchControl.exe cute ("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 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.