• javascript笔记,手写简易底层库


    javascript作为编程语言,虽然灵活,但内置函数过少、不同浏览器存在兼容系问题,所以自己学习写一个简易的底层库对理解这门语言是非常重要的。下面的代码是根据某教程改写的。现重新备份如下

      1 //20:33 2014/1/19 星期日
      2 //base层:a兼容性处理;b扩展接口
      3 //liulele622@163.com
      4 //可以使用命名空间,诸如GLOBAL.Dom GLOBAL.Event GLOBAL.lang 
      5 
      6 
      7 var GLOBAL={};
      8 GLOBAL.namespace=function(str){
      9     var arr=str.split("."), o=GLOBAL;
     10     for(i=(arr[0]=="GLOBAL")?1:0; i < arr.length; i++){
     11         o[arr[i]] = o[arr[i]] || {};
     12         o = o[arr[i]];
     13     }
     14 }
     15 
     16 /**获取下一个元素**/
     17 //Firefox下nextSibling把空白也当成元素;而IE下仅仅是DOM元素
     18 function getNextNode(node){
     19     node = typeof node == "string" ? document.getElementById(node) : node;
     20     var nextNode = node.nextSibling;
     21     if(!nextNode) return null;
     22     if(!document.all){ //FF not supports
     23         while(true){
     24             if(nextNode.nodeType == 1){
     25                 break;
     26             }else{
     27                 if(nextNode.nextSibling){
     28                     nextNode = nextNode.nextSibling;
     29                 }else{
     30                     break;
     31                 }
     32             }
     33         }
     34     }
     35     return nextNode;
     36 }
     37 
     38 /**透明度**/
     39 function setOpacity(node,level){
     40     node = typeof node == "string" ? document.getElementById(node) : node;
     41     if(document.all){
     42         node.style.filter = 'alpha(opacity = ' + level + ')';
     43     }else{
     44         node.style.opacity = level / 100;
     45     }
     46 }
     47 
     48 /*event*/
     49 function getEventTarget(e){
     50     e = window.event || e;  //IE || FF
     51     return e.srcElement || e.target;
     52 }
     53 
     54 /**阻止事件冒泡**/
     55 function stopPropagation(e){
     56     e = window.event || e;
     57     if(document.all){
     58         e.cancelBubble = true;
     59     }else{
     60         e.stopPropagation();
     61     }
     62 }
     63 
     64 /**同时监听多个事件**/
     65 function on(node,eventType,handler){
     66     node = typeof node == "string" ? document.getElementById(node) : node;
     67     if(document.all){
     68         node.attachEvent("on" + eventType, handler);
     69     }else{
     70         node.addEventListener(eventType, handler, false);
     71     }
     72 }
     73 
     74 /**去除首位空白**/
     75 function trim(ostr){
     76     return ostr.replace(/^s+|s+$/g,"");
     77 }
     78 
     79 /**判断是否为某类型**/
     80 function isNumber(s){
     81     return !isNaN(s);
     82 }
     83 function isString(s){
     84     return typeof s === "string";
     85 }
     86 
     87 function isBoolean(s){
     88     return typeof s === "boolean";
     89 }
     90 
     91 function isFunction(s){
     92     return typeof s === "function";
     93 }
     94 
     95 function isNull(s){
     96     return s === null;
     97 }
     98 
     99 function isUndefined(s){
    100     return typeof s === "undefined";
    101 }
    102 
    103 function isEmpty(s){
    104     return /^s*$/.test(s);
    105 }
    106 
    107 function isArray(s){
    108     return s instanceof Array;
    109 }
    110   
    111 /**get**/
    112 function get(node){
    113     node = typeof node == "string" ? document.getElementById(node) : node;
    114     return node;
    115 }
    116 function $(node){
    117     node = typeof node == "string" ? document.getElementById(node) : node;
    118     return node;
    119 }
    120 
    121 /**getElementsByClassName**/
    122 function getElementsByClassName(str,root,tag){
    123     if(root){
    124         root = typeof root == "string" ? document.getElementById(root) : root;
    125     }else{
    126         root = document.body;
    127     }
    128     tag = tag || "*";
    129     var els = root.getElementsByTagName(tag), arr=[];
    130     for(var i = 0, n = els.length; i < n; i++){
    131         for(var j = 0, k = els[i].className.split(" "), l = k.length; j < l; j++){
    132             if(k[j] === str){
    133                 arr.push(els[i]);
    134                 break;
    135             }
    136         }
    137     }
    138     return arr;
    139 }
    140 
    141 /**extend**/
    142 function extend(subClass,superClass){
    143     var F = function(){};
    144     F.prototype = superClass.prototype;
    145     subClass.prototype = new F();
    146     subClass.prototype.constructor = subClass;
    147     subClass.superclass = superClass.prototype;
    148     if(superClass.prototype.constructor == Object.prototype.constructor){
    149         superClass.prototype.constructor = superClass;
    150     }
    151 }
    152 
    153 /****/
    //20:33 2014/1/19 星期日
    //base层:a兼容性处理;b扩展接口
    //liulele622@163.com
    //可以使用命名空间,诸如GLOBAL.Dom GLOBAL.Event GLOBAL.lang 
    
    /**cookie操作**/
    GLOBAL.namespace("Cookie");
    GLOBAL.Cookie = {
        read : function(name){
            var cookieStr = "; " + document.cookie + "; ";
            var index = cookieStr.indexOf("; " + name + "=");
            //console.log(document.cookie);
            if(index != -1){
                var s = cookieStr.substring(index + name.length + 3, cookieStr.length);
                return unescape(s.substring(0,s.indexOf("; ")));
            }else{
                return null;
            }
        },
        set : function(name, value, expires){
            var expDays = expires * 24 * 60 * 60 * 1000;
            var expDate = new Date();
            expDate.setTime(expDate.getTime() + expDays);
            var expString = expires ? "; expires=" + expDate.toGMTString() : "";
            var pathString = "; path=/";
            cookie = name + "=" + escape(value) + expString + pathString;
            document.cookie  = cookie;
            //console.log(cookie);
        },
        del: function(name){
            var exp = new Date(new Date().getTime() - 1);
            var s = this.read(name);
            if(s != null){
                document.cookie = name + "=" + s + "; expires=" + exp.toGMTString() + "; path=/";
            }
        }
    }
    
    /**Ajax**/
    GLOBAL.namespace("Ajax");
    GLOBAL.Ajax = function(url,fnSucc,fnFaild){
        //创建ajax对象
        if(window.XMLHttpRequest){
            var oAjax = new XMLHttpRequest();
        }else{
            var oAjax = ActiveXObject("Microsoft.XMLHTTP");
        }
        //连接服务器
        oAjax.open('get',url,true);
        //发送
        oAjax.send();
        //接收
        oAjax.onreadystatechange = function(){
            if(oAjax.readyState == 4){
                if(oAjax.status == 200){
                    fnSucc(oAjax.responseText);
                }else{
                    if(fnFaild){
                        fnFaild();
                    }
                }
            }
        }
        
    }
    
    /**运动**/
    GLOBAL.namespace("Move");
    GLOBAL.Move = {
        getStyle: function(obj,name){
            if(obj.currentStyle){
                return obj.currentStyle[name];
            }else{
                    return getComputedStyle(obj,false)[name];
            }
        },
        startMove: function(obj,json,fnEnd){
            clearInterval(obj.timer);
            obj.timer = setInterval(function(){
                var bStop = true;
                for(var attr in json){
                    var cur = 0;
                    if(attr =='opacity'){
                        cur = Math.round(parseFloat(GLOBAL.Move.getStyle(obj,attr))*100);
                    }else{
                        cur = parseInt(GLOBAL.Move.getStyle(obj,attr));
                    }
                    var speed=(json[attr]-cur)/6;
                    speed = speed>0 ? Math.ceil(speed) : Math.floor(speed);
                    console.log("%s,%s",cur,json[attr]);
                    if(cur != json[attr])        
                        bStop = false;
                    if(attr == 'opacity'){
                        obj.style.filter='alpha(opacity:'+(cur+speed)+')';
                        obj.style.opacity=(cur+speed)/100;
                    }else{
                        obj.style[attr] = cur + speed + 'px';
                    }
                }
                if(bStop){
                    clearInterval(obj.timer);
                    if(fnEnd) fnEnd();
                }
            }, 30);
        }
        
    }
  • 相关阅读:
    Delphi 之Copyrect的使用
    Delphi GDI对象之脱屏位图(Offscreen Bitmaps),也叫内存位图
    1067. Sort with Swap(0,*) (25)【贪心】——PAT (Advanced Level) Practise
    Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds
    每一个JavaScript开发者都应该知道的10道面试题
    【HDOJ 5407】 CRB and Candies (大犇推导
    Wireshark默认不抓取本地包的解决方式
    Android LaunchMode案例篇
    使用ViewPager实现广告滑动效果
    剑指offer面试题26-复杂链表的复制
  • 原文地址:https://www.cnblogs.com/liulele/p/7052384.html
Copyright © 2020-2023  润新知