How to check if css box-shadow is supported (jQuery)?

前端 未结 2 482
小蘑菇
小蘑菇 2021-01-20 18:36

I\'m creating a layout in full css. However, some browser (such as IE6) do not support box-shadow (.. and -webkit-box-shadow or -moz-box-shadow). I would like to check if it

相关标签:
2条回答
  • 2021-01-20 18:58
    var check = document.createElement('div');
    
    var shadow = !!(0 + check.style['MozBoxShadow']);
    if(shadow)
       alert('moz-box-shadow available');
    

    That is the doing-it-yourself way. Other reliable way is the modernizr library, which does feature detection for you.

    http://www.modernizr.com/

    No jQuery needed at all here.

    0 讨论(0)
  • 2021-01-20 18:59

    A neat function (pure JavaScript, no jQuery) to check which CSS features are supported by the browser is described in Quick Tip: Detect CSS3 Support in Browsers with JavaScript.

    Function is as follows:

    var supports = (function() {
       var div = document.createElement('div'),
          vendors = 'Khtml Ms O Moz Webkit'.split(' '),
          len = vendors.length;
    
       return function(prop) {
          if (prop in div.style) {
              return true;
          }
    
          prop = prop.replace(/^[a-z]/, function(val) {
             return val.toUpperCase();
          });
    
          while (len--) {
             if (vendors[len] + prop in div.style) {
                // browser supports box-shadow. Do what you need.
                // Or use a bang (!) to test if the browser doesn't.
                return true;
             } 
          }
    
          return false;
       };
    })();
    

    Usage is like this:

    if (supports('boxShadow')) { 
       // Do whatever
    }
    

    It worked like a charm for me! :-)

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