css3动画从入门到精通
什么是css3动画?
通过 CSS3,我们能够创建动画,这可以在许多网页中取代动画图片、Flash 动画以及 JavaScript。
CSS3带来了圆角,半透明,阴影,渐变,多背景图等新的特征,轻松实现了设计稿中常见的图层样式,用简洁的代码替代图片,代替了多余的空标签。
现成的CSS3动画库:http://leaverou.github.io/animatable/
background-color
- .background-color { background:#708090; animation:1s ease 0s alternate none infinite background-color;}
- @keyframes background-color{
- from{ background:#708090;}
- to{ background:#000;}
- }
background-position
background-position 属性设置背景图像的起始位置,默认值:0% 0%,效果等同于left top
范例一:
- .background-position-1 { background-image: -moz-linear-gradient(-45deg, transparent 25%, black 25%, black 50%, transparent 50%, transparent 75%, black 75%, black); background-size:50px 50px; animation:1s ease 0s alternate none infinite background-position-1;}
- @keyframes background-position-1{
- from{ background-position:0 0;}
- to{ background-position:100% 100%;}
- }
范例二:
- .background-position-2 { background-color: #E04332; background-repeat: repeat-x; background-size: 24px 300px; background-image: linear-gradient(-45deg, slategray 25%, transparent 25%), linear-gradient(45deg, transparent 75%, slategray 75%), linear-gradient(45deg, slategray 25%, transparent 25%), linear-gradient(-45deg, transparent 75%, slategray 75%); animation:1s ease 0s alternate none infinite background-position-2;}
- @keyframes background-position-2{
- from{ background-position:3px 0, 3px 0, 15px -150px, 15px -150px;}
- to{ background-position:3px -70px, 3px -70px, 15px -80px, 15px -80px;}
- }
background-size
background-size 属性规定背景图片的尺寸。在 CSS3 之前,背景图片的尺寸是由图片的实际尺寸决定的。在CSS3中,可以规定背景图片的尺寸,这就允许我们在不同的环境中重复使用背景图片。
- .background-size {background-image: repeating-radial-gradient(transparent, transparent 9px, black 11px, black 20px); background-position:center; animation:1s ease 0s alternate none infinite background-size;}
- @keyframes background-size{
- from{ background-size:5px 5px;}
- to{ background-size:150px 150px;}
- }
border-radius
border-radius 属性是一个简写属性,用于设置四个 border-*-radius 属性。
范例一
- .border-radius-1 { animation:1s ease 0s alternate none infinite border-radius-1;}
- @keyframes border-radius-1{
- from{ border-radius:0;}
- to{ border-radius:50%;}
- }
范例二
- .border-radius-2 { animation:1s ease 0s alternate none infinite border-radius-2;}
- @keyframes border-radius-2{
- from{ border-radius:0 100%;}
- to{ border-radius:100% 0;}
- }
border-width
范例一
- .border-width-1 {border-style:solid; border-color: black; animation:1s ease 0s alternate none infinite border-width-1;}
- @keyframes border-width-1{
- from{ border-width:0;}
- to{ border-width:75px;}
- }
范例二
- .border-width-2 {border-style:dashed; border-color: black; animation:1s ease 0s alternate none infinite border-width-2;}
- @keyframes border-width-2{
- from{ border-width:0;}
- to{ border-width:35px;}
- }
范例三
- .border-width-3 {border-style:double; border-color: black; animation:1s ease 0s alternate none infinite border-width-3;}
- @keyframes border-width-3{
- from{ border-width:0;}
- to{ border-width:75px;}
- }
box-shadow
box-shadow 属性向框添加一个或多个阴影
范例一
- .box-shadow-1 { animation:1s ease 0s alternate none infinite box-shadow-1;}
- @keyframes box-shadow-1{
- from{ box-shadow:0 0 black;}
- to{ box-shadow:0 150px 10px -50px rgba(0,0,0,.5);}
- }
范例二
- .box-shadow-2 { animation:1s ease 0s alternate none infinite box-shadow-2;}
- @keyframes box-shadow-2{
- from{ box-shadow:none;}
- to{ box-shadow:inset -75px -75px 400px #000;}
- }
范例三
- .box-shadow-3 { box-radius: 1px; animation:1s ease 0s alternate none infinite box-shadow-3;}
- @keyframes box-shadow-3{
- from{ box-shadow:inset 0 0 75px 75px slategray, 0 0 0 slategray;}
- to{ box-shadow:inset 0 0 35px 35px transparent, 0 0 75px 50px transparent;}
- }
color
CSS3 Color属性在传统的hex和RGB值中增加了HSL特性,也就是增加了Opacity(透明度)和Alpha channels
- .color { animation:1s ease 0s alternate none infinite color;}
- @keyframes color{
- from{ color:white;}
- to{ color:black;}
- }
font-size
- .font-size { animation:1s ease 0s alternate none infinite font-size;}
- @keyframes font-size{
- from{ font-size:60px;}
- to{ font-size:300px;}
- }
width
- .width { animation:1s ease 0s alternate none infinite width;}
- @keyframes width{
- from{ width:150px;}
- to{ width:330px;}
- }
height
- .height { animation:1s ease 0s alternate none infinite height;}
- @keyframes height{
- from{ height:150px;}
- to{ height:0;}
- }
letter-spacing
- .letter-spacing { animation:1s ease 0s alternate none infinite letter-spacing;}
- @keyframes letter-spacing{
- from{ letter-spacing:0;}
- to{ letter-spacing:100px;}
- }
line-height
- .line-height { animation:1s ease 0s alternate none infinite line-height;}
- @keyframes line-height{
- from{ line-height:10px;}
- to{ line-height:300px;}
- }
opacity
- .opacity { animation:1s ease 0s alternate none infinite opacity;}
- @keyframes opacity{
- from{ opacity:1;}
- to{ opacity:0;}
- }
outline-width
- .outline-width { outline-color:black; outline-style: solid; animation:1s ease 0s alternate none infinite outline-width;}
- @keyframes outline-width{
- from{ outline-width:0;}
- to{ outline-width:100px;}
- }
outline-offset
- .outline-offset { outline-style: dashed; outline-color:slategray; animation:1s ease 0s alternate none infinite outline-offset;}
- @keyframes outline-offset{
- from{ outline-offset:-5px;}
- to{ outline-offset:30px;}
- }
outline-color
- .outline-color { outline-width: 30px; outline-style: solid; animation:1s ease 0s alternate none infinite outline-color;}
- @keyframes outline-color{
- from{ outline-color:transparent;}
- to{ outline-color:red;}
- }
padding
- .padding { animation:1s ease 0s alternate none infinite padding;}
- @keyframes padding{
- from{ padding:0;}
- to{ padding:0 200px 0 50px;}
- }
text-indent
- .text-indent { animation:1s ease 0s alternate none infinite text-indent;}
- @keyframes text-indent{
- from{ text-indent:0;}
- to{ text-indent:100px;}
- }
text-shadow
范例一
- .text-shadow-1 { animation:1s ease 0s alternate none infinite text-shadow-1;}
- @keyframes text-shadow-1{
- from{ text-shadow:0 0 black;}
- to{ text-shadow:20px 20px 10px rgba(0,0,0,.5);}
- }
范例二
- .text-shadow-2 { color: transparent; animation:1s ease 0s alternate none infinite text-shadow-2;}
- @keyframes text-shadow-2{
- from{ text-shadow:0 0 0 white;}
- to{ text-shadow:0 0 10px white;}
- }
范例三
- .text-shadow-3 { color: transparent; animation:1s ease 0s alternate none infinite text-shadow-3;}
- @keyframes text-shadow-3{
- from{ text-shadow:0 0 white;}
- to{ text-shadow:0 0 rgba(255,255,255,0), -45px -45px 0 red, -30px -30px 0 orange, -15px -15px 0 yellow, 0 0 0 green, 15px 15px 0 blue, 30px 30px 0 indigo, 45px 45px 0 violet;}
- }
top
- .top { position:relative; animation:1s ease 0s alternate none infinite top;}
- @keyframes top{
- from{ top:0;}
- to{ top:100px;}
- }
范例一
- .transform-1 { animation:1s ease 0s alternate none infinite transform-1;}
- @keyframes transform-1{
- from{ transform:rotate(0deg);}
- to{ transform:rotate(360deg);}
- }
范例二
- .transform-2 { animation:1s ease 0s alternate none infinite transform-2;}
- @keyframes transform-2{
- from{ transform:scale(1);}
- to{ transform:scale(2);}
- }
范例三
- .transform-3 { animation:1s ease 0s alternate none infinite transform-3;}
- @keyframes transform-3{
- from{ transform:skew(0);}
- to{ transform:skew(180deg);}
- }
范例四
- .transform-4 { animation:1s ease 0s alternate none infinite transform-4;}
- @keyframes transform-4{
- from{ transform:rotate(0) scale(1);}
- to{ transform:rotate(360deg) scale(0);}
- }
范例五
- .transform-5 { animation:1s ease 0s alternate none infinite transform-5;}
- @keyframes transform-5{
- from{ transform:perspective(400px) rotateY(0);}
- to{ transform:perspective(400px) rotateY(360deg);}
- }
范例六
- .transform-6 { animation:1s ease 0s alternate none infinite transform-6;}
- @keyframes transform-6{
- from{ transform:perspective(400px) rotateX(0);}
- to{ transform:perspective(400px) rotateX(360deg);}
- }
范例七
- .transform-7 { animation:1s ease 0s alternate none infinite transform-7;}
- @keyframes transform-7{
- from{ transform:perspective(400px) rotateY(0);}
- to{ transform:perspective(400px) translateZ(150px) rotateY(180deg);}
- }
范例八
- .transform-8 { animation:1s ease 0s alternate none infinite transform-8;}
- @keyframes transform-8{
- from{ transform:perspective(400px) translate3d(0,0,0) rotateX(0) rotateY(0) rotateZ(0);}
- to{ transform:perspective(400px) translate3d(0,0,-5000px) rotateX(720deg) rotateY(360deg) rotateZ(-360deg);}
- }
范例九
- .transform-9 { animation:1s ease 0s alternate none infinite transform-9;}
- @keyframes transform-9{
- from{ transform:perspective(400px) rotate3d(.5,.5,0,0deg);}
- to{ transform:perspective(400px) rotate3d(.5,.5,0,180deg);}
- }
范例十
- .transform-10 {transform-origin: 100% 50%; animation:1s ease 0s alternate none infinite transform-10;}
- @keyframes transform-10{
- from{ transform:perspective(400px) rotate3d(0,1,0,0deg);}
- to{ transform:perspective(400px) rotate3d(0,1,0,-180deg);}
- }
范例十一
- .transform-origin {transform:rotate(45deg); animation:1s ease 0s alternate none infinite transform-origin;}
- @keyframes transform-origin{
- from{ transform-origin:50% 50%;}
- to{ transform-origin:0 100%;}
- }
二、CSS3动画详解
1、变形&过渡
- 2D变形&过渡
-
旋转:设定旋转角度,对应css:transform:rotate(ndeg) (工具里n为-360 – 360间的任意数值)
X轴缩放:设定X轴方向缩放,对应css: transform:scaleX(n)(工具里n为-5 – 5间的任意数值)
Y轴缩放:设定Y轴方向缩放,对应css: transform:scaleY(n)(工具里n为-5 – 5间的任意数值)
X轴位移:设定X轴位移,对应css:transform:translateX(npx)
Y轴位移:设定Y轴位移,对应css:transform:translateY(npx)
X轴倾斜:设定X轴倾斜,对应css:transform:skewX(ndeg)
Y轴倾斜:设定Y轴倾斜,对应css:transform:skewY(ndeg)
矩阵变形:设定矩阵变形,对应css:transform:matrix(a,c,e,b,d,f) c,e值用正弦或余弦值表示,ab为缩放值,df为位移值
变形原点 :设定变形原点,对应css:transform-origin:X Y;transform-origin(X,Y):用来设置元素的运动的基点(参照点)。默认点是元素的中心点。其中X和Y的值可以是百分 值,em,px,其中X也可以是字符参数值left,center,right;Y和X一样除了百分值外还可以设置字符值 top,center,bottom,这个看上去有点像我们background-position设置一样;下面我列出他们相对应的写法:
- top left | left top 等价于 0 0 | 0% 0%
- top | top center | center top 等价于 50% 0
- right top | top right 等价于 100% 0
- left | left center | center left 等价于 0 50% | 0% 50%
- center | center center 等价于 50% 50%(默认值)
- right | right center | center right 等价于 100% 50%
- bottom left | left bottom 等价于 0 100% | 0% 100%
- bottom | bottom center | center bottom 等价于 50% 100%
- bottom right | right bottom 等价于 100% 100%
其中 left,center right是水平方向取值,对应的百分值为left=0%;center=50%;right=100%而top center bottom是垂直方向的取值,其中top=0%;center=50%;bottom=100%;如果只取一个值,表示垂直方向值不变
过渡属性:执行变换的css属性,语法:transition-property
延迟时间:指定元素 转换过程的持续时间,语法:transition-duration
过渡效果:允许你根据时间的推进去改变属性值的变换速率,语法:transition-timing-function。点击对应按钮添加过渡效果,点击cubic-bezier()自定义立方贝塞尔曲线效果。
- 3D变形&过渡
-
透视:perspective有两种写法,一种是设置所有的子元素有一个共同的透视值,perspective作为一个属性值来写;一种是直接作用于元素本身,perspective作为transform的一个函数来写如:
.wrap{-webkit-perspective:1000px;}
.wrap .child{-webkit-transform:perspective(1000px);}X轴旋转: rotateX(angele),相当于rotate3d(1,0,0,angle)指定在3维空间内的X轴旋转,语法:transform:perspective(length) rotateX(angele);
Y轴旋转: rotateY(angele),相当于rotate3d(0,1,0,angle)指定在3维空间内的Y轴旋转,语法:transform:perspective(length) rotateY(angele);
Z轴旋转: rotateZ(angele),相当于rotate3d(0,0,1,angle)指定在3维空间内的Z轴旋转,语法:transform:perspective(length) rotateZ(angele);
X轴缩放:scaleX():表示只在X轴(水平方向)缩放元素。
Y轴缩放:scaleY():表示只在Y轴(垂直方向)缩放元素。
Z轴缩放:scaleZ():表示只在Z轴缩放元素。前提是元素本身或者元素的父元素设定了透视值
X轴位移: : 表示只在X轴(水平方向)移动元素。语法:translateX();
Y轴位移: 表示只在Y轴(水平方向)移动元素。语法:translateY();
Z轴位移: 表示只在Z轴(水平方向)移动元素。语法:translateZ();
X轴倾斜:设定X轴倾斜,对应css:transform:skewX(ndeg)
Y轴倾斜: :设定Y轴倾斜,对应css:transform:skewY(ndeg)
变形原点: 同2D
过渡属性: 同2D
过渡时间: 同2D
延迟时间: 同2D
过渡效果: 同2D
2、自定义动画
-
动画名称:自定义动画的名称, 对应css:animation-name:
执行时间:指定对象动画的持续时间,对应css: animation-duration:
延迟执行设置动画延迟执行的时间,对应css: animation-delay:
执行次数:设置对象动画的循环次数,对应css: animation-iteration-count:或infinite(无限循环)
变形原点:同变形&过渡
过渡效果:允许你根据时间的推进去改变属性值的变换速率,语法:animation-timing-function。点击对应按钮添加过渡效果,点击cubic-bezier()自定义立方贝塞尔曲线效果。
播放方向::设置对象动画循环播放次数大于1次时,动画是否反向运动。语法:animation-direction:,normal:正常方向,reverse:动画反向运行(FF14.0.1以下不支持),alternate:动画会循环正反方向交替运动,alternate-reverse:动画从反向开始,再正反方向交替运动,运动方向始终与alternate定义的相反。(FF14.0.1以下不支持)
隐藏背景:设置对象动画时间之外的状态。语法:animation-fill-mode:, none:默认值。不设置对象动画之外的状态;forwards:结束后保持动画结束时的状态,但当animation-direction为0,则动画不执行,持续保持动画开始时的状态;backwards:结束后返回动画开始时的状态;both:结束后可遵循forwards和backwards两个规则。
动画状态设置对象动画的状态,语法:animation-play-state:, running:默认值。运动;paused:暂停。
执行动画的属性: 设置动画过程中对象属性
3、动画库
-
Animate.css的动画库。Animate.css是由Dan Eden制作的CSS3动画效果合集。只要在页面引入该样式表,然后给要添加动作的元素中添加对应动画的class即可。
点击对应效果右侧查看实例,点击查看代码可直接查看对应效果的源码。
三、运用CSS3的页面案例
- 1、纯CSS3实现质感发光动画按钮
- 2、10大经典CSS3菜单应用欣赏
- 3、9种CSS3炫酷图片预览展示动画特效
css3动画从入门到精通的更多相关文章
- 《HTML5+CSS3+JavaScript 从入门到精通(标准版)》学习笔记(二)
这是一个应用的例子,学以致用嘛 <!--这些代码我就直接放在了博客园的"页首Html代码"中,用于自定义博客,效果就是页面左上角的白色文字--> <p> & ...
- 《HTML5+CSS3+JavaScript 从入门到精通(标准版)》学习笔记(一)
以下是以代码形式书写的笔记,本系列会持续更新,主要内容预计是类似下文的笔记,兼或一些思考与小项目,希望对你会有所帮助 1 <!-- --> <!DOCTYPE html>< ...
- HTML5+CSS3从入门到精通随书光盘 ISO 镜像视频教程
HTML5+CSS3从入门到精通(清华社“视频大讲堂”大系)通过基础知识+中小实例+综合案例的方式,讲述了用HTML5+ CSS3设计构建网站的必备知识,相对于权威指南.高级程序设计.开发指南同类图书 ...
- HTML5+CSS3从入门到精通 中文pdf版
HTML5+CSS3从入门到精通是通过基础知识+中小实例+综合案例的方式,讲述了用HTML5+ CSS3设计构建网站的必备知识,相对于专业指南.高级程序设计.开发指南同类图书,本书是一本适合快速入手的 ...
- css3动画入门transition、animation
css3动画 transition.animation CSS3 transition demo <!DOCTYPE html> <html> <head> < ...
- 办公软件Office PPT 2010视频教程从入门到精通系列教程(22课时)
办公软件Office PPT 2010视频教程从入门到精通系列教程(22课时) 乔布斯的成功离不开美轮美奂的幻灯片效果,一个成功的商务人士.部门经理也少不了各种各样的PPT幻灯片.绿色资源网给你提供了 ...
- 【转载】google搜索从入门到精通
原文地址:http://www.cnblogs.com/helloIT/articles/5095668.html /***************************************** ...
- 进入css3动画世界(一)
其实我做css3动画也没有多久,这篇文章目标人群是css3动画的新手,不喜勿喷. 分类 目前我接触到的css3动画有2类:一种是transition的,另一种是@keyframes的. 两者的区别就是 ...
- 进入css3动画世界(二)
进入css3动画世界(二) 今天主要来讲transition和transform入门,以后会用这两种属性配合做一些动效. 注:本文面向前端css3动画入门人员,我对这个也了解不深,如本文写的有纰漏请指 ...
随机推荐
- Android TextView常用属性
[说明] TextView是用来显示文本的组件.以下介绍的是XML代码中的属性,在java代码中同样可通过 "组件名.setXXX()方法设置.如,tv.setTextColor(); [属 ...
- Java不走弯路教程(前言)
本教程的程序基于Windows开发,所以你需要有一台安装Windows操作系统的电脑. 前言本教程将带你完成Java的初学和WEB框架的开发,学完本教程,你将完成对Java的入门并且对下一步不再迷茫. ...
- javascript 中如何判断是否是JSON格式的字符串
var stringToJson = function(value){ try{ eval('('+value+')'); return angular.fromJson(value); } catc ...
- 第一篇博客 ---- 分享关于Maven使用的一些技巧
Maven环境搭建 在官网上下载maven安装包,地址:http://maven.apache.org/download.cgi . 解压文件到电脑坐在盘符目录,如E:\apache-maven-3. ...
- [转载]致创业者:APP已死 服务永生
前几日,有位创业者和我讲他在带领团队做一个将爱踢球的人集中在一起的App,我告诉他你的创业方向错了.原因在于你的目的是要为爱踢球的人提供服务,而你现在却在竭尽全力的做App,你应该做的是设计你为爱踢球 ...
- Node.js ZLIB
Zlib 稳定性: 3 - 文档 可以通过以下方式访问这个模块: var zlib = require('zlib'); 这个模块提供了对 Gzip/Gunzip, Deflate/Inflate, ...
- 深入理解DirectByteBuffer
介绍 最近在工作中使用到了DirectBuffer来进行临时数据的存放,由于使用的是堆外内存,省去了数据到内核的拷贝,因此效率比用ByteBuffer要高不少.之前看过许多介绍DirectBuffer ...
- Redis和nosql简介,api调用;Redis数据功能(String类型的数据处理);List数据结构(及Java调用处理);Hash数据结构;Set数据结构功能;sortedSet(有序集合)数
1.Redis和nosql简介,api调用 14.1/ nosql介绍 NoSQL:一类新出现的数据库(not only sql),它的特点: 1. 不支持SQL语法 2. 存储结构跟传统关系型数 ...
- 自己创建一个android studio在线依赖compile
我正参加2016CSDN博客之星评选麻烦帮下 奖品我随机送给投票者(写一个随机数抽取) http://blog.csdn.net/vote/candidate.html?username=qfanmi ...
- SpriteKit中的共享动作(Sharing Actions)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 在SpriteKit中某些动作需要一些额外的延时,如果每次都重 ...