测试浏览器是否支持某个CSS属性

花了几个小时写了个API,为了兼容多种用法和测试花了不少时间,求鞭打、嘲笑和建议。

<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
</head>
<body>

</body>
<script>
    //Cound use four types of propertyName,like:
    //'animation','-webkit-animation','webkit-animation','webkitAnimation'
    function isSupportCSS(propertyName) {
        var div = document.createElement('div'),
            getComputedStyle = window.getComputedStyle || (document.defaultView && document.defaultView.getComputedStyle),
            result,
            body = document.body || document.getElementsByTagName('body')[0],
            currentStyle,
            //to replace propertyName from dash style to camel case
            rcamelCase = /-([\da-z])/gi,
            //to see the function expression toCamelCase。we need the first character of propertyName is Uppercase,like 'MozAnimation',when the browser is FF.
            //so we need to keep the first dash when the browser is FF.
            rprefix = /^-(?!moz)/i,
            toCamelCase = function (string) {
                return string.replace(rprefix,'').replace(rcamelCase,function (all,letter) { return letter.toUpperCase();});
            },
            prefixArray = ['webkit-','moz-','ms-'],
            i = prefixArray.length,
            rhasPrefix = /^-?(webkit|moz|ms)/i,
            //Could you show me a better way to test whether some property need to add the prefix?
            sCSS3 = 'animation|appearance|backface|background|border|box|clip|column|filter|font|highlight|hyphenate|'+
                    'line|locale|locale|margin|mask|perspective|print|rtl|text|transform|transition|user|writing|app|flex|'+
                    'grid|hyphens|content|adjust|flow|wrap|scroll|user|touch|binding|font|image|outline|orient|stack|tab|window|marquee|svg',
            rCSS3 = new RegExp(sCSS3,'i');
        //now we just support string
        if(typeof propertyName !== 'string') {
            return false;
        }
        //to handle the situation when propertyName like 'moz-animation'
        if (propertyName.indexOf('moz') === 0) {
            propertyName = '-'+propertyName;
        }

        propertyName = toCamelCase(propertyName);

        if (getComputedStyle) {
            result = getComputedStyle(div)[propertyName === 'float'? 'cssFloat' :propertyName];
            if (result || result === '') return true;
            //if need add prefix
            if (rCSS3.test(propertyName)) {
                while (i > 0) {
                    result = getComputedStyle(div)[rhasPrefix.test(propertyName)? propertyName : toCamelCase(prefixArray[i-1]+propertyName)];
                    if (result || result === '') return true;
                    i--;
                }
            }
        //old IE
        } else if (body.currentStyle || body.runtimeStyle) {

            div.style['display'] = 'none';
            //only when the element have appended to the DOM tree it can have the currentStyle property
            body.appendChild(div);
            currentStyle = div.currentStyle || div.runtimeStyle;
            result = currentStyle[propertyName === 'float'? 'styleFloat' :propertyName];

            if (result || result === '') {
                body.removeChild(div);
                return true;
            }
            if (rCSS3.test(propertyName)) {
                result = currentStyle[rhasPrefix.test(propertyName)? propertyName : toCamelCase('ms-'+propertyName)];
                if (result || result === '') {
                    body.removeChild(div);
                    return true;
                }
            }
            body.removeChild(div);
        }
        return false;
    }
    alert('animation:'+isSupportCSS('animation'));
    alert('webkit-animation:'+isSupportCSS('webkit-animation'));
    alert('-webkit-animation:'+isSupportCSS('-webkit-animation'));
    alert('webkitAnimation:'+isSupportCSS('webkitAnimation'));
    alert('moz-animation:'+isSupportCSS('moz-animation'));
    alert('-moz-animation:'+isSupportCSS('-moz-animation'));
    alert('mozAnimation:'+isSupportCSS('mozAnimation'));
    alert('ms-animation:'+isSupportCSS('ms-animation'));
    alert('-ms-animation:'+isSupportCSS('-ms-animation'));
    alert('msAnimation:'+isSupportCSS('msAnimation'));
</script>
</html>

转载于:https://www.cnblogs.com/suprise/p/3607967.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值