Refactored $.media and moved to jQuery.mobile.support.js

This commit is contained in:
Scott González 2010-09-17 14:19:53 -04:00
parent a056d4636b
commit 0479ff5239
2 changed files with 20 additions and 34 deletions

View file

@ -27,40 +27,6 @@
backBtnText = "Back",
urlStack = [location.hash.replace(/^#/,'')];
/*
add some core behavior,events
*/
//test whether a CSS media type or query applies (adapted from work by Scott Jehl & Paul Irish: http://gist.github.com/557891)
$.media = (function(){
/*
note: once support improves, try window.matchMedia here
if ( window.matchMedia ){
//use native support if available
return window.matchMedia;
}
*/
var cache = {},
testDiv = $('<div id="jq-mediatest"></div>'),
fakeBody = $('<body></body>').append(testDiv);
return function(q){
if (cache[q] === undefined) {
var styleBlock = $('<style type="text/css"></style>');
var cssrule = '@media '+q+' { #jq-mediatest { position: absolute; } }';
if (styleBlock[0].styleSheet){
styleBlock[0].styleSheet.cssText = cssrule;
}
else {
styleBlock.text(cssrule);
}
$html.prepend(fakeBody).prepend(styleBlock);
cache[q] = ((window.getComputedStyle ? window.getComputedStyle(testDiv[0],null) : testDiv[0].currentStyle)['position'] == 'absolute');
fakeBody.add(styleBlock).remove();
}
return cache[q];
};
})();
//hide Address bar
function hideBrowserChrome(){
$.event.special.scrollstart.enabled = false;

View file

@ -42,3 +42,23 @@ $.extend( $.support, {
fakeBody.remove();
})();
// test whether a CSS media type or query applies
$.media = (function() {
// TODO: use window.matchMedia once at least one UA implements it
var cache = {},
testDiv = $( "<div id='jquery-mediatest'>" ),
fakeBody = $( "<body>" ).append( testDiv );
return function( query ) {
if ( !( query in cache ) ) {
var styleBlock = $( "<style type='text/css'>" +
"@media " + query + "{#jquery-mediatest{position:absolute;}}" +
"</style>" );
$html.prepend( fakeBody ).prepend( styleBlock );
cache[ query ] = testDiv.css( "position" ) === "absolute";
fakeBody.add( styleBlock ).remove();
}
return cache[ query ];
};
})();