Detecting 'transform: translate3d' support

前端 未结 12 1621
遥遥无期
遥遥无期 2020-11-29 19:03

Does anyone know how I would detect transform: translate3d(x,y,z) support is available?

My issue is that I want to use translate3d across b

相关标签:
12条回答
  • 2020-11-29 19:44

    The author of Hardware Accelerated Accordion checks like this:

    var has3d = ('WebKitCSSMatrix' in window && 'm11' in new WebKitCSSMatrix())
    
    0 讨论(0)
  • 2020-11-29 19:46

    You can try CCS3 @supports:

    @supports (transform: translate3d) {
      div {
        transform : translate3d(20px,0,0);
      }
    }
    
    @supports not (transform: translate3d) {
      div {
        transform: translate(20px,0);
      }
    }
    

    Can I use @support

    0 讨论(0)
  • 2020-11-29 19:47

    Check out this solution.

    It is based on the fact that if a browser supports transforms, the value of

    window.getComputedStyle(el).getPropertyValue('transform')
    

    will be a string containing the transformation matrix, when a 3d transform is applied to the element el. Otherwise, it will be undefined or the string 'none', as in the case of Opera 12.02.

    It works on all major browsers.

    The code:

    function has3d() {
        if (!window.getComputedStyle) {
            return false;
        }
    
        var el = document.createElement('p'), 
            has3d,
            transforms = {
                'webkitTransform':'-webkit-transform',
                'OTransform':'-o-transform',
                'msTransform':'-ms-transform',
                'MozTransform':'-moz-transform',
                'transform':'transform'
            };
    
        // Add it to the body to get the computed style.
        document.body.insertBefore(el, null);
    
        for (var t in transforms) {
            if (el.style[t] !== undefined) {
                el.style[t] = "translate3d(1px,1px,1px)";
                has3d = window.getComputedStyle(el).getPropertyValue(transforms[t]);
            }
        }
    
        document.body.removeChild(el);
    
        return (has3d !== undefined && has3d.length > 0 && has3d !== "none");
    }
    
    0 讨论(0)
  • 2020-11-29 19:51

    I'd suggest using Modernizr.

    It does feature detection for a whole range of browser features, including 3D transforms. It also provides a method of specifying CSS rules for browsers which have various features or not, so it sounds like it will do exactly what you're looking for.

    Hope that helps.

    0 讨论(0)
  • 2020-11-29 19:54

    The original blog post announcing 3D transforms has an image flip demo, which does it with a media query, like this:

    @media all and (-webkit-transform-3d) {
      /* Use the media query to determine if 3D transforms are supported */
      #flip-container {
        -webkit-perspective: 1000;
      }
      #flip-card {
        width: 100%;
        height: 100%;
        -webkit-transform-style: preserve-3d;
        -webkit-transition: -webkit-transform 1s;
      }
      #flip-container:hover #flip-card {
        -webkit-transform: rotateY(180deg);
      }
    }
    

    This blog post has a good intro to media queries. This has some more details.

    0 讨论(0)
  • 2020-11-29 19:57

    tl:dr - Use user agent sniffing. Here is a script for detecting CSS 3D transform support across browsers: https://github.com/zamiang/detect-css3-3D-transform

    I tried most of the methods in this post, among others like Modernizer and Meny's methods but could not support browsers like Firefox while maintaining a good experience for older browsers like Safari 4&5, iOS devices and Chrome on Retina MacBook pros (they all have their quirks).

    CSS3 3D transforms involve interaction between the browser and the graphics card. The browser may be able to parse the 3D declarations but may not be able to properly instruct the graphics card in how to render your page. There are many possible outcomes ranging from the page rendering with lines across it (Safari 4) to the page rendering beautifully then crashing the browser seconds later (Safari on iOS4). Any ‘feature detection’ approach would unacceptably flag these as ‘supports CSS3 3D transforms’. This is one case where ‘feature detection’ fails and user agent sniffing (and lots of testing) wins hands down.

    Most feature detection assumes a 'supports' or 'does not support' binary. This is not the case with CSS3 3D Transforms - there is a 'gradient of support'.

    CSS3 3D transform support can be separated into 4 levels:

    1. Reliably supports 3D transforms across most machines. For example: Safari 6
    2. Can parse and apply 3D transform declarations but ignores the 3D parts. For example: Chrome on a Retina MacBook Pro.
    3. Can parse and apply 3D transform declarations but renders in unacceptable ways. For example: Safari 4 and Safari 4/5 on Windows show lines across the page.
    4. Cannot apply 3D transform declarations in any way. For example: IE or Firefox < v10

    This script will return true in scenario one and two but false for 3 and 4:

    Note: new to participating in stackoverflow - please let me know if I should paste that code inline (it is a bit long)

    0 讨论(0)
提交回复
热议问题