Go JavaScript fast detection of browser support for CSS3 features

Source: Internet
Author: User

transferred from:https://yuguo.us/weblog/detect-css-support-in-browsers-with-javascript/

--------------------------------------------------------------------------------------------------

In the project, you need to quickly detect if the browser supports a CSS3 feature, such as detecting whether "transform" is supported, and then my layout will have two completely different layouts.

Of course, in addition to the quick approach described in this article, there is a more famous and more general approach, that is, Modernizr, after running the script it will add a list of all the features supported by the browser on the HTML class.

Advantages:

    • JS is configurable and unwanted feature detection can be removed from the configuration script
    • Based on feature detection
    • JS Library Simple and useful

In addition, there is a less good way, that is to judge the browser UA, the bad reason is that the UA may be forged, and the version is cumbersome and unstable.

Pros: Performance may be optimal

Finally, this article describes this method, I wrote a function to quickly detect whether the browser supports a CSS feature, the appropriate scenario is to quickly need to know whether the browser supports a certain CSS features (rather than several).

Advantages:

    • Good performance
    • High versatility
    • Suitable for detecting individual CSS features
var supports = (function () {var div = document.createelement (' div '), vendors = ' khtml O Moz Webkit '. Split ("), Len = ven Dors.length; return function (prop) {if (prop in Div.style) return True, if ('-ms-' + prop in Div.style) return true; prop = Prop.rep Lace (/^[a-z]/, function (val) {return val.touppercase ();}); while (len--) {if (Vendors[len] + prop in Div.style) {return true;}} return false; }; }), if (Supports (' Textshadow ')) {document.documentElement.className + = ' Textshadow ';}

This is the final code, the principle is:

1. Create a div, and then you can get Div.style, which is the list of arrays of properties that it supports.

2. Check if text is contained in the array, and if so, return true directly.

3. Check the various prefixes, such as WebKit plus text, that is, webkittransition, if included in the style, returns True.

4. It is worth noting that in CSS the property is named:-webkit-transition, but in the DOM style, it is the corresponding webkittransition. I don't know why this is so.

Reference: http://net.tutsplus.com/tutorials/html-css-techniques/quick-tip-detect-css-support-in-browsers-with-javascript/

Go JavaScript fast detection of browser support for CSS3 features

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.