JS檢查判斷瀏覽器是否支援CSS3

來源:互聯網
上載者:User

常見的CSS3動畫就很有必要檢測瀏覽器是否支援。下面分享幾種方法:

第一種:javascript比較常用下面這個代碼:

var support_css3 = (function() {
   var div = document.createElement('div'),
      vendors = '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 ) {
            return true;
         }
      }
      return false;
   };
})();

使用:檢查是否支援transform

if(support_css3('transform')){

}else{

}

第二:JavaScript方法2:不支援ie6

function isPropertySupported(property)
{
 return property in document.body.style;
}

使用:
記得上面的屬性,要用backgroundColor替換background-color

if(isPropertySupported('opacity')){

}else{

}

第三:CSS.supports

CSS.suports是CSS3 @support 規則中比較特別的一個,每一個支援@support規則的都支援下面這個函數(不推薦這種方法,畢竟@support也有相容性,某些瀏覽器可能支援CSS3屬性中的一個,但卻不支援@support)

//pass the same string as you pass to the @supports rule
if(CSS.supports("(background-color: red) and (color:white"))
{
    document.body.style.color = "white";
    document.body.style.backgroundColor = "red";
}

最後再分享一個判斷瀏覽器是否支援某些HTML5屬性的函數,比如input屬性是否支援palaceholder.

function elementSupportsAttribute(element, attribute) {
  var test = document.createElement(element);
  if (attribute in test) {
    return true;
  } else {
    return false;
  }
};

用法:

if (elementSupportsAttribute("textarea", "placeholder") {

} else {
   // fallback
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.