オリジナルjavascriptは自分でカプセル化する常用の小さい方法です.

39587 ワード

var zycTools = {
    //  css         
    $:function(elem){
        return document.querySelector(elem);
    },
    //  css         
    $$:function(elem){
        return document.querySelectorAll(elem);
    },
    //       
    browserType:function(){
        var browser = window.navigator.userAgent; //     
        if (userAgent.indexOf("Opera") > -1) {
        return "Opera"
        }; //    Opera   
        if (userAgent.indexOf("Firefox") > -1) {
            return "FireFox";
        } //    Firefox   
        if (userAgent.indexOf("Chrome") > -1){
            return "Chrome";
        }
        if (userAgent.indexOf("Safari") > -1) {
            return "Safari";
        } //    Safari   
        if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
            return "IE";
        }; //    IE   
    },
    //      (   、PC )
    terminalType:function(){
        var browser = window.navigator.userAgent; //     
        if (userAgent.indexOf("Android") > -1) {
        return "Android"
        }; //        
        if (userAgent.indexOf("iPhone") > -1) {
            return "iPhone";
        } //         
    },
    //      
    removeNode:function (elem){
        for(var i = 0,len = elem.length;i < len;i++){
            elem[i].parentNode.removeChild(elem[i]);
        }
    },
    //       
    emptyChildElem:function(elem){
        elem.innerHTML = "";
    },
    //     
    getChildNodes:function(elem){
        var childNodes = [];
        var node = elem.childNodes;
        for(var i = 0;i < node.length;i++){
            if(node[i].nodeType == 1){
                childNodes[childNodes.length] = node[i];
            }
        }
        return childNodes;
    },
    //        
    getSiblingElems:function (elem){
        var result = [], parentNode ,childNodes;
        parentNode = elem.parentNode;
        childNodes = parentNode.childNodes;
        for(var i = 0,len = childNodes.length;i < len;i++){
            if(childNodes[i].nodeType == 1 && childNodes[i] != elem){
                result[result.length] = childNodes[i];
            }
        }
        return result;
    },
    //         
    nextSiblingElem:function(elem){
        var siblings = zycTools.getChildNodes(elem.parentNode);
        var index = zycTools.getIndex(siblings,elem);
        var result = siblings[parseInt(index)+1];
        return result;
    },
    //               
    nextSiblingsAll:function(elem){
        var index,result = [];
        var childNodes = zycTools.getChildNodes(elem.parentNode);
        for(var i = 0,len = childNodes.length;i < len;i++){
            if(elem == childNodes[i]){
                index = i+1;
            }
        }
        for(var j = index;j < len;j++){
            result[result.length] = childNodes[j];
        }
        return result;
    },
    //         
    prevSiblingElem:function(elem){
        var siblings = zycTools.getChildNodes(elem.parentNode);
        var index = zycTools.getIndex(siblings,elem);
        var result = siblings[parseInt(index)-1];
        return result;
    },
    //               
    prevSiblingsAll:function(elem){
        var index,result = [];
        var childNodes = zycTools.getChildNodes(elem.parentNode);
        for(var i = 0,len = childNodes.length;i < len;i++){
            if(elem == childNodes[i]){
                index = i-1;
            }
        }
        for(var j = 0;j <= index;j++){
            result[result.length] = childNodes[j];
        }
        return result;
    },
    //          
    insertAfter:function(newEl, targetEl){ 
        var parentEl = targetEl.parentNode; 
        if(parentEl.lastChild == targetEl){ 
            parentEl.appendChild(newEl); 
        }else{ 
            parentEl.insertBefore(newEl,targetEl.nextSibling); 
        } 
    },
    //       Dom  
    addDom:function(elem, addelem){
        elem[elem.length] = addelem;
        return elem;
    },
    //      
    findElem:function (elemSelector, hasElemSelector){
        var result = document.querySelector(elemSelector).querySelectorAll(hasElemSelector);
        return result;
    },
    //              
    getIndex:function(elem, targetE){
        var index;
        for(var i = 0;i < elem.length;i++){
            if(elem[i] == targetE){
                index = i;
            }
        }
        return index;
    },
    //       ,        $$()           Dom  
    getText:function(elem){
        var text = "";
        for(var j = 0, lenj = elem.length;j < lenj;j++){
            for(var i = 0, leni = elem[j].childNodes.length;i < leni;i++){
                if(elem[j].childNodes[i].nodeType == 3){
                    var empText = elem[j].childNodes[i].nodeValue.replace(/[ ]/g,"").replace(/[\r
]/g
,""); text += empText + " "; } } } return text; }, // class addClass:function(elem,classname){ var oldClass = elem.className; if(elem.className){ if(elem.className.indexOf(classname) == -1){ elem.className = oldClass + " " + classname; } }else{ elem.className = classname; } }, // class removeClassName:function(elem,classname){ var oldClass = elem.className; if(elem.className && oldClass.indexOf(classname) != -1){ elem.className = elem.className.replace(classname, ""); }else{ return false; } }, // getStyle:function(elem,prop){ if(window.getComputedStyle){ return window.getComputedStyle(elem)[prop]; }else{ return elem.currentStyle[prop]; } }, // setCss:function(elem, cssObj){ for(var i in cssObj){ elem.style[i] = cssObj[i] } }, // toggleClass:function(elem,classname){ this.flag = false; var oldClass = elem.className; if(oldClass.indexOf(classname) != -1 && !flag){ flag = true; elem.className = elem.className.replace(" "+classname,""); }else{ flag = false; elem.className = oldClass + " " + classname; } }, // elemMove:function(elem, dir, time, callback){ var flag = true, value; dir == "top" ? value = "height" : value = "width"; var elemWith = parseInt(zycTools.getStyle(elem,value)); // var elemPosition = zycTools.getStyle(elem,"position"); // position if(elemPosition == "static"){ elem.style.position = "relative"; elem.style[dir] = "0px"; } setInterval(function(){ var left = parseInt(zycTools.getStyle(elem,dir)); if(left < elemWith && flag){ elem.style[dir] = (left + 1) + "px"; }else{ flag = false; elem.style[dir] = (left - 1) + "px"; if(parseInt(zycTools.getStyle(elem,dir)) < 0){ elem.style[dir] = "0px"; flag = true; } } },time); }, // silderUp:function(elem, time, callback){ //var i = 1; var timmerId = setInterval(function(){ var height = parseInt(zycTools.getStyle(elem,"height")); // if(height > 0){ elem.style.height = (height - 1) + "px"; }else{ elem.style.height = "0px"; clearTimeout(timmerId); if(typeof callback == "function"){ callback(); } } },time); }, // silderDown:function(elem, time, callback){ var initHeight = parseInt(zycTools.getStyle(elem,"height")); // elem.style.height = "0px"; elem.style.display = "block"; var timmerId = setInterval(function(){ var height = parseInt(zycTools.getStyle(elem,"height")); // if(height < initHeight){ elem.style.height = (height + 1) + "px"; }else{ clearTimeout(timmerId); if(typeof callback == "function"){ callback(); } } },time); }, // , transformScale:function(elem, time, multiple, callback){ var flag = true; elem.style.webkitTransform = "scale(1)"; var timeId = setInterval(function(){ var scaleStr = elem.style.webkitTransform.replace("scale(",""); var scale = parseFloat(scaleStr); if(scale < multiple && flag){ elem.style.webkitTransform = "scale(" + (scale + 0.01) + ")"; }else{ flag = false; elem.style.webkitTransform = "scale(" + (scale - 0.01) + ")"; if(scale < 1){ elem.style.webkitTransform = "scale(1)"; flag = true; clearTimeout(timeId); // callback if(typeof callback == "function"){ callback(); } } } },time); }, // Z rotate:function(elem, time, deg, callback){ var flag = true; elem.style.webkitTransform ? {}:elem.style.webkitTransform = "rotate(0deg)"; var timeId = setInterval(function(){ var rotateStr = elem.style.webkitTransform.replace("rotate(",""); //console.log(rotateStr); var rotate = parseInt(rotateStr); //console.log(rotate); if(rotate < deg && flag){ elem.style.webkitTransform = "rotate(" + (rotate + 1) + "deg)"; }else{ flag = false; elem.style.webkitTransform = "rotate(" + (rotate - 1) + "deg)"; if(rotate < 0){ elem.style.webkitTransform = "rotate(0deg)"; flag = true; // callback if(typeof callback == "function"){ callback(); } } } },time); }, // fadeOut:function(elem, time, callback){ var elemOpcity = zycTools.getStyle(elem,"opcity"); if(!elemOpcity){ elem.style.opacity = "1"; } setInterval(function(){ var intOpacityValue = parseFloat(elem.style.opacity); // if(intOpacityValue > 0){ elem.style.opacity = (intOpacityValue - 0.05) + ""; }else{ elem.style.opacity = "0"; elem.style.display = "none"; if(typeof callback == "funtion"){ callback(); } } },time); }, // fadeIn:function(elem, time, callback){ elem.style.dispaly = "block"; var elemOpcity = zycTools.getStyle(elem,"opcity"); if(!elemOpcity){ elem.style.opacity = "0"; } var timerId = setInterval(function(){ var intOpacityValue = parseFloat(elem.style.opacity); // if(intOpacityValue < 1){ elem.style.opacity = (intOpacityValue + 0.05) + ""; }else{ elem.style.opacity = "1"; clearTimeout(timerId); if(typeof callback == "funtion"){ callback(); } } },time); }, // validate:{ // chineseFirstName: function(str){ var firstName=' : '; var nameStr1=str.substring(0,1); // var nameStr2=str.substring(0,2); // var hasName=(firstName.indexOf(nameStr1)!=-1)||(firstName.indexOf(nameStr2)!=-1); // // if(hasName){ return true; }else{ return false; } }, number: function(number){ var pattern = /^[0-9]$/; if(pattern.test(number)){ return true; }else{ return false; } }, // chinese: function(str){ var pattern = /[\u4e00-\u9fa5]/; if(pattern.test(number)){ return true; }else{ return false; } }, // ( ) wordsNumber_: function(str){ var pattern = /[A-Za-z0-9_\-\u4e00-\u9fa5]+/; if(pattern.test(str)){ return true; }else{ return false; } }, // english: function(str){ var pattern = /^[a-zA-Z]$/; if(pattern.test(str)){ return true; }else{ return false; } }, // email: function(str){ var pattern = /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/; if(pattern.test(str)){ return true; }else{ return false; } }, // +86 cellphone: function(number){ var pattern = /^1[3|4|5|7|8]\d{9}$/; if(pattern.test(number)){ return true; }else{ return false; } }, // tellphone: function(number){ var pattern = /[0-9-()()]{7,18}/; if(pattern.test(number)){ return true; }else{ return false; } }, // website: function(str){ var pattern = /^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+/; if(pattern.test(str)){ return true; }else{ return false; } }, ipAddress: function(str){ var pattern = /(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)/; if(pattern.test(str)){ return true; }else{ return false; } }, // idCard: function(number){ var pattern = /\d{17}[\d|x]|\d{15}/; if(pattern.test(number)){ return true; }else{ return false; } }, // postCodes: function(number){ var pattern = /\d{6}/; if(pattern.test(number)){ return true; }else{ return false; } }, //QQ tencentQQ: function(number){ var pattern = /[1-9]([0-9]{5,11})/; if(pattern.test(number)){ return true; }else{ return false; } }, // specialWord: function(str){ var pattern = /`~!@#$%^&*()_+-=[]{}\|;:'".?\//; if(pattern.test(str)){ return true; }else{ return false; } }, // ( , , 8-16 ) strongPassword: function(str){ var pattern = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,16}$/; if(pattern.test(str)){ return true; }else{ return false; } }, // 2016-01-01 dataFormat: function(str){ var pattern = /^[1-9]{1}\d{3}-[0-1]{1}\d{0,1}-[0-1]{1}\d{0,1}/; if(pattern.test(str)){ return true; }else{ return false; } } } }