Check browser CSS property support with JavaScript?

前端 未结 4 398
暖寄归人
暖寄归人 2020-11-27 15:02

Is it possible in JavaScript to know if a CSS property is supported by the client browser? I\'m talking about the rotation properties of CSS3. I want to execute some functio

相关标签:
4条回答
  • 2020-11-27 15:28

    May be try this?

    var temp = document.createElement('div');
    var isSupport = temp.style['propName'] != undefined;
    
    0 讨论(0)
  • 2020-11-27 15:29

    You can use Modernizr library.

    Example for css transform:

    in .css file;

    .no-csstransforms .box { color: red; }
    .csstransforms .box { color: green; }
    

    in .js file;

    if (Modernizr.csstransforms) {
      // supported
    } else {
      // not-supported
    }
    
    0 讨论(0)
  • 2020-11-27 15:50

    I believe you can do it this way:

    if ('WebkitTransform' in document.body.style 
     || 'MozTransform' in document.body.style 
     || 'OTransform' in document.body.style 
     || 'transform' in document.body.style) 
    {
        alert('I can Rotate!');
    }
    
    0 讨论(0)
  • 2020-11-27 15:51

    There is a new DOM API CSS.supports for that purpose. FF, Opera (as supportsCSS) and Chrome Canary already implement this method.

    For cross-browser compatibility you can use my CSS.supports polyfill

    Example:

    CSS.supports("display", "table");//IE<8 return false
    

    But, you still need to specify browser prefix for prefixing css properties. For example:

    CSS.supports("-webkit-filter", "blur(10px)");
    

    I suggest to using Modernizr for feature-detection.

    0 讨论(0)
提交回复
热议问题