浏览器兼容问题
  # 浏览器兼容
by 若愚@饥人谷
# 什么是浏览器兼容问题
同一份代码,有的浏览器效果正常,有的不正常
- 不正常的原因是什么?(不支持? bug?)
 - 如何让它展示正常?(条件注释? 单独Hack?)
 
# 为什么会有浏览器兼容问题
- 同一产品,版本越老 bug 越多
 - 同一产品,版本越新,功能越多
 - 不同产品,不同标准,不同实现方式
 
# 用到的网站
- 浏览器市场份额 (opens new window)
 - caniuse.com (opens new window) 查CSS属性兼容
 - browserhacks (opens new window) 查 Hack 的写法
 
# 处理兼容问题的思路
- 要不要做
- 产品的角度(产品的受众、受众的浏览器比例、效果优先还是基本功能优先)
 - 成本的角度 (有无必要做某件事)
 
 - 做到什么程度
- 让哪些浏览器支持哪些效果
 
 - 如何做
- 根据兼容需求选择技术框架/库(jquery)
 - 根据兼容需求选择兼容工具(html5shiv.js (opens new window)、respond.js (opens new window)、css reset (opens new window)、normalize.css (opens new window)、Modernizr (opens new window))
 - postCSS (opens new window)
 - 条件注释、CSS Hack、js 能力检测做一些修补
 
 
# 渐进增强和优雅降级
渐进增强(progressive enhancement): 针对低版本浏览器进行构建页面,保证最基本的功能,然后再针对高级浏览器进行效果、交互等改进和追加功能达到更好的用户体验优雅降级(graceful degradation): 一开始就构建完整的功能,然后再针对低版本浏览器进行兼容。
stackoverflow-渐进增强和优雅降级的区别 (opens new window)
# 处理兼容问题的手段
# 合适的框架
- Bootstrap (>=ie8)
 - jQuery 1.~ (>=ie6), jQuery 2.~ (>=ie9)
 - Vue (>= ie9)
 - ...
 
# 条件注释
条件注释 (conditional comment) 是于HTML源码中被IE有条件解释的语句。条件注释可被用来向IE提供及隐藏代码。
    <!--[if IE 6]>
    <p>You are using Internet Explorer 6.</p>
    <![endif]-->
    <!--[if !IE]><!-->
    <script>alert(1);</script>
    <!--<![endif]-->
    <!--[if IE 8]>
    <link href="ie8only.css" rel="stylesheet">
    <![endif]-->
 1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
使用了条件注释的页面在 Windows Internet Explorer 9 中可正常工作,但在 Internet Explorer 10 中无法正常工作。 IE10不再支持条件注释 (opens new window)
# 条件注释
| 项目 | 范例 | 说明 | 
|---|---|---|
| ! | [if !IE] | 非IE | 
| lt | [if lt IE 5.5] | 小于IE 5.5 | 
| lte | [if lte IE 6] | 小于等于IE6 | 
| gt | [if gt IE 5] | 大于 IE5 | 
| gte | [if gte IE 7] | 大于等于IE7 | 
| | | [if (IE 6)|(IE 7)] | IE6或者IE7 | 
# CSS hack
由于不同厂商的浏览器,比如Internet Explorer,Safari,Mozilla Firefox,Chrome等,或者是同一厂商的浏览器的不同版本,如IE6和IE7,对CSS的解析认识不完全一样,因此会导致生成的页面效果不一样,得不到我们所需要的页面效果。
这个时候我们就需要针对不同的浏览器去写不同的CSS,让它能在不同的浏览器中也能得到我们想要的页面效果。
# CSS hack
CSS Hack大致有3种表现形式,CSS属性前缀法、选择器前缀法以及IE条件注释法(即HTML头部引用if IE)Hack,实际项目中CSS Hack大部分是针对IE浏览器不同版本之间的表现差异而引入的。
- 属性前缀法(即类内部Hack):例如 IE6能识别下划线""和星号" * ",IE7能识别星号" * ",但不能识别下划线"",IE6~IE10都认识"\9",但firefox前述三个都不能认识
 - 选择器前缀法(即选择器Hack)
 - IE条件注释法(即HTML条件注释Hack):针对所有IE(注:IE10+已经不再支持条件注释): 
<!--[if IE]>IE浏览器显示的内容 <![endif]-->,针对IE6及以下版本:<!--[if lt IE 6]>只在IE6-显示的内容 <![endif]-->。这类Hack不仅对CSS生效,对写在判断语句里面的所有代码都会生效 
# 常见hack写法
.box{
  color: red;
  _color: blue; /*ie6*/
  *color: pink; /*ie67*/
  color: yellow\9;  /*ie/edge 6-8*/
}
 1
2
3
4
5
6
2
3
4
5
6
<!–-[if IE 7]>
<link rel="stylesheet" href="ie7.css" type="text/css" />
<![endif]–->
 1
2
3
2
3
# 常见属性的兼容情况
inline-block: >=ie8min-width/min-height: >=ie8:before,:after: >=ie8div:hover: >=ie7inline-block: >=ie8background-size: >=ie9- 圆角: >= ie9
 - 阴影: >= ie9
 - 动画/渐变: >= ie10
 
# 常见兼容处理范例
.clearfix:after{
  content: '';
  display: block;
  clear: both;
}
.clearfix{
  *zoom: 1; /* 仅对ie67有效 */
}
 1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 常见兼容处理范例
.target{
  display: inline-block;
  *display: inline;
  *zoom: 1;
}
 1
2
3
4
5
6
2
3
4
5
6
# 常见兼容处理范例
 <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->
 1
2
3
4
2
3
4
<!DOCTYPE html>
<!--[if IEMobile 7 ]> <html dir="ltr" lang="en-US"class="no-js iem7"> <![endif]-->
<!--[if lt IE 7 ]> <html dir="ltr" lang="en-US" class="no-js ie6 oldie"> <![endif]-->
<!--[if IE 7 ]>    <html dir="ltr" lang="en-US" class="no-js ie7 oldie"> <![endif]-->
<!--[if IE 8 ]>    <html dir="ltr" lang="en-US" class="no-js ie8 oldie"> <![endif]-->
<!--[if (gte IE 9)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html dir="ltr" lang="en-US" class="no-js"><!--<![endif]-->
 1
2
3
4
5
6
2
3
4
5
6
# 一些和兼容相关的开发利器
# html5shiv.js (opens new window)
# respond.js (opens new window)
# css reset (opens new window)
# normalize.css (opens new window)
# Modernizr (opens new window)
# 其他
# 如何调试老掉牙的 IE
- 安装虚拟机
 - 给元素添加border
 
上次更新: 2024/07/21, 21:46:04