Autoprefixer解析CSS文件并且添加浏览器前缀到CSS规则里,使用Can I Use的数据来决定哪些前缀是需要的。

所有你需要做的就是把它添加到你的资源构建工具(例如 Grunt)并且可以完全忘记有CSS前缀这东西。尽管按照最新的W3C规范来正常书写你的CSS而不需要浏览器前缀。像这样:

1
2
3
a{
     transition :transform 1s
}

Autoprefixer使用一个数据库根据当前浏览器的普及度以及属性支持提供给你前缀:

1
2
3
4
5
a{
     -webkit-transition :-webkit-transform 1s;
     transition :-ms-transform 1s;
     transition :transform 1s
}

问题

 
当然我们可以手写浏览器前缀,但是这显得乏味以及易错。
 
我们也可以使用类似Prefixr这样的服务以及编辑器插件,但这仍然乏于跟一堆重复的代码打交道。
 
我们可以使用象Compass之于Sass以及nib之于Stylus之类的预处理器提供的mixin库。它们可以解决绝大部分问题,但同时又引入了其他问题。
它们强制我们使用新的语法。它们迭代慢于现代浏览器,所以当稳定版发布时会产生很多不必要的前缀,有时我们需要创建自己的mixins。
 
Compass实际上并没有为你屏蔽前缀,因为你依然需要决定许多问题,例如:我需要为 border-radius 写一个mixin吗?我需要用逗号分割 +transition 的参数吗?
 
Lea Verou的-prefix-free几乎解决了这个问题,但是使用客户端库并不是个好注意,当你把最终用户性能考虑进去的话。为了防止反复做同样的事情,最好是在资源构建或者项目发布时再构建一次CSS。
 
 
揭秘
 
Autoprefixer是一个后处理程序,不象Sass以及Stylus之类的预处理器。它适用于普通的CSS而不使用特定的语法。可以轻松跟Sass以及Stylus集成,由于它在CSS编译后运行。
 
Autoprefixer基于Rework,一个可以用来编写你自己的CSS后处理程序的框架。Rework解析CSS成有用Javascript结构经过你的处理后导回给CSS。
 
Autoprefixer的每个版本都包含一份最新的 Can I Use 数据:
  • 当前浏览器列表以及它们的普及度。
  • 新CSS属性,值和选择器前缀列表。
Autoprefixer默认将支持主流浏览器最近2个版本,这点类似Google。不过你可以在自己的项目中通过名称或者模式进行选择:
  • 主流浏览器最近2个版本用“last 2 versions”;
  • 全球统计有超过1%的使用率使用“>1%”;
  • 仅新版本用“ff>20”或”ff>=20″.
然后Autoprefixer计算哪些前缀是需要的,哪些是已经过期的。
 
当Autoprefixer添加前缀到你的CSS,还不会忘记修复语法差异。这种方式,CSS是基于最新W3C规范产生:
1
2
3
4
5
6
7
a {
     background : linear-gradient(to top, black, white);
     display : flex
}
::placeholder {
     color : #ccc
}
编译成:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
a {
    background : -webkit-linear-gradient(bottom, black, white);
    background : linear-gradient(to top, black, white);
    display : -webkit-box;
    display : -webkit-flex;
    display : -moz-box;
    display : -ms-flexbox;
    display : flex
}
:-ms-input-placeholder {
    color : #ccc
}
::-moz-placeholder {
    color : #ccc
}
::-webkit-input-placeholder {
    color : #ccc
}
::placeholder {
    color : #ccc
}
Autoprefixer 同样会清理过期的前缀(来自遗留的代码或类似 Bootstrap CSS库),因此下面的代码:
1
2
3
4
a {
    -webkit-border-radius : 5px;
    border-radius : 5px
}
编译成:
1
2
3
a {
    border-radius : 5px
}
因为经过Autoprefixer处理,CSS将仅包含实际的浏览器前缀。Fotorama从Compass切换到Autoprefixer之后,CSS大小减少了将近20%。

 
 
演示
 
如果你还没用过任何工具来自动化构建你的静态资源,一定要尝试下Grunt,我强烈推荐你开始使用构建工具。这将开启你整个语法糖世界,高效的mixin库以及实用的图片处理工具。所有开发者的高效方法用来节约大量精力以及时间(自由选择语言,代码服用,使用第三方库的能力)现将都适用于前端开发人员。
 
