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
The author of Hardware Accelerated Accordion checks like this:
var has3d = ('WebKitCSSMatrix' in window && 'm11' in new WebKitCSSMatrix())
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
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");
}
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.
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.
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:
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)