How to check if CSS shadow (jQuery) is supported?

I am creating a layout in full css. However, some browsers (e.g. IE6) do not support box-shadow (.. and -webkit-box-shadow or -moz-box-shadow). I would like to check if it supports and then add other styles.

How is this possible in jQuery?

Martti Lane

+3
source share
2 answers
var check = document.createElement('div');

var shadow = !!(0 + check.style['MozBoxShadow']);
if(shadow)
   alert('moz-box-shadow available');

This DIY . Another reliable way is a library modernizrthat performs a discovery function for you.

http://www.modernizr.com/

No jQuery is needed at all.

+9
source

( JavaScript, jQuery), , CSS , : CSS3 JavaScript.

:

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;
   };
})();

:

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

, !: -)

+3

Source: https://habr.com/ru/post/1760609/


All Articles