CSS动画
过渡动画
-
允许 CSS 的属性值在一定时间区间内平滑的过渡,在鼠标点击,鼠标滑过或对元素任何改变中触发,并圆滑地以动画改变css的属性值
-
CSS3 过渡语法:
- transition:[ transition-property ] || [ transition-duration ] || [ transition-timing-function ] || [ transition-delay ]
- [ transition-property ]: 检索或设置对象中的参与过渡的属性
- all | none | property,property ...
- all: 所有可以进行过渡的css属性
- none: 不指定过渡的css属性
- property: 指定要进行过渡的css属性
- [ transition-duration ]: 检索或设置对象过渡的持续时间
- [ transition-timing-function ]: 检索或设置对象中过渡的动画类型
- transition-timing-function:linear | ease | ease-in | ease-out | ease-in-out | cubic-bezier(number, number, number, number)
- 检索或设置对象中过渡的动画类型
- 如果提供多个属性值,以逗号进行分隔
- http://cubic-bezier.com/(贝塞尔曲线生成器)
- [ transition-delay ]: 检索或设置对象延迟过渡的时间
-
练习
![]()
-
注意事项:
并不是所有元素都支持过渡
元素没有渲染完的情况下,过渡不生效
关键帧动画
关键帧
-
CSS3 动画语法-关键帧:
@keyframes name{ [keyframes-selector] { Properties:Properties value; } [keyframes-selector] { Properties:Properties value; } }- keyframes 控制关键位置,并不是所有的位置
- keyframes-selector:必写项,动画持续时间的百分比
- 0% - 100%之间, 或者使用from和to关键字也可以设置,from代表0%,to代表100%
-
案例
@keyframes move{ 0% { margin-left: 100px; background: green; } 40% { margin-left: 150px; background: orange; } 60% { margin-left: 75px; background: blue; } 100% { margin-left: 100px; background: red; } }
关键帧的使用
-
基础语法:
-
animation-name:move; /动画属性名,前面keyframes样例定义的动画名/
-
animation-duration: 10s; /动画持续时间/
-
animation-timing-function: ease-in-out; /动画帧频,和transition-timing-function是一样的//animation-timing-function:检测的是关键帧的区间,并不是整个动画过程/ease | linear | ease-in | ease-out | cubic-Bezier (n1 , n2, n3, n4)
-
animation-delay: 2s; /动画延迟时间/
-
animation-iteration-count: 10; /动画循环次数,infinite为无限次/
-
animation-direction: normal; /定义动画播放方式/
默认normal,动画正常播放; alternate,动画轮流反向播放
-
合写:animation:[
<animation-name>||<animation-duration>||<animation-timing-function>||<animation-delay>||<animation-iteration-count>||<animation-direction>]<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>关键帧动画</title> <style> .box{ width: 200px; height: 200px; background-color: #009ff2; /*animation: jello 1s infinite;*/ /*!*关键帧名称*! animation-name: move2; !*关键帧动画执行总时间,百分比是平分了总时间*! animation-duration: 1s; !*动画执行类型,在每一个帧节点之间进行动画类型*! animation-timing-function: linear; !*整个动画的延迟时间*! animation-delay: 500ms; !*动画执行的次数,可以是数字 可以是infinite*! animation-iteration-count: infinite; !*动画播放方式:可以选择动画轮流反向播放*! animation-direction: alternate;*/ /*合写:按照刚才书写的顺序 依次书写即可 不用的可以省略*/ animation:move2 3s linear .5s infinite alternate; } @keyframes move{ 0%{ /*background-color: black;*/ } 50%{ /*变形在检测0%的时候没有这个属性,50%出现了,就会在0%到50%之间过渡*/ transform: rotateX(30deg); background-color: green; } 100%{ background-color: pink; } } @keyframes move2 { 0%{ margin-left: 0px; background-color: pink; } 25%{ margin:0 0 0 1000px; transform: scale(1.3); } 50%{ margin: 400px 0 0 1000px; } 100%{ margin: 400px 0 0 0; } /*百分百不写就是默认*/ } @keyframes swing { 20% { -webkit-transform: rotate3d(0, 0, 1, 15deg); transform: rotate3d(0, 0, 1, 15deg); } 40% { -webkit-transform: rotate3d(0, 0, 1, -10deg); transform: rotate3d(0, 0, 1, -10deg); } 60% { -webkit-transform: rotate3d(0, 0, 1, 5deg); transform: rotate3d(0, 0, 1, 5deg); } 80% { -webkit-transform: rotate3d(0, 0, 1, -5deg); transform: rotate3d(0, 0, 1, -5deg); } to { -webkit-transform: rotate3d(0, 0, 1, 0deg); transform: rotate3d(0, 0, 1, 0deg); } } @-webkit-keyframes jello { from, 11.1%, to { -webkit-transform: translate3d
-

