Detect with JavaScript or jQuery if CSS transform 2D is available

前端 未结 5 1169
攒了一身酷
攒了一身酷 2021-02-04 09:01

I\'m displaying a element on my site which I rotate with -90deg but if a browser doesn\'t support the CSS transform the element looks misspositioned and not really good. Now I w

5条回答
  •  遇见更好的自我
    2021-02-04 09:17

    This code tests for 2D transforms support.

    It can be easily adjusted to detect 3D transforms support instead. Just add 'translateZ(1)' to test CSS (see defaultTestValues in source code).

    The plus of this code is that it detects the vendor prefix supported (if any). Call it:

    testCSSSupport('transform')
    

    Possible return values:

    false, when feature unsupported, or

    {
        vendor: 'moz',
        cssStyle: '-moz-transform',
        jsStyle: 'MozTransform'
    }
    

    when feature supported

    /**
     * Test for CSS3 feature support. Single-word properties only by now.
     * This function is not generic, but it works well for transition and transform at least
     */
    testCSSSupport: function (feature, cssTestValue/* optional */) {
        var testDiv,
            featureCapital = feature.charAt(0).toUpperCase() + feature.substr(1),
            vendors = ['', 'webkit', 'moz', 'ms'],
            jsPrefixes = ['', 'Webkit', 'Moz', 'ms'],
            defaultTestValues = {
                transform: 'translateX(.5em)'
               // This will test for 2D transform support
               // Add translateZ(1) to test 3D transform
            },
            testFunctions = {
                transform: function (jsProperty, computed) {
                    return computed[jsProperty].substr(0, 9) === 'matrix3d(';
                }
            };
    
        function isStyleSupported(feature, jsPrefixedProperty) {
            if (jsPrefixedProperty in testDiv.style) {
                var testVal = cssTestValue || defaultTestValues[feature],
                    testFn = testFunctions[feature];
                if (!testVal) {
                    return false;
                }
    
                //Assume browser without getComputedStyle is either IE8 or something even more poor
                if (!window.getComputedStyle) {
                    return false;
                }
    
                testDiv.style[jsPrefixedProperty] = testVal;
                var computed = window.getComputedStyle(testDiv);
    
                if (testFn) {
                    return testFn(jsPrefixedProperty, computed);
                }
                else {
                    return computed[jsPrefixedProperty] === testVal;
                }
            }
        }
    
        var cssPrefixedProperty,
            jsPrefixedProperty,
            testDiv = document.createElement('div');
    
        for (var i = 0; i < vendors.length; i++) {
            if (i === 0) {
                cssPrefixedProperty = feature;  //todo: this code now works for single-word features only!
                jsPrefixedProperty = feature;   //therefore box-sizing -> boxSizing won't work here
            }
            else {
                cssPrefixedProperty = '-' + vendors[i] + '-' + feature;
                jsPrefixedProperty = jsPrefixes[i] + featureCapital;
            }
    
            if (isStyleSupported(feature, jsPrefixedProperty)) {
                return {
                    vendor: vendors[i],
                    cssStyle: cssPrefixedProperty,
                    jsStyle: jsPrefixedProperty
                };
            }
        }
    
        return false;
    }
    

提交回复
热议问题