Wesley 是对的,但要记住的只是一个简短的说明,它if (Modernizr.mq("screen and (max-width:480px)")) {
仍然只有在满足媒体查询条件时才会触发!
因此,如果您的屏幕大于 480 像素并且调用了此脚本,它不会发出警报。
就在今天创建了这个以在触发媒体查询时触发脚本(使用 IE 回退):
//Test to see if media queries are acceptable
if(Modernizr.mq('only all')){
var mql = window.matchMedia('(max-width: 980px)');
mql.addListener(tabletChange);
tabletChange(mql);
function tabletChange(mediaQueryList) {
//If media query triggered
if (mediaQueryList.matches) {
//SMALL SCREEN
} else {
//LARGE SCREEN
}
}
} else {
var resizeTimer;
mediaMatchFallback();
$(window).resize(function(){
if(resizeTimer){
clearTimeout(resizeTimer);
}
resizeTimer = setTimeout(mediaMatchFallback, 500);
});
function mediaMatchFallback(){
if($(window).width() <= 980){
//SMALL SCREEN
}else{
//LARGE SCREEN
}
}
}