过渡和动画都是CSS3
的重要部分,今天有时间,了解些相关内容并记录下。
在开始以前,首先看看CSS3
的转化。css
CSS3
的转化分为如下几种:web
每种转化都还有对应的3d版本性能
注意:闭合的内联元素不支持转化,过渡和动画:如<span>、<small>、<i>等。能够经过添加样式 display: inline-block 或 display: block 来转化成块级元素。
字体
translate()方法,根据左(X轴)和顶部(Y轴)位置给定的参数,从当前元素位置移动。动画
span { display: inline-block; transform: translate(50px,100px); /* 向下移动50像素,向右移动100像素 */ -ms-transform: translate(50px,100px); /* IE 9 */ -webkit-transform: translate(50px,100px); /* Safari and Chrome */ }
rotate()方法,在一个给定度数顺时针旋转的元素。负值是容许的,这样是元素逆时针旋转。spa
i { display: block; transform: rotate(30deg); /* 顺时针旋转30度,负数为逆时针旋转 */ -ms-transform: rotate(30deg); /* IE 9 */ -webkit-transform: rotate(30deg); /* Safari and Chrome */ }
scale()方法,该元素增长或减小的大小,取决于宽度(X轴)和高度(Y轴)的参数:3d
div { display: inline; /*这样设置是缩放不生效 */ transform: scale(2,3); /*长扩大2倍,宽扩大3倍,小于1的小数为缩小 */ -ms-transform:scale(2,3); /* IE 9 */ -webkit-transform: scale(2,3); /* Safari */ transform: scale(2,3); /* 标准语法 */ }
skew() 包含两个参数值,分别表示X轴和Y轴倾斜的角度,若是第二个参数为空,则默认为0,参数为负表示向相反方向倾斜。(想象成一根直的柱子,被推歪了...)code
div { transform: skew(30deg,20deg); -ms-transform: skew(30deg,20deg); /* IE 9 */ -webkit-transform: skew(30deg,20deg); /* Safari and Chrome */ }
matrix()方法和2D变换方法合并成一个。
matrix 方法有六个参数,包含旋转,缩放,移动(平移)和倾斜功能。orm
div { transform:matrix(0.866,0.5,-0.5,0.866,0,0); -ms-transform:matrix(0.866,0.5,-0.5,0.866,0,0); /* IE 9 */ -webkit-transform:matrix(0.866,0.5,-0.5,0.866,0,0); /* Safari and Chrome */ }
2D 版ip
3D 版
CSS3中,咱们为了添加某种效果能够从一种样式转变到另外一个的时候,无需使用Flash动画或JavaScript。
过渡和动画均可以改变元素的样式,但他们中间也有些不一样:
总的来讲:通常简单的样式使用过渡,实在须要复杂的效果再考虑使用动画
div { transition-property: width; /*可使用转化更好的过渡 */ transition-duration: 1s; transition-timing-function: linear; transition-delay: 2s; /* Safari */ -webkit-transition-property:width; -webkit-transition-duration:1s; -webkit-transition-timing-function:linear; -webkit-transition-delay:2s; } /*简写 */ div { transition: width 1s linear 2s; /* Safari */ -webkit-transition:width 1s linear 2s; }
CSS3,咱们能够建立动画,它能够取代许多网页动画图像,Flash动画,和JAVAScripts。
动画属性:
1.经过@keyframes规则建立动画
/*建立动画,字体颜色由红变蓝 */ @keyframes changeColor { from { color: red; } to { color: blue; } }
2.元素绑定动画
span { display: inline-block; /*内联元素要转成block元素 */ animation: changeColor 1s linear; /*绑定动画,并设置动画时间和执行曲线 */ }
动画还可使用百分比来更加精细的控制动画流程:
@keyframes changeColor { 0% {color: red;} 25% {color: yellow;} 50% {color: green;} 75% {color: pickle;} 100% {color: blue;} } /* Safari 与 Chrome */ @-webkit-keyframes changeColor { 0% {color: red;} 25% {color: yellow;} 50% {color: green;} 75% {color: pickle;} 100% {color: blue;} }