Modernizr帮助咱们检测浏览器是否实现了某个特征,若是实现了那么开发人员就能够充分利用这个特征作一些工做javascript
Modernizr是自动运行的,无须调用诸如modernizr_init()之类的初始化方法css
Modernizr的官网地址http://modernizr.com/html
在官网首页你就能够下载modernizr.js(它分两个版本Development和Production版本。其实它们都会导向同一个下载页面,只不过前者会帮咱们把选项预先勾上而已。)html5
当我把下载的modernizr.js引用到页面中时(这时我什么也没作),看以下的图片就知道modernizr.js是自执行的java
使用了Modernizr后,页面中渲染后的html代码是这个样子的:jquery
其中有不少以no做为前缀的class,固然大部分都没有这个前缀。事实上,若是一个类名以no做为前缀,好比 no-touch
这表示浏览器不支持touch特性css3
modernizr还有一个test页面,你们能够在本身的浏览器里面打开这个test页面看看浏览器对于html5和css3的支持状况(据我所知chrome应该是对html5和css3支持最好的一个浏览器)git
test页面的地址http://modernizr.github.io/Modernizr/test/index.htmlgithub
/* *@desc:检测浏览器是否支持canvas API */ function supports_canvas() { return !!document.createElement('canvas').getContext; }
if (Modernizr.canvas) { //support } else { //not support }
//浏览器支持canvas API并不意味着支持canvas文本API(缘由是绘制文本的函数是后来才被归入规范中) /* *@desc:检测浏览器是否支持canvas 文本API */ function supports_canvas_text() { if (!supports_canvas()) { return false; } var dummy_canvas = document.createElement('canvas'); var context = dummy_canvas.getContext('2d'); return typeof context.fillText == 'function'; }
if (Modernizr.canvastext) { //support } else { //not support }
/* *@desc:检测浏览器是否HTML5的video */ function supports_video() { return !!document.createElement('video').canPlayType; }
if (Modernizr.video) { //support } else { //not support }
//检测视频格式 /* *@desc:检测Mac 和iPhone支持的受专利保护的格式 */ function supports_h264_baseline_video() { if (!supports_video()) { return false; } var v = document.createElement('video'); return v.canPlayType('video/mp4;codecs="avc1.42E01E,mp4a.40.2"'); } /* *@desc:检测被Mozilla Firefox和其它一些开源浏览器 支持的开放视频格式 */ function supports_ogg_theora_video() { if (!supports_video()) { return false; } var v = document.createElement('video'); return v.canPlayType('video/ogg;codecs="theora,vorbis"'); } /* *@desc:WebM(一种开源的视频编码格式) 检测被Chrome FireFox Opera支持的开放视频格式 */ function supports_webm_video() { if (!supports_video()) { return false; } var v = document.createElement('video'); return v.canPlayType('video/webm;codecs="vp8,vorbis"'); }
if (Modernizr.video) { //能够播放视频格式 但播放哪一种格式的呢 if (Modernizr.video.ogg) { } else { if (Modernizr.video.h264) { } else { if (Modernizr.video.webm) { } } } } else { //not support }
/* *@desc:检测浏览器是否支持本地存储 */ function supports_local_storage() { return ('localStorage' in window) && window['localStorage'] != null; }
if (Modernizr.localstorage) { //support } else { //not support }
/* *@desc:检测浏览器是否支持web worker */ function supports_local_storage() { return !!window.Worker; }
if (Modernizr.webworkers) { //support } else { //not support }
/* *@desc:检测浏览器是否支持离线web应用 */ function supports_offline() { return !!window.applicationCache; }
if (Modernizr.applicationcache) { //support } else { //not support }
/* *@desc:检测浏览器是否支持地理定位 */ function supports_geolocation() { return !!navigator.geolocation; }
if (Modernizr.geolocation) { //support } else { //not support }
/* *@desc:检测浏览器是否占位文本 */ function supports_input_placeholder() { var i = document.createElement('input'); return 'placeholder' in i; }
if (Modernizr.input.placeholder) { //support } else { //not support }
/* *@desc:检测浏览器是否支持自动聚焦 */ function supports_input_autofocus() { var i = document.createElement('input'); return 'autofocus' in i; }
if (Modernizr.input.autofocus) { //support } else { //not support }
还有不少,这里就不一一介绍了,总之,经过上面的特征检测能够知道,用modernizr.js更加方便,使用原生的方法相对来讲会要复杂一些。web
Modernizr帮助咱们检测浏览器是否实现了某个feature,若是实现了那么开发人员就能够充分利用这个feature作一些工做,反之没有实现开发人员也好提供一个fallback。
举例来讲,当咱们引入了Modernizr.js类库后, <html>
标签的class属性就会被相应的赋值,以显示浏览器是否支持某类CSS属性。好比在IE6下面,不支持boderradius特性,那么在 <html>
标签中就会出现 no-borderradius
类,咱们能够作一些fallback的工做:
.no-borradius div{ /*-- do some hacks here --*/ }
上面咱们已经介绍了,检测浏览器是否支持某项特性,咱们能够用这种语法:
Modernizr.featuretodetect
再举一个一般的例子
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script> <script>window.jQuery || document.write('<script src="js/libs/jquery-1.7.1.min.js">\x3C/script>')</script>
通常咱们都会这样加载jQuery类库,先从Google CDN上拿,若是没拿到再加载本地的。
Modernizr.load()根据一些条件判断来动态选择加载CSS和JavaScript,这无疑对避免没必要要的资源加载有极大的帮助。
那么上面的例子能够用Modernizr.load写成以下所示
Modernizr.load([ { load: '//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js', complete: function () { if ( !window.jQuery ) { Modernizr.load('js/libs/jquery-1.7.1.min.js'); } } }, { // This will wait for the fallback to load and // execute if it needs to. load: 'needs-jQuery.js' } ]);
事实上Modernizr.load最简单的语法以下所示:
Modernizr.load( test: Modernizr.webgl, yep : 'three.js', nope: 'jebgl.js' );
也就是当浏览器支持WebGL的时候,就引入 three.js 这个类库作一些3D效果。浏览器不支持WebGL的时候可使用 jebgl.js 作一些fallback操做。
有兴趣能够去官网看看具体API