非常不错的一个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 相关文章推荐
JavaScript 实现类的多种方法实例
May 01 Javascript
浅析jquery某一元素重复绑定的问题
Jan 03 Javascript
JavaScript Window浏览器对象模型方法与属性汇总
Apr 20 Javascript
详解JavaScript基于面向对象之继承
Dec 13 Javascript
js操作cookie保存浏览记录的方法
Dec 25 Javascript
vue.js删除动态绑定的radio的指定项
Jun 02 Javascript
JS实现页面内跳转的简单代码
Sep 03 Javascript
无限循环轮播图之运动框架(原生JS实现)
Oct 01 Javascript
jQuery插件jsonview展示json数据
May 26 jQuery
详解用JS添加和删除class类名
Mar 25 Javascript
extjs4图表绘制之折线图实现方法分析
Mar 06 Javascript
vue3.0中使用element的完整步骤
Mar 04 Vue.js
不错的一个日期输入 动态
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
DC动画电影《黑暗正义联盟》曝预告 5月5日上线数字平台
2020/04/09 欧美动漫
用PHP连接MySQL代码的参数说明
2008/06/07 PHP
Smarty中常用变量操作符汇总
2014/10/27 PHP
php解析xml方法实例详解
2015/05/12 PHP
php模板引擎技术简单实现
2016/03/15 PHP
PHP获取指定时间段之间的 年,月,天,时,分,秒
2016/06/05 PHP
php分页查询mysql结果的base64处理方法示例
2017/05/18 PHP
php设计模式之观察者模式定义与用法经典示例
2019/09/19 PHP
PHP基于进程控制函数实现多线程
2020/12/09 PHP
jquery 全局AJAX事件使用代码
2010/11/05 Javascript
5分钟理解JavaScript中this用法分享
2013/11/09 Javascript
JS小功能(checkbox实现全选和全取消)实例代码
2013/11/28 Javascript
javascript实现汉字转拼音代码分享
2015/04/20 Javascript
JavaScript常用判断写法大全(推荐)
2016/05/30 Javascript
JS简单实现无缝滚动效果实例
2016/08/24 Javascript
微信小程序 条件渲染详解
2016/10/09 Javascript
使用ES6语法重构React代码详解
2017/05/09 Javascript
vue elementui form表单验证的实现
2018/11/11 Javascript
使用Vue-Awesome-Swiper实现旋转叠加轮播效果&amp;平移轮播效果
2019/08/16 Javascript
[02:23]2014DOTA2国际邀请赛中国战队回顾
2014/08/01 DOTA
python监控网卡流量并使用graphite绘图的示例
2014/04/27 Python
python使用sorted函数对列表进行排序的方法
2015/04/04 Python
wxPython使用系统剪切板的方法
2015/06/16 Python
python从入门到精通(DAY 3)
2015/12/20 Python
python TF-IDF算法实现文本关键词提取
2019/05/29 Python
python 如何区分return和yield
2020/09/22 Python
美国演唱会订票网站:Ticketmaster美国
2017/10/05 全球购物
马来西亚和新加坡巴士票在线预订:CatchThatBus
2018/11/17 全球购物
英国首屈一指的票务公司:See Tickets
2019/05/11 全球购物
如何写你的创业计划书
2014/01/07 职场文书
期中考试后的反思
2014/02/08 职场文书
2014年小学校长工作总结
2014/12/08 职场文书
学习经验交流会策划书
2015/11/02 职场文书
廉洁自律准则学习心得体会
2016/01/13 职场文书
大学迎新生的欢迎词
2019/06/25 职场文书
php将xml转化对象的实例详解
2021/11/17 PHP