Home > Web Front-end > JS Tutorial > How does JavaScript determine whether the browser supports CSS3 attributes?

How does JavaScript determine whether the browser supports CSS3 attributes?

不言
Release: 2018-06-25 16:17:56
Original
1507 people have browsed it

This article mainly introduces how JavaScript determines whether the browser supports CSS3 attributes. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

In fact, when using css3 For some attributes, in order to take into account the unfriendliness of low-end browsers to CSS3, it is often necessary to know whether certain browsers support the CSS3 attributes to be used, so as to make downward adaptation. For example, for common CSS3 animations, it is necessary to check whether the browser supports it. This article shares several methods below, and friends in need can refer to them.

Preface

The emergence of CSS3 has made the performance of browsers more colorful. The one with the greatest impact is animation. In daily writing animation When using , it is necessary to determine in advance whether the browser supports it, especially when writing a CSS3 animation library. For example, animation-play-state of transition is only supported by some browsers.

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

The first method: javascript is more commonly used with the following code:

var support_css3 = (function() {
 var p = document.createElement('p'),
  vendors = 'Ms O Moz Webkit'.split(' '),
  len = vendors.length;
 
 return function(prop) {
  if ( prop in p.style ) return true;
 
  prop = prop.replace(/^[a-z]/, function(val) {
   return val.toUpperCase();
  });
 
  while(len--) {
   if ( vendors[len] + prop in p.style ) {
   return true;
   } 
  }
  return false;
 };
})();
Copy after login

Use: Check if supportedtransform

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

if(isPropertySupported('opacity')){

}else{

}
Copy after login
# with

backgroundColor

##Third: CSS.supports

CSS.supports

is a special one among the CSS3 @support rules One, everyone 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. For example, does the input attribute support

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

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About JavaScript for making simple frame charts


About js for passing array parameters to the background controller method


The above is the detailed content of How does JavaScript determine whether the browser supports CSS3 attributes?. For more information, please follow other related articles on the PHP Chinese website!

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template