Skip to content

Instantly share code, notes, and snippets.

@jgonera
Forked from lorenzopolidori/has3d.js
Last active December 15, 2015 11:09
Show Gist options
  • Save jgonera/5250507 to your computer and use it in GitHub Desktop.
Save jgonera/5250507 to your computer and use it in GitHub Desktop.
This version sandboxes el inside an iframe to avoid weird Android Browser quirks. Requires jQuery.
function has3d(){
var el = $('<p>')[0], $iframe = $('<iframe>'), has3d, t,
transforms = {
'webkitTransform': '-webkit-transform',
'OTransform': '-o-transform',
'msTransform': '-ms-transform',
'transform': 'transform'
};
// Add it to the body to get the computed style
// Sandbox it inside an iframe to avoid Android Browser quirks
$iframe.appendTo('body').contents().find('body').append( el );
for (t in transforms) {
if (el.style[t] !== undefined) {
el.style[t] = 'translate3d(1px,1px,1px)';
has3d = window.getComputedStyle(el).getPropertyValue(transforms[t]);
}
}
$iframe.remove();
return has3d !== undefined && has3d.length > 0 && has3d !== "none";
}
@PAEz
Copy link

PAEz commented Nov 29, 2015

Screw jQ....

function has3d(){
    var el = document.createElement("P"), iframe = document.createElement("IFRAME"), has3d, t,
        transforms = {
            'webkitTransform': '-webkit-transform',
            'OTransform': '-o-transform',
            'msTransform': '-ms-transform',
            'transform': 'transform'
        };

    // Add it to the body to get the computed style
    // Sandbox it inside an iframe to avoid Android Browser quirks+
    document.body.appendChild(iframe);
    iframe.contentDocument.body.appendChild(el);

    for (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(iframe);

    return has3d !== undefined && has3d.length > 0 && has3d !== "none";
}


has3d();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment