非常不错的一个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 相关文章推荐
excel操作之Add Data to a Spreadsheet Cell
Jun 12 Javascript
说明你的Javascript技术很烂的五个原因
Apr 26 Javascript
JQuery扩展插件Validate—6 radio、checkbox、select的验证
Sep 05 Javascript
jquery得到font-size属性值实现代码
Sep 30 Javascript
jQuery插件制作之全局函数用法实例
Jun 01 Javascript
AngularJS监听路由的变化示例代码
Sep 23 Javascript
JavaScript制作弹出层效果
Dec 02 Javascript
vue.js获取数据库数据实例代码
May 26 Javascript
页面内锚点定位及跳转方法总结(推荐)
Apr 24 Javascript
JS如何把字符串转换成json
Feb 21 Javascript
vue自动添加浏览器兼容前后缀操作
Aug 13 Javascript
html+vue.js 实现漂亮分页功能可兼容IE
Nov 07 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 和 MySQL 基础教程(二)
2006/10/09 PHP
WindowsXP中快速配置Apache+PHP5+Mysql
2008/06/05 PHP
php数组函数序列之array_slice() - 在数组中根据条件取出一段值,并返回
2011/11/07 PHP
PHP使用SOAP调用.net的WebService数据
2013/11/12 PHP
destoon利用Rewrite规则设置网站安全
2014/06/21 PHP
php及codeigniter使用session-cookie的方法(详解)
2017/04/06 PHP
通过修改Laravel Auth使用salt和password进行认证用户详解
2017/08/17 PHP
阿里云的WindowsServer2016上部署php+apache
2018/07/17 PHP
js实现的真正的iframe高度自适应(兼容IE,FF,Opera)
2010/03/07 Javascript
jquery获取input表单值的代码
2010/04/19 Javascript
(jQuery,mootools,dojo)使用适合自己的编程别名命名
2010/09/14 Javascript
关于js注册事件的常用方法
2013/04/03 Javascript
现如今最流行的JavaScript代码规范
2014/03/08 Javascript
JavaScript中的ParseInt(&quot;08&quot;)和“09”返回0的原因分析及解决办法
2016/05/19 Javascript
JS与Ajax Get和Post在使用上的区别实例详解
2016/06/08 Javascript
JavaScript的Object.defineProperty详解
2018/07/09 Javascript
vue debug 二种方法
2018/09/16 Javascript
[01:21]DOTA2 新英雄 森海飞霞
2020/12/18 DOTA
Python实现识别手写数字 Python图片读入与处理
2020/03/23 Python
使用 Python 实现简单的 switch/case 语句的方法
2018/09/17 Python
python 实现分页显示从es中获取的数据方法
2018/12/26 Python
python实现弹跳小球
2019/05/13 Python
计算机二级python学习教程(3) python语言基本数据类型
2019/05/16 Python
bluepy 一款python封装的BLE利器简单介绍
2019/06/25 Python
Python中的单下划线和双下划线使用场景详解
2019/09/09 Python
python修改文件内容的3种方法详解
2019/11/15 Python
如何基于python3和Vue实现AES数据加密
2020/03/27 Python
linux mint中搜狗输入法导致pycharm卡死的问题
2020/10/28 Python
浅谈CSS3中的变形功能-transform功能
2017/12/27 HTML / CSS
canvas使用注意点总结
2013/07/19 HTML / CSS
出生公证书样本
2014/04/04 职场文书
教师个人师德总结
2015/02/06 职场文书
员工工作表扬信
2015/05/05 职场文书
介绍信应该怎么开?
2019/04/03 职场文书
创业计划书之情侣餐厅
2019/09/29 职场文书
导游词之五台山
2019/10/11 职场文书