HTML5 CSS3新特性的浏览器支持情况.docx_第1页
HTML5 CSS3新特性的浏览器支持情况.docx_第2页
HTML5 CSS3新特性的浏览器支持情况.docx_第3页
HTML5 CSS3新特性的浏览器支持情况.docx_第4页
HTML5 CSS3新特性的浏览器支持情况.docx_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

代码检测HTML5/CSS3新特性的浏览器支持情况伴随着今年10月底HTML5标准版的发布,未来使用H5的场景会越来越多,这是令web开发者欢欣鼓舞的事情。然而有一个现实我们不得不看清,那就是IE系列浏览器还占有一大部分市场份额,以IE8、9为主,windows8.1的用户已经用上了IE10/11,而考虑我国的国情,IE6、7依然存留不少。在我们放手用HTML5开发的时候,新特性支持度检测就是必不可少的了。一种方式是用navigator.userAgent或navigator.appName来检测浏览器类型和版本,不过这种方式不是很可靠,浏览器对于一些新特性也是在逐渐支持,不能肯定说某个浏览器100%支持了HTML5。而且,IE11做了一个恶心的举动:在UA中去掉了“MSIE”标志,把appName改为了“Netspace”,并且开始支持-webkit-前缀的css属性,这是活生生要伪装成chrome的节奏。所以,HTML5/CSS3支持性的检测,还是靠特征检测(figure detection)或者说能力检测更好些。本篇就来介绍一下常用的检测方式都有哪些。HTML5部分检测HTML5新特性的方法主要有以下几种:1. 检查全局对象(window或navigator)上有没有相应的属性名2. 创建一个元素,检查元素上有没有相应的属性3. 创建一个元素,检测元素上有没有方法名称,然后调用该方法,看能否正确执行4. 创建一个元素,为元素的相应属性赋一个值,然后再获取此属性的值,看看赋值是否生效由于不同浏览器的不同行为,检测一些特性的时候,可能会用到上述几个方法的组合,接下来用上面的方法做一下常用特性的检测:canvas1. functionsupport_canvas()2. varelem=document.createElement(canvas);3. return!(elem.getContext&elem.getContext(2d);4. 一般来讲,创建canvas元素并检查getContext属性即可,但是在一些浏览器下不够准确,所以再检测一下elem.getContext(2d)的执行结果,就可以完全确定。以上代码摘自Modernizr:/Modernizr/Modernizr/issues/issue/97/关于canvas,有一点要补充的,那就是fillText方法,尽管浏览器支持canvas,我们并不能确切的确定它支持fillText方法,因为canvas API经历了各种修改,有一些历史原因,检测支持fillText的方法如下:1. functionsupport_canvas_text()2. varelem=document.createElement(canvas);3. varcontext=elem.getContext(2d);4. returntypeofcontext.fillText=function;5. video/audio1. functionsupport_video()2. return!document.createElement(video).canPlayType;3. 4. 5. functionsupport_audio()6. return!document.createElement(audio).canPlayType;7. video和audio的写法相似。要检测video/audio支持的资源格式,可以调用canPlayType方法来进行检查,具体如下:1. unctionsupport_video_ogg()2. varelem=document.createElement(video);3. returnelem.canPlayType(video/ogg;codecs=theora);4. 5. functionsupport_video_h264()6. varelem=document.createElement(video);7. returnelem.canPlayType(video/mp4;codecs=avc1.42E01E);8. 9. functionsupport_video_webm()10. varelem=document.createElement(video);11. returnelem.canPlayType(video/webm;codecs=vp8,vorbis);12. 13. 14. functionsupport_audio_ogg()15. varelem=document.createElement(audio);16. returnelem.canPlayType(audio/ogg;codecs=vorbis);17. 18. functionsupport_audio_mp3()19. varelem=document.createElement(audio);20. returnelem.canPlayType(audio/mpeg;);21. 22. functionsupport_audio_wav()23. varelem=document.createElement(wav);24. returnelem.canPlayType(audio/wav;codecs=1);25. 要注意的是,canPlayType的返回值并不是布尔类型,而是字符串,取值有以下几种: probably:浏览器完全支持此格式 maybe:浏览器可能支持此格式 :空串,表示不支持 localStorage一般来讲,检查全局对象是否有localStorage属性即可,如下:1. functionsupport_localStorage()2. try3. returnlocalStorageinwindow&windowlocalStorage!=null;4. 5. catch(e)6. returnfalse;7. 8. 在一些浏览器禁用cookie,或者设置了隐私模式什么的情况,检查属性或报错,所以加在try语句中,如果报错了认为不支持。另外,还有一种更严格的检查方法,检查相应方法是否支持,如下:1. functionsupport_localStorage()2. try3. if(localStorageinwindow&windowlocalStorage!=null)4. localStorage.setItem(test_str,test_str);5. localStorage.removeItem(test_str);6. returntrue;7. 8. returnfalse;9. 10. catch(e)11. returnfalse;12. 13. webWorker1. functionsupport_webWorker()2. return!window.Worker;3. applicationCache1. functionsupport_applicationCache()2. return!window.applicationCache;3. geolocation1. functionsupport_geolocation()2. returngeolocationinnavigator;3. input标签新属性input标签新增的属性包括:autocomplete、autofocus、list、placeholder、max、min、multiple、pattern、required、step,检测是否支持用上面提到的方法2即可,新建一个input标签,看是否有这些属性,以autocomplete为例:1. functionsupport_input_autocomplete()2. varelem=document.createElement(input);3. returnautocompleteinelem;4. 另外要特别注意list属性,因为它是与datalist标签连用的,所以检查的时候要一并检测datalist标签是否支持:1. functionsupport_input_list()2. varelem=document.createElement(input);3. return!(listinelem&document.createElement(datalist)&window.HTMLDataListElement);4. input标签新类型这里的类型就是指type的取值,input新增的type值包括:search、tel、url、email、datetime、date、month、week、time、datetime-local、number、range、color。检测这些值需要用上面提到的方法4,以number为例:1. functionsupport_input_type_number()2. varelem=document.createElement(input);3. elem.setAttribute(type,number);4. returnelem.type!=text;5. 这是一种较为简单的检查方法,因为严格来讲,浏览器会为不同的类型提供不同的外观或实现,例如chrome中range类型会长这样:我们要确切的知道浏览器为该类型提供了对应的实现,才可以认为是“支持的”,要从这个方面检测是有难度的,各浏览器实现都不一。下面贴出Modernizr中的一个实现,供参考:检测email类型的实现:1. functionsupport_input_type_email()2. varelem=document.createElement(input);3. elem.setAttribute(type,email);4. elem.value=:);5. returnelem.checkValidity&elem.checkValidity()=false;6. 为email类型设置了一个非法的value,然后手动调用校验方法,如果返回false,说明浏览器提供了该类型的实现,认为是支持的。historyhistory虽说是HTML4就有的,但HTML5提供了新的方法,检测方法如下:1. functionsupport_history()2. return!(window.history&history.pushState);3. webgl1. functionsupport_webgl()2. return!window.WebGLRenderingContext;3. postMessage1. functionsupport_postMessage()2. return!window.postMessage;3. draggableHTML5的拖拽特性:1. functionsupport_draggable()2. vardiv=document.createElement(div);3. return(draggableindiv)|(ondragstartindiv&ondropindiv);4. webSocket1. unctionsupport_webSocket()2. returnWebSocketinwindow|MozWebSocketinwindow;3. svg1. functionsupport_svg()2. return!document.createElementNS&!document.createElementNS(/2000/svg,svg).createSVGRect;3. 事件的支持性判断通用方法:检查事件的支持性,用上面提到的方法2就可以,创建一个元素,看元素上有没有对应的事件名称,下面是摘自Modernizr的实现:1. isEventSupported=(function()2. 3. varTAGNAMES=4. select:input,change:input,5. submit:form,reset:form,6. error:img,load:img,abort:img7. ;8. 9. functionisEventSupported(eventName,element)10. 11. elementelement=element|document.createElement(TAGNAMESeventName|div);12. eventName=on+eventName;13. 14. /WhenusingsetAttribute,IEskipsunload,WebKitskipsunloadandresize,whereasincatchesthose15. varisSupported=eventNameinelement;16. 17. if(!isSupported)18. /IfithasnosetAttribute(i.e.doesntimplementNodeinterface),trygenericelement19. if(!element.setAttribute)20. element=document.createElement(div);21. 22. if(element.setAttribute&element.removeAttribute)23. element.setAttribute(eventName,);24. isisSupported=is(elementeventName,function);25. 26. /Ifpropertywascreated,removeit(bysettingvaluetoundefined)27. if(!is(elementeventName,undefined)28. elementeventName=undefined;29. 30. element.removeAttribute(eventName);31. 32. 33. 34. element=null;35. returnisSupported;36. 37. returnisEventSupported;38. )()touch事件如果仅仅是检查touch事件是否支持,就没必要写上面那么多东西了,可以简单书写如下:1. functionsupport_touch_event()2. return!(ontouchstartinwindow)|window.DocumentTouch&documentinstanceofDocumentTouch);3. Mozilla还提供了一个媒体查询的语句用来检测touch的支持性:touch-enabled,可以在页面上插入一个带有此媒体查询的元素来判断是否支持touch事件。参考:/css/mediaqueries/touch.html不过我们做移动开发一般都只考虑webkit内核了,Mozilla的方式就不写了,如果需要请参考Modernizr源码。css3部分通用方法css3属性支持度的检测,主要通过上面方法提到的2和4来检查,不过我们要检查的是元素的style属性。另外一个要提的就是浏览器私有前缀,在现阶段我们用css3属性大部分是要写前缀的,因为各浏览器还未完全支持。我们用到的前缀有:-webkit-、-ms-、-o-、-moz-,至于前缀-khtml-,这是Safari早期使用的,现在基本也没有人再用它了,所以进行检测的时候就把它省去了。Modernizr移除了此前缀的检测,详见:/Modernizr/Modernizr/issues/454通用代码如下1. varsupport_css3=(function()2. vardiv=document.createElement(div),3. vendors=MsOMozWebkit.split(),4. len=vendors.length;5. 6. returnfunction(prop)7. if(propindiv.style)returntrue;8. 9. propprop=prop.replace(/a-z/,function(val)10. returnval.toUpperCase();11. );12. 13. while(len-)14. if(vendorslen+propindiv.style)15. returntrue;16. 17. 18. returnfalse;19. ;20. )();3D变形css3 3D变形的检测稍微复杂些,首先要支持perspective属性,其次要支持transform-style的值为preserve-3d。用方法4的方式无法检测出来,需要借助媒体查询的方式,代码如下:1. functionsupport_css3_3d()2. vardocElement=document.documentElement;

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论