非常不错的一个javascript 类


Posted in Javascript onNovember 07, 2006

非常不错的一个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 < 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.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<this.fixedHeight?0:d_st-this.fixedHeight-42); 
                } 
                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<parseInt(sidebar.style.marginTop,10)){ 
                        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 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);
Javascript 相关文章推荐
自定义的一个简单时尚js下拉选择框
Nov 20 Javascript
jQuery模拟点击A标记示例参考
Apr 17 Javascript
Javascript学习笔记之函数篇(四):arguments 对象
Nov 23 Javascript
javascript里使用php代码实例
Dec 13 Javascript
浅析javascript 定时器
Dec 23 Javascript
基于JavaScript代码实现自动生成表格
Jun 15 Javascript
Javascript类型系统之String字符串类型详解
Jun 21 Javascript
jQuery实现的浮动层div浏览器居中显示效果
Feb 03 Javascript
浅谈vue引入css,less遇到的坑和解决方法
Jan 20 Javascript
Vue中对拿到的数据进行A-Z排序的实例
Sep 25 Javascript
JavaScript遍历数组和对象的元素简单操作示例
Jul 09 Javascript
使用konva和vue-konva库实现拖拽滑块验证功能
Apr 27 Javascript
不错的一个日期输入 动态
Nov 06 #Javascript
由prototype_1.3.1进入javascript殿堂-类的初探
Nov 06 #Javascript
javascript读取xml
Nov 04 #Javascript
用javascript操作xml
Nov 04 #Javascript
一个加密JavaScript的开源工具PACKER2.0.2
Nov 04 #Javascript
破除一些网站复制、右键限制
Nov 04 #Javascript
js计数器代码
Nov 04 #Javascript
You might like
PHP下一个非常全面获取图象信息的函数
2008/11/20 PHP
关于php正则匹配汉字的方法介绍
2013/04/25 PHP
基于PHP静态类的原罪详解
2013/05/06 PHP
PHP封装的数据库模型Model类完整示例【基于PDO】
2019/03/14 PHP
csdn 论坛技术区平均给分功能
2009/11/07 Javascript
javascript循环变量注册dom事件 之强大的闭包
2010/09/08 Javascript
AngularJS基础学习笔记之表达式
2015/05/10 Javascript
js运动应用实例解析
2015/12/28 Javascript
NodeJS和BootStrap分页效果的实现代码
2016/11/07 NodeJs
AngularJS实现页面定时刷新
2017/03/14 Javascript
原生js简单实现放大镜特效
2017/05/16 Javascript
微信小程序 密码输入(源码下载)
2017/06/27 Javascript
JS SetInterval 代码实现页面轮询
2017/08/11 Javascript
简单的网页广告特效实例
2017/08/19 Javascript
Vue实现typeahead组件功能(非常靠谱)
2017/08/26 Javascript
JS设计模式之观察者模式实现实时改变页面中金额数的方法
2018/02/05 Javascript
浅析vue cli3 封装Svgicon组件正确姿势(推荐)
2020/04/27 Javascript
微信小程序轮播图swiper代码详解
2020/12/01 Javascript
Python编程语言的35个与众不同之处(语言特征和使用技巧)
2014/07/07 Python
在Django的URLconf中使用多个视图前缀的方法
2015/07/18 Python
Python中的字典与成员运算符初步探究
2015/10/13 Python
Python socket网络编程TCP/IP服务器与客户端通信
2017/01/05 Python
python编程羊车门问题代码示例
2017/10/25 Python
对numpy中array和asarray的区别详解
2018/04/17 Python
使用python进行拆分大文件的方法
2018/12/10 Python
Python叠加两幅栅格图像的实现方法
2019/07/05 Python
PyCharm汉化安装及永久激活详细教程(靠谱)
2020/01/16 Python
文件中有一组整数,要求排序后输出到另一个文件中
2012/01/04 面试题
学生不讲诚信检讨书
2014/09/29 职场文书
党员批评与自我批评发言材料
2014/10/14 职场文书
兵马俑的导游词
2015/02/02 职场文书
尼克胡哲观后感
2015/06/08 职场文书
《画家和牧童》教学反思
2016/02/17 职场文书
《小摄影师》教学反思
2016/02/18 职场文书
导游词之井冈山
2019/11/20 职场文书
世界各国短波电台对东亚播送时间频率表(SW)
2021/06/28 无线电