csstranslate3d

How to detect CSS translate3d without the webkit context?


To detect if a browser support translate3d we can use (WebKitCSSMatrix in window && m11 in new WebKitCSSMatrix())

but now that firefox support translate3d how to have a correct detection of it?

The idea would be to find a solution without using Modernizr.


Solution

  • I needed something similar. I wanted to test if a browser supported translate3d without using a library. I didn't find any good generic test that wasn't webkit specific. So after much experimentation, I came up with the following test. I first create a test element, assign a transform function to it, then see if the element retained the transform function.

    function Has3DSupport()
    {
        var sTranslate3D = "translate3d(0px, 0px, 0px)";
    
        var eTemp = document.createElement("div");
    
        eTemp.style.cssText = "  -moz-transform:"    + sTranslate3D +
                              "; -ms-transform:"     + sTranslate3D +
                              "; -o-transform:"      + sTranslate3D +
                              "; -webkit-transform:" + sTranslate3D +
                              "; transform:"         + sTranslate3D;
        var rxTranslate = /translate3d\(0px, 0px, 0px\)/g;
        var asSupport = eTemp.style.cssText.match(rxTranslate);
        var bHasSupport = (asSupport !== null && asSupport.length == 1);
    
        return bHasSupport;
    } // Has3DSupport
    

    The function is plenty fast for my needs: < 50 microseconds in desktop browsers, < 500 microseconds in mobile browsers.

    Hope this helps.