api.js

作者: 陈建芳 | 来源:发表于2016-12-09 17:31 被阅读0次

    作为工具仓库,持续更新ing

    判断是否是数组:
    function isArrayFn(arr){
        if(typeof Array.isArray === 'function'){
            return Array.isArray(arr);
        }else{
            return Object.prototype.toString.call(arr) === '[object Array]';
        }
    }
    
    跨浏览器事件处理:
    /*
    *
    * 元素 事件 操作
    * element 元素; type 事件类型; handler 处理函数   
    *
    */
    var EventUtil = {
        addHandler:function(element,type,handler){
            if(element.addEventListener){
                element.addEventListener(type,handler,false);
            }else if(element.attachEvent){
                element.attachEvent("on"+type,handler);
            }else{
                element['on'+type] = handler;
            }
        },
        removeHandler:function(element,type,handler){
            if(element.removeEventListener){
                element.removeEventListener(type,handler,false);
            }else if(element.detachEvent){
                element.detachEvent('on'+type,handler);
            }else{
                element['on'+type] = null;
            }
        }
    }
    /*
    * 获取事件
    */
    getEvent:function(event){
        return event ? event : window.event;
    },
    /*
    * 获取触发事件的目标元素
    */
    getTarget:function(event){
        return event.target || event.srcElement;
    }
    /*
    * 取消默认事件
    */
    preventDefault:function(event){
        if(event.preventDefault){
            event.preventDefault();
        }else{
            event.returnValue = false;
        }
    }
    /*
    * 阻止事件冒泡
    */
    stopPropagation:function(event){
        if(event.stopPropagation){
            event.stopPropagation();
        }else{
            event.cancelBubble = true;
        }
    }
    
    获取鼠标在页面中坐标
    EventUti.addHandler(element,"click",function(event){
        event = EventUtil.getEvent(event);
        var pageX = event.pageX;
        var pageY = event.pageY;
        if(pageX === undefined){
            pageX = event.clientX + (document.body.scrollLeft || document.documentElement.scrollLeft);
        }
        if(pageY === undefined){
            pageY = event.clientY + (document.body.scrollTop || document.documentElement.scrollLeft);
        }
        return {
            pageX,
            pageY
        }
    })
    
    元素距离文档顶部距离
    function getTop(el) { let top = el.offsetTop; let currentParent = el.offsetParent; while (currentParent != null) { top += currentParent.offsetTop; currentParent = currentParent.offsetParent; } return top; }
    
    输入框正整数
                var znInput = false;
                $(".layout-content").on("compositionstart",".input-num",function(){
                    znInput = true;
                });
                $(".layout-content").on("compositionend",".input-num",function(){
                    znInput = false;
                    giftNum = $(this).val().replace(/[^0-9]/g,"");
                    $(this).val(giftNum);
                    $(".user-money-num").html(giftNum*unitPrice);
                });
                $(".layout-content").on("input",".input-num",function(){
                    if(znInput) return;
                    giftNum = $(this).val().replace(/[^0-9]/g,"");
                    $(this).val(giftNum);
                    $(".user-money-num").html(giftNum*unitPrice);
                });
    
    获取客户端平台
    var os = function() {  
         var ua = navigator.userAgent,  
         isWindowsPhone = /(?:Windows Phone)/.test(ua),  
         isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone,   
         isAndroid = /(?:Android)/.test(ua),   
         isFireFox = /(?:Firefox)/.test(ua),   
         isChrome = /(?:Chrome|CriOS)/.test(ua),  
         isTablet = /(?:iPad|PlayBook)/.test(ua) || (isAndroid && !/(?:Mobile)/.test(ua)) || (isFireFox && /(?:Tablet)/.test(ua)),  
         isPhone = /(?:iPhone)/.test(ua) && !isTablet,  
         isPc = !isPhone && !isAndroid && !isSymbian;  
         return {  
              isTablet: isTablet,  
              isPhone: isPhone,  
              isAndroid : isAndroid,  
              isPc : isPc  
         };  
    }();  
    
    
    inherit继承
    /**
     * [inherit 继承属性]
     * @param  {[type]} p [description]
     * @return {[type]}   [description]
     */
    function inherit(p){
        if(p == null) throw TypeError();
        if(Object.create) return Object.create(p);
        var t = typeof p;
        if(t !== 'object' && t !== 'function') throw TypeError();
        function f(){};
        f.prototype = p;
        return new f();
    }
    @example:
     MyFoo.prototype = inherit(Root.prototype);
    
    移动端1像素边框
    .border{
        //1px
        &::after{
            content: "";
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            border-radius: 50%;
            transform-origin: 0 0;
            -webkit-transform-origin: 0 0;
            border: 1px solid #aaa;
        }
    }
    @media only screen and (-webkit-min-device-pixel-ratio: 2){
        .border::after{
            width: 200%;
            height: 200%;
            transform: scale(0.5);
            -webkit-transform: scale(0.5);
        }
    }
    @media only screen and (-webkit-min-device-pixel-ratio: 3){
        .border::after{
            width: 300%;
            height: 300%;
            transform: scale(0.33);
            -webkit-transform: scale(0.33);
        }
    }
    
    计算时间
    function culTime (mil) {
        if(mil<=0){
            activityEnd = true;
            clearInterval(timeST);
            return $('.recharge-loadtime').html('00天00时00分00秒');
        }
    
        var d = ~~(mil/(1000*60*60*24));
        var h = ~~((mil-d*24*60*60*1000)/(1000*60*60));
        var i = ~~((mil-d*24*60*60*1000-h*60*60*1000)/(1000*60));
        var s = ~~((mil%(60*1000))/1000);
        d = d < 10? '0'+d : d;
        h = h < 10? '0'+h : h;
        i = i < 10? '0'+i : i;
        s = s < 10? '0'+s : s;
        $('.recharge-loadtime').html(d+'天'+h+'时'+i+'分'+s+'秒');
    }
    
    判断是否支持CSS3
    function supportCss3(style) {
        var prefix = ['webkit', 'Moz', 'ms', 'o'],
            i,
            humpString = [],
            htmlStyle = document.documentElement.style,
            _toHumb = function (string) {
                return string.replace(/-(\w)/g, function ($0, $1) {
                    return $1.toUpperCase();
                });
            };
    
        for (i in prefix)
            humpString.push(_toHumb(prefix[i] + '-' + style));
    
        humpString.push(_toHumb(style));
    
        for (i in humpString)
            if (humpString[i] in htmlStyle) return true;
    
        return false;
    }
    

    相关文章

      网友评论

          本文标题:api.js

          本文链接:https://www.haomeiwen.com/subject/wvxymttx.html