花了几个小时写了个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. centos7 源码部署LNMP

    一.环境 系统环境:centos 7.4 64位 Nginx:1.7.9 MySQL: 5.7.20 (二进制包) PHP:5.6.37 二.Ngin 安装 Nginx部署 yum install   ...

  2. python + selenium webdriver 自动化测试 之 环境异常处理 (持续更新)

    1.webdriver版本与浏览器版本不匹配,在执行的时候会抛出如下错误提示 selenium.common.exceptions.WebDriverException: Message: unkno ...

  3. 萌新程序媛的首个作品,基于NoSQL的内容管理及低码开发平台

    尽管入行有一段时间了,但之前还从来没想过要开发一款完整的软件产品.这个我跟朋友开发的第一款软件,希望大家帮我们多宣传推广.首个版本肯定有很多的不足,大家也给我们多提意见,还有很多规划中的功能要在之后的 ...

  4. 用Unity简单实现第三人称人物的移动和转向

    上图不重要,因为实现人物的移动用的是动画,没有什么可说的,主要是下面实现人物的转向. 比如在一个平面中,玩家按了w和d键则人物会面向右前方向前进,如果此时玩家按了a和s键则人物会面向左后方向前进,那么 ...

  5. GitHubDesktop权限问题解决办法

    Desktop对于管理仓库非常方便.实用 很多人实用Desktop将仓库项目clone到本地 但是更新后同步时出现了如下权限错误: Error Authentication failed. You m ...

  6. mysql学习(2)-Navicat Premium 12 链接MySQL8.0.11数据库报2059错误

    Navicat Premium 12 链接MySQL8.0.11数据库报2059错误 1,问题现象 安装完MySQL8.0.11和Navicat Premium12后,我们会用Navicat去测试连接 ...

  7. linux内核分析第五周学习笔记

    linux内核分析第五周学习笔记 标签(空格分隔): 20135328陈都 陈都 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.co ...

  8. 作业三(下)安装VS2013

    VS2013 今天常识安装Microsoft Visual Studio 2013,虽然 直接在软件管家上下载,一键安装,但是还是遇到许多问题,安装过程相当的艰难,花了好多时间.但是在尝试多次后成功的 ...

  9. [转帖]DRAM芯片战争,跨越40年的生死搏杀

    DRAM芯片战争,跨越40年的生死搏杀 超级工程一览 ·2017-08-20 12:50·半导体行业观察 阅读:1.4万 来源:内容来自超级工程一览 , 谢谢. DRAM是动态随机存储器的意思,也就是 ...

  10. JIRA部署破解和confluence整合

    JIRA是一个项目跟踪管理工具,帮助团队创建计划任务.构建并发布优秀的产品.全球成千上万的团队选择JIRA,用JIRA来捕获.组织管理缺陷.分配任务,跟踪团队的活动.不论在桌面PC还是移动终端设备上, ...