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
Bootstrap uses following Code:
@media all and (transform-3d), (-webkit-transform-3d) {
.carousel-inner > .carousel-item {
transition: transform 0.6s ease-in-out;
backface-visibility: hidden;
perspective: 1000px;
}
.carousel-inner > .carousel-item.next,
.carousel-inner > .carousel-item.active.right {
left: 0;
transform: translate3d(100%, 0, 0);
}
.carousel-inner > .carousel-item.prev,
.carousel-inner > .carousel-item.active.left {
left: 0;
transform: translate3d(-100%, 0, 0);
}
.carousel-inner > .carousel-item.next.left,
.carousel-inner > .carousel-item.prev.right,
.carousel-inner > .carousel-item.active {
left: 0;
transform: translate3d(0, 0, 0);
}
}
//The following is based on iScroll4's tests to determine if a browser supports CSS3 3D transforms.
var has3d = function() {
return ('WebKitCSSMatrix' in window && 'm11' in new WebKitCSSMatrix());
}
just simply use:
alert($.support.cssTransform3d);
this alerts true if supported and false if not
Was tinkering around with a way to check for 3d support.. used this implementation from Jeffery Way in this article. Allows for less code and more use cases ;)
/**
* Test For CSS3 property support
* use 'perspective' to test for 3d support
*/
var supports = (function() {
'use strict';
var div = document.createElement('div'),
vendors = 'Khtml ms O Moz Webkit'.split(' '),
len = vendors.length;
return function(prop) {
if (prop in div.style) return true;
prop = prop.replace(/^[a-z]/, function(val) {
return val.toUpperCase();
});
while(len--) {
if (vendors[len] + prop in div.style) {
return true;
}
}
return false;
};
})();
if(supports('perspective')) {
// do 3d stuff
}
This code is adjusted for testing 3D transforms support and other CSS3 features.
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: 'translateZ(0.5em) rotateY(10deg) scale(2)'
// This will test for 3D transform support
// Use translateX to test 2D 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;
}
}
}
//Create a div for tests and remove it afterwards
if (!testDiv) {
testDiv = document.createElement('div');
document.body.appendChild(testDiv);
setTimeout(function () {
document.body.removeChild(testDiv);
testDiv = null;
}, 0);
}
var cssPrefixedProperty,
jsPrefixedProperty;
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;
}
Using jQuery:
var cssTranslate3dSupported = false;
(function()
{
var div = $('<div style="position:absolute;">Translate3d Test</div>');
$('body').append(div);
div.css(
{
'transform' : "translate3d(20px,0,0)",
'-moz-transform' : "translate3d(20px,0,0)",
'-webkit-transform' : "translate3d(20px,0,0)",
'-o-transform' : "translate3d(20px,0,0)",
'-ms-transform' : "translate3d(20px,0,0)"
});
cssTranslate3dSupported = (div.offset().left == 20);
div.empty().remove();
})();