html5中文学习网

您的位置: 首页 > 视频教程 > CSS3视频教程 » 正文

闲谈CSS3动画_HTML5中文网 - 中国领先的HTML5技术门户

[ ] 已经帮助:人解决问题

 随着CSS3越来越热,CSS3动画也逐渐受到大家的关注。这次有幸修改淘宝网全站页头,小小地应用了下(详见http://www.taobao.com/下拉箭头处的hover效果)。与其说是渐进增强,不如说是满足了技术人小小的虚荣心。pPAHTML5中文学习网 - HTML5先行者学习网

以下是自己的一点理解,希望能对大家有所帮助。pPAHTML5中文学习网 - HTML5先行者学习网

pPAHTML5中文学习网 - HTML5先行者学习网

淘宝案例解析

pPAHTML5中文学习网 - HTML5先行者学习网

需求:鼠标移动到菜单上时旋转箭头,且给支持CSS3的浏览器加上旋转动画。pPAHTML5中文学习网 - HTML5先行者学习网

源码请查看demo源文件。pPAHTML5中文学习网 - HTML5先行者学习网

demo (http://fiddle.jshell.net/pjRVT/show/light/)pPAHTML5中文学习网 - HTML5先行者学习网

pPAHTML5中文学习网 - HTML5先行者学习网

关于CSS3动画

从(http://www.w3.org/Style/CSS/current-work)可以看出,CSS动画涉及的知识点包括 CSS 2D Transformations, CSS 3D Transformations, CSS Transitions, CSS Animations。pPAHTML5中文学习网 - HTML5先行者学习网

Transformation 补充定义了width, height, left, top等之外的一些可用于实现动画的属性,如rotate, scale, skew。pPAHTML5中文学习网 - HTML5先行者学习网
Transition 和 Animation 用于定义动画的过程。其中Transition 可以实现简单的动画;Animation则可以通过设定多个关键帧实现相对复杂的动画。pPAHTML5中文学习网 - HTML5先行者学习网

pPAHTML5中文学习网 - HTML5先行者学习网

Can I Use? 兼容性

(数据来自http://caniuse.com/)pPAHTML5中文学习网 - HTML5先行者学习网

  IE Firefox Safari Chrome Opera
CSS 2D Transform no 3.5 3.2 2.0 10.5
CSS 3D Transform no no 4.* (Mac) no no
CSS Transition no 3.7 3.2 2.0 10.5
CSS Animation no no 4.0 2.0 no

 pPAHTML5中文学习网 - HTML5先行者学习网

可以看到,CSS Animation目前只有Webkit内核浏览器支持,目前只能自己玩玩;而Transition用来做渐进增强则较为合适。pPAHTML5中文学习网 - HTML5先行者学习网

pPAHTML5中文学习网 - HTML5先行者学习网

一个简单的例子

需求:让一个div元素在鼠标移上去时变大1倍,旋转180度,且背景由红变蓝。pPAHTML5中文学习网 - HTML5先行者学习网

html code::pPAHTML5中文学习网 - HTML5先行者学习网

<div></div>

css code::pPAHTML5中文学习网 - HTML5先行者学习网

div {    position: absolute;    left: 100px;    top: 100px;    width: 100px;    height: 100px;    background: red;    /* 定义动画的过程 */    -webkit-transition: -webkit-transform .5s ease-in, background .5s ease-in;    -moz-transition:    -moz-transform .5s ease-in, background .5s ease-in;    -o-transition:      -o-transform .5s ease-in, background .5s ease-in;    transition:         transform .5s ease-in, background .5s ease-in;}div:hover {    /*  定义动画的状态 */    -webkit-transform: rotate(180deg) scale(2);    -moz-transform: rotate(180deg) scale(2);    -o-transform: rotate(180deg) scale(2);    -transform: rotate(180deg) scale(2);    background: blue;}

demo (http://fiddle.jshell.net/NVErB/show/light/) (no IE)pPAHTML5中文学习网 - HTML5先行者学习网

pPAHTML5中文学习网 - HTML5先行者学习网

无奈的浏览器前缀

这是个令人非常痛苦的问题,因为不得不针对每个浏览器copy一遍重复代码。pPAHTML5中文学习网 - HTML5先行者学习网

值得注意的是无前缀的标准代码需放在最后。假如几年后某个属性成为标准,被浏览器默认支持了,这行代码会覆盖前面的定义,使得浏览器可以优先使用他。pPAHTML5中文学习网 - HTML5先行者学习网

pPAHTML5中文学习网 - HTML5先行者学习网

稍微复杂点的例子(css3 animation)

需求:让一个div元素在点击后变大1倍,旋转180度,且背景由红变蓝;然后向右移动400px。pPAHTML5中文学习网 - HTML5先行者学习网

源码请查看demo源文件。pPAHTML5中文学习网 - HTML5先行者学习网

demo (http://fiddle.jshell.net/a4r94/show/light/) (Safari, Chrome only)pPAHTML5中文学习网 - HTML5先行者学习网

pPAHTML5中文学习网 - HTML5先行者学习网

惊艳!CSS 3D Transformations

见live demo (http://www.satine.org/research/webkit/snowleopard/snowstack.html) (Mac Safari Only,类似于http://www.cooliris.com/的效果),没Mac的可以到(http://www.satine.org/archives/2009/07/11/snow-stack-is-here/)看视频演示。pPAHTML5中文学习网 - HTML5先行者学习网

PS: Mac Safari的3D Transform、2D Transform和Opacity等视觉效果都是跑在GPU上的,为此我还特地验证下了Win Safari,果然不支持。pPAHTML5中文学习网 - HTML5先行者学习网

pPAHTML5中文学习网 - HTML5先行者学习网

相关资料

webkit blog介绍animation/2d transforms/3d transforms的三篇文章pPAHTML5中文学习网 - HTML5先行者学习网
http://webkit.org/blog/138/css-animation/pPAHTML5中文学习网 - HTML5先行者学习网
http://webkit.org/blog/130/css-transforms/pPAHTML5中文学习网 - HTML5先行者学习网
http://webkit.org/blog/386/3d-transforms/pPAHTML5中文学习网 - HTML5先行者学习网

W3组织的CSS规范集pPAHTML5中文学习网 - HTML5先行者学习网
http://www.w3.org/Style/CSS/current-workpPAHTML5中文学习网 - HTML5先行者学习网

苹果官方关于CSS视觉效果的文档pPAHTML5中文学习网 - HTML5先行者学习网
http://developer.apple.com/safari/library/documentation/InternetWeb/Conceptual/SafariVisualEffectsProgGuide/Introduction/Introduction.htmlpPAHTML5中文学习网 - HTML5先行者学习网

css animation的兼容性数据来源pPAHTML5中文学习网 - HTML5先行者学习网
http://caniuse.com/pPAHTML5中文学习网 - HTML5先行者学习网

3d transform的运用apppPAHTML5中文学习网 - HTML5先行者学习网
http://www.satine.org/research/webkit/snowleopard/snowstack.htmlpPAHTML5中文学习网 - HTML5先行者学习网
http://css-vfx.googlecode.com/svn/trunk/examples/zflow.htmlpPAHTML5中文学习网 - HTML5先行者学习网
http://www.fofronline.com/experiments/cube-3d/pPAHTML5中文学习网 - HTML5先行者学习网

css3动画的应用pPAHTML5中文学习网 - HTML5先行者学习网
http://www.webdesignerwall.com/trends/47-amazing-css3-animation-demos/pPAHTML5中文学习网 - HTML5先行者学习网
http://www.optimum7.com/internet-marketing/web-development/pure-css3-spiderman-ipad-cartoon-jquery-html5-no-flash.htmlpPAHTML5中文学习网 - HTML5先行者学习网
http://www.optimum7.com/css3-man/animation.htmlpPAHTML5中文学习网 - HTML5先行者学习网
http://hedgerwow.appspot.com/demo/flippagepPAHTML5中文学习网 - HTML5先行者学习网
http://neography.com/journal/our-solar-system-in-css3/pPAHTML5中文学习网 - HTML5先行者学习网
http://css-tricks.com/examples/StarryNightCSS3/pPAHTML5中文学习网 - HTML5先行者学习网
http://www.dmxzone.com/demo/dmxAnimator/effects/slide_out_menu.htmlpPAHTML5中文学习网 - HTML5先行者学习网

css3 animation的入门应用:钟的实现pPAHTML5中文学习网 - HTML5先行者学习网
http://g-zone.org/test/g-clock/index.htmlpPAHTML5中文学习网 - HTML5先行者学习网

(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助