Skip to content

Instantly share code, notes, and snippets.

@premasagar
Created March 8, 2011 17:08
Show Gist options
  • Save premasagar/860567 to your computer and use it in GitHub Desktop.
Save premasagar/860567 to your computer and use it in GitHub Desktop.
// Use the test to create a simple element positioning function
function positionElemSimple(elem, x, y){
elem.style.cssText = "position:absolute;top:" + y + "px;left:" + x + "px;";
return elem;
}
function positionElem3d(elem, x, y){
var translate3d = "transform:translate3d(" + x + "px," + y + "px,0);";
// TODO: simplify this
elem.style.cssText = "position:absolute;" + translate3d + "-webkit-" + translate3d + "-moz-" + translate3d + "-o-" + translate3d + "-ms-" + translate3d + "-khtml-" + translate3d;
return elem;
}
var positionElem = supportsCssTransform3d() ?
positionElemSimple : positionElem3d;
/////
// e.g.
positionElem(myElem, 50, 30);
/*
Test browser support for 3D CSS transforms.
Modified by Premasagar Rose <http://premasagar.com>
from Modernizr v1.6 <http://modernizr.github.com>
*//*global window */
var supportsCssTransform3d = (function(window){
var document = window.document,
docElement = document.documentElement,
docHead = document.head || document.getElementsByTagName('head')[0],
id = 'modernizr3d',
m_style = document.createElement(id).style,
prefixes = ' -webkit- -moz- -o- -ms- -khtml- '.split(' '),
undef;
function testMediaQuery(mq){
var st = document.createElement('style'),
div = document.createElement('div'),
ret;
st.textContent = mq + '{#' + id + '{height:3px}}';
docHead.appendChild(st);
div.id = id;
docElement.appendChild(div);
ret = div.offsetHeight === 3;
docHead.removeChild(st);
docElement.removeChild(div);
return ret;
}
function test_props(props, callback){
var i = props.length;
for (; i; i--){
if (m_style[ props[i-1] ] !== undef){
return true;
}
}
}
return function(){
var ret = !!test_props(['perspectiveProperty', 'WebkitPerspective', 'MozPerspective', 'OPerspective', 'msPerspective']);
if (ret && docElement.style.webkitPerspective !== undef){
ret = testMediaQuery('@media ('+prefixes.join('transform-3d),(')+id+')');
}
return ret;
};
}(window));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment