css3动画从入门到精通

简介: 什么是css3动画? 通过 CSS3,我们能够创建动画,这可以在许多网页中取代动画图片、Flash 动画以及 JavaScript。 CSS3带来了圆角,半透明,阴影,渐变,多背景图等新的特征,轻松实现了设计稿中常见的图层样式,用简洁的代码替代图片,代替了多余的空标签。 现成的CSS3动画库:http://leaverou.github.io/animatable/ backg

什么是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;}
}
1

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%;}
}
1

范例二:

.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;}
}
1

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;}
}
1

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%;}
}
1

范例二

.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;}
}
1

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;}
}
1

范例二

.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;}
}
1

范例三

.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;}
}
1

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);}
}
1

范例二

.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;}
}
1

范例三

.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;}
}
1

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;}
}
1

font-size

.font-size { animation:1s ease 0s alternate none infinite font-size;}
@keyframes font-size{
    from{ font-size:60px;}
    to{ font-size:300px;}
}
1

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;}
}  
1

line-height

.line-height { animation:1s ease 0s alternate none infinite line-height;}
@keyframes line-height{
    from{ line-height:10px;}
    to{ line-height:300px;}
}  
1

opacity

.opacity { animation:1s ease 0s alternate none infinite opacity;}
@keyframes opacity{
    from{ opacity:1;}
    to{ opacity:0;}
}  
1

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;}
} 
1

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;}
}
1

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;}
}   
1

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;}
}  
1

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);}
}  
1

范例二

.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;}
}
1

范例三

.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;}
} 
1

top

.top { position:relative; animation:1s ease 0s alternate none infinite top;}
@keyframes top{
    from{ top:0;}
    to{ top:100px;}
}
1

范例一

.transform-1 { animation:1s ease 0s alternate none infinite transform-1;}
@keyframes transform-1{
    from{ transform:rotate(0deg);}
    to{ transform:rotate(360deg);}
}
1

范例二

.transform-2 { animation:1s ease 0s alternate none infinite transform-2;}
@keyframes transform-2{
    from{ transform:scale(1);}
    to{ transform:scale(2);}
}
1

范例三

.transform-3 { animation:1s ease 0s alternate none infinite transform-3;}
@keyframes transform-3{
    from{ transform:skew(0);}
    to{ transform:skew(180deg);}
}
1

范例四

.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);}
}
1

范例五

.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);}
}
1

范例六

.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);}
}
1

范例七

.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);}
}
1

范例八

.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);}
}
1

范例九

.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);}
}
1

范例十

.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);}
}
1

范例十一

.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%;}
}
1

二、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设置一样;下面我列出他们相对应的写法:

  1. top left | left top 等价于 0 0 | 0% 0%
  2. top | top center | center top 等价于 50% 0
  3. right top | top right 等价于 100% 0
  4. left | left center | center left 等价于 0 50% | 0% 50%
  5. center | center center 等价于 50% 50%(默认值)
  6. right | right center | center right 等价于 100% 50%
  7. bottom left | left bottom 等价于 0 100% | 0% 100%
  8. bottom | bottom center | center bottom 等价于 50% 100%
  9. 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炫酷图片预览展示动画特效
目录
相关文章
|
8天前
|
前端开发 JavaScript 开发工具
【HTML/CSS】入门导学篇
【HTML/CSS】入门导学篇
19 0
|
28天前
|
前端开发 内存技术
CSS动画示例(上一篇是CSS过渡…)
CSS动画示例(上一篇是CSS过渡…)
15 1
|
1月前
|
资源调度 前端开发 CDN
纯css动画库animate.css的用法
纯css动画库animate.css的用法
41 0
|
2月前
CSS3滑动轮播动画
CSS3滑动轮播动画
27 8
|
2月前
|
Web App开发 JSON 前端开发
Webpack【搭建Webpack环境、Webpack增加配置文件、Webpack中使用Loader、Webpack分离CSS文件 】(一)-全面详解(学习总结---从入门到深化)
Webpack【搭建Webpack环境、Webpack增加配置文件、Webpack中使用Loader、Webpack分离CSS文件 】(一)-全面详解(学习总结---从入门到深化)
51 0
|
3天前
|
移动开发 前端开发 JavaScript
CSS选择器 前端开发入门笔记(十)
CSS选择器 前端开发入门笔记(十)
15 1
|
3天前
|
前端开发 搜索推荐 数据安全/隐私保护
HTML标签详解 HTML5+CSS3+移动web 前端开发入门笔记(四)
HTML标签详解 HTML5+CSS3+移动web 前端开发入门笔记(四)
13 1
|
11天前
|
前端开发
css3动画
css3动画
16 0
|
28天前
|
前端开发
CSS3中的动画示例
CSS3中的动画示例
8 1
|
30天前
|
前端开发
纯css实现的3D立体鸡蛋动画视觉效果
纯css实现的3D立体鸡蛋动画视觉效果
24 6
纯css实现的3D立体鸡蛋动画视觉效果