让我们创建一个项目目录以及在style.css中写些简单的CSS:
 
style.css
{ }
 
在这个例子中,我们将使用Grunt。首先需要使用npm安装 grunt-autoprefixer :
npm install grunt-cli grunt-contrib-watch grunt-autoprefixer
 
然后我们需要创建 Gruntfile.js 文件以及启用Autoprefixer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Gruntfile.js
module.exports = function (grunt) {
     grunt .initConfig ({
          autoprefixer : {
               dist : {
                    files : { 'build/style.css' : 'style.css' } } },
                    watch : {
                         styles : {
                              files : ['style.css' ],
                              tasks : ['autoprefixer' ]
                         }
                    }
               });
  
grunt.loadNpmTasks('grunt-autoprefixer' );
grunt.loadNpmTasks('grunt-contrib-watch' );};
此配置文件可以让Autoprefixer编译 style.css 到 build/style.css. 同样我们将用 grunt-contrib-watch监听style.css文件变化重新编译build/style.css。

 
启用Grunt的Watch功能:
./node_modules/.bin/grunt watch
 
现在我们添加一个CSS3表达式到style.css并保存:
style.css
1
2
3
a {
     width : calc(50% - 2em)
}
接下来是见证奇迹的时刻,现在我有了build/style.css文件,Grunt监测到style.css文件发生变化并启用Autoprefixer任务。

Autoprefixer发现了calc() 值单元需要Safari 6的浏览器前缀
build/style.css
1
2
3
4
a {
     width : -webkit-calc(50% - 2em);
     width : calc(50% - 2em)
}
我们再添加多一点点复杂的CSS3到style.css并保存:

style.css
1
2
3
4
a {
     width : calc(50% - 2em);
     transition : transform 1s
}
Autoprefixer已知Chrome,Safari 6以及Opera 15需要transitiontransform 添加前缀。但IE9也需要为transform添加前缀,作为transition的值。

 
build/style.css
1
2
3
4
5
6
7
a {
     width : -webkit-calc(1% + 1em);
     width : calc(1% + 1em);
     -webkit-transition : -webkit-transform 1s;
     transition : -ms-transform 1s;
     transition : transform 1s
}
Autoprefixer为完成你所有脏活而设计。它会根据Can I Use数据库,写入所有需要的前缀并且同样理解规范之间的区别,欢迎来到未来的CSS3 – 不再有浏览器前缀!

 
下一步?
 
1、Autoprefixer支持Ruby on Rails,MiddlemanMincer,Grunt,Sublime Text。了解更多关于如何在你的环境中使用的文档
2、如果你的环境还不支持Autoprefixer,请报告给我
3、关注@autoprefixer获得更新以及新特性信息。

最新文章

  1. FFmpeg 转码和截屏
  2. iOS--使用UIImageView进行GIF动图播放
  3. 一个实现了View接口的Fragment
  4. EF深入系列--Code First
  5. javascript 常用实用函数。。。。。。
  6. 用meta-data配置参数
  7. 【学习笔记】【C语言】结构体
  8. SQL中 patindex函数的用法
  9. C#运行时鼠标移动控件 - 调用Windows API(ReleaseCapture)
  10. MFC中使用ADO方式连接数据库
  11. python web with bottle and session (beaker)
  12. 方向梯度直方图(HOG)和颜色直方图的一些比較
  13. laravel多种安装方法
  14. Redux源码分析之combineReducers
  15. js 前端操作的分页路由设计
  16. SuperMap iObject入门开发系列之一组件式GIS开发平台介绍
  17. 一丶HTML介绍
  18. H5 实现图片上传预览
  19. linux网关设置
  20. JAVA常用工具类汇总

热门文章

  1. IOS YYKit 源码解析
  2. OpenJudge 2971 抓住那头牛
  3. C#、.Net学习资料免注册下载基地。。。
  4. hdu 2732 Leapin' Lizards 最大流 拆点 建图
  5. sooket数据成传输压缩
  6. 关于platform_device和platform_driver的匹配【转】
  7. android的布局-----FrameLayout(帧布局)
  8. HashMap和TreeMap的常用排序方法
  9. docker下使用DB2
  10. es6 解构写法:给变量取别名