如何检测CSS过滤器?

前端之家收集整理的这篇文章主要介绍了如何检测CSS过滤器?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在某些浏览器中,包括Chrome稳定版,您可以这样做:
  1. h3 {
  2. -webkit-filter: grayscale(1);
  3. filter: grayscale(1);
  4. }

你不会知道,h1将完全渲染成灰度. Everything old is new again.

无论如何 – 有谁知道任何方式来检测特征?

我需要能够应用其他样式,如果fil过滤器不起作用.

解决方法

所谓UPDATED答案:

由于OP提到了一个好点,我正在更新答案,但是没有任何相关或与之前的答案相矛盾,这只是浏览器检测.

艾伦H.提到IE在10日之前.版本,支持过滤器css属性,但不是我们都知道的(CSS3过滤器我的意思).

所以如果我们想要检测CSS3过滤器,我们应该继续使用一点浏览器检测.正如我在my comments提到的.

使用documentMode属性,并将其与简单的特征检测结合起来,我们可以在IE中排除所谓的误报.

  1. function css3FilterFeatureDetect(enableWebkit) {
  2. //As I mentioned in my comments,the only render engine which truly supports
  3. //CSS3 filter is webkit. so here we fill webkit detection arg with its default
  4. if(enableWebkit === undefined) {
  5. enableWebkit = false;
  6. }
  7. //creating an element dynamically
  8. el = document.createElement('div');
  9. //adding filter-blur property to it
  10. el.style.cssText = (enableWebkit?'-webkit-':'') + 'filter: blur(2px)';
  11. //checking whether the style is computed or ignored
  12. //And this is not because I don't understand the !! operator
  13. //This is because !! is so obscure for learning purposes! :D
  14. test1 = (el.style.length != 0);
  15. //checking for false positives of IE
  16. //I prefer Modernizr's smart method of browser detection
  17. test2 = (
  18. document.documentMode === undefined //non-IE browsers,including ancient IEs
  19. || document.documentMode > 9 //IE compatibility moe
  20. );
  21. //combining test results
  22. return test1 && test2;
  23. }

Original Modernizr source

  1. if(document.body.style.webkitFilter !== undefined)

要么

  1. if(document.body.style.filter !== undefined)

额外的信息:

只需简单的功能检测就可以使用我上面的代码.有关支持功能的列表,请查看:

> Filter Effects 1.0
> Filter Effects 1.0 functions

要在Chrome中现场演示过滤器,请查看:

> CSS Filter Effects

还有2个资源给你:

> CSS Filter Effects Landing in WebKit
> When can I use CSS Filter Effects?

在我写这个答案的时候,你必须使用webkit供应商的前缀使它工作.

猜你在找的CSS相关文章