花了几个小时写了个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>

测试浏览器是否支持某个CSS属性的更多相关文章

  1. 判断浏览器是否支持某个css属性

    方法:直接判断浏览器是否支持某个CSS属性才是王道,document.documentElement.style 如:判断是否支持 transform if( 'MozTransform' in do ...

  2. 检测浏览器是否支持某个css属性

    以浏览器是否支持translate3d 为例说明,当然现代浏览器已经支持该属性.如果浏览器实现了带有前缀的某个属性,比如说支持-webkit-transform,但是不支持直接写transform,可 ...

  3. 判断浏览器是否支持指定CSS属性和指定值

    /** * @param attrName 属性名 * @param attrVal 属性值 * @returns {boolean} */ function isCssAttrSupported(a ...

  4. 十个实用但IE不支持的CSS属性

    对IE浏览器尤其是IE6的抱怨基本已进入麻痹状态,偶尔甚至产生非常消极的想法:这个世界只有一个浏览器就好了,哪怕这唯一的浏览器就是IE6.当然,这样的想法是非常病态的,马上打消.本文里面,介绍了10个 ...

  5. 判断浏览器是否支持某个css3属性的javascript方法

    判断浏览器是否支持css3某个属性的方法: /** * 判断浏览器是否支持某一个CSS3属性 * @param {String} 属性名称 * @return {Boolean} true/false ...

  6. CSS中浏览器开发商特定的CSS属性

    浏览器制造商(像Microsoft.Mozilla等,还有WebKit的后台人员等)通常会为他们的浏览器增加新的功能来测试新的特性, 或者实现一直在考虑但还没有得到标准组织批准的CSS扩展.在这些情况 ...

  7. animate支持的css属性

    支持下列CSS 样式 * backgroundPosition * borderWidth * borderBottomWidth * borderLeftWidth * borderRightWid ...

  8. Transition 所支持的css属性

    transition-property是用来指定当元素其中一个属性改变时执行transition效果: 所支持的属性类型如下: color: 通过红.绿.蓝和透明度组件变换(每个数值处理)如:back ...

  9. 测试浏览器是否支持JavaScript脚本

    如果用户不能确定浏览器是否支持JavaScript脚本,那么可以应用HTML提供的注释符号进行验证.HTML注释符号是以 <-- 开始以 --> 结束的.如果在此注释符号内编写 JavaS ...

随机推荐

  1. docker-compose编排

    创建并启动容器 docker-compose up -d 备注: -d 后台启动并运行容器 前提是你在执行该命令的时候已经编写好了docker-compose.yml文件,在这个文件的当前目录执行上述 ...

  2. python基础面试题

    函数1def foo(arg,li=[]): li.append(arg) return li list1 = foo(21) list2 = foo(11,[2]) list3 = foo(28) ...

  3. 4星|《流量池》:Luckin Coffee营销操盘手经验谈

    流量池:“急功近利”的流量布局.营销转化 作者是一线营销操盘手,全书是作者的经验总结,这样的作者在营销类图书中比较罕见,因此这本书非常有价值. 全书是写给巨头之外的企业营销人员看的,这样的企业的流量来 ...

  4. 3. Python3 基本数据类型

    Python3 基本数据类型 Python 中的变量不需要声明.每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建. 在 Python 中,变量就是变量,它没有类型,我们所说的"类型& ...

  5. 假设检验,alpha,p值 通俗易懂的的理解。

    假设检验: 一般原假设H0 :表是为 XXX和YYYY无显著差异,H1,是有显著差异. 如果我们定义alpha的值是0.05.意味着我们接受H0是真的但是我们却认为他是假的的概率. 这里你想想,这个值 ...

  6. Alpha答辩总结

    [Alpha展示评审表格] 小组序号 小组名称 格式(20%) 内容(20%) PPT(20%) 演讲(20%) 答辩(20%) 总分 1 天机组 15 15 15 15 16 76 2 PMS 16 ...

  7. 作业C#程序分析

    阅读下面程序,请回答如下问题: 问题1:这个程序要找的是符合什么条件的数? 问题2:这样的数存在么?符合这一条件的最小的数是什么? 问题3:在电脑上运行这一程序,你估计多长时间才能输出第一个结果?时间 ...

  8. express框架结合jade模板引擎使用

    在views文件夹里新建一个jade.jade文件作为模板: html head title 哈哈 body #box ul li 标题1 li 标题2 li 标题3 li 标题4 #aside 在j ...

  9. Perfmon - Windows 自带系统监测工具(转)

    本文转自:http://blog.csdn.net/oscar999/article/details/7918385 一. 简述 可以用于监视CPU使用率.内存使用率.硬盘读写速度.网络速度等. Pe ...

  10. ITSS相关的名词解释

    1.ITSM(IT Service Management)IT服务管理.从宏观的角度可以理解为一个领域或行业,人中观的角度可以理解为一种IT管理的方法论,从微观的角度可以理解为是一套协同动作的流程.从 ...