Home > php教程 > PHP开发 > body text

Various methods for JavaScript to determine whether the browser supports CSS3 attributes

高洛峰
Release: 2016-12-07 10:00:55
Original
1150 people have browsed it

Foreword

The emergence of CSS3 has made browser performance more colorful. The biggest impact on performance is animation. When writing animations on a daily basis, it is necessary to determine whether the browser supports it in advance, especially when writing CSS3 animation libraries. when. For example, transition animation-play-state is only supported by some browsers.

The following method can use a script to determine whether the browser supports a certain CSS3 attribute:

First type: javascript is more commonly used the following code:

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

Usage: Check whether transform is supported

if(support_css3('transform')){
 
}else{
 
}
Copy after login

Second: JavaScript method 2: ie6 is not supported

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

Use:

Remember the above attributes and replace background-color with backgroundColor

if(isPropertySupported('opacity')){
 
}else{
 
}
Copy after login

Third: CSS.supports

CSS.supports is a special one among CSS3 @support rules. Every one that supports @support rules supports the following function (this method is not recommended. After all, @support also has compatibility. Some browsers may support CSS3 attributes. One, but it does not support @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";
}
Copy after login

Finally, I will share a function to determine whether the browser supports certain HTML5 attributes, such as whether the input attribute supports palaceholder.

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

Usage:

if (elementSupportsAttribute("textarea", "placeholder") {
 
} else {
 // fallback
}
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!