一、颜色渐变:background: linear-gradient(direction, color-stop1, color-stop2, ...);
参数:direction: 方向或者角度; color-stop1, color-stop2, ... :指定渐变的起止颜色,可以使用rgba()函数设定透明色。
例:<button class="button"></button>
.button{
/* Safari 5.1 - 6.0 */
background: -webkit-linear-gradient(left, orange, yellow);
/* Opera 11.1 - 12.0 */
background: -o-linear-gradient(right, orange, yellow);
/* Firefox 3.6 - 15 */
background: -moz-linear-gradient(right, orange, yellow);
/* 标准的语法(必须放在最后) */
background: linear-gradient(to right, orange, yellow);
}
二、元素的2D/3D转换:transform属性
1. 浏览器支持
Internet Explorer 10、Firefox 以及 Opera 支持 transform 属性。
Chrome 和 Safari 需要前缀 -webkit-。
注释:Internet Explorer 9 需要前缀 -ms-。
2. transform属性
转换方法:
(1) matrix(n,n,n,n,n,n): 定义 2D 转换,使用六个值的矩阵。[3D:matrix3d(n,n,n,n,n,n,
n,n,n,n,n,n,n,n,n,n)16个值的4*4矩阵]
(2) translate(x,y):定义 2D移动转换。可只在X或Y轴进行位置变换(translateX(x)、translateY(y))。[3D:translate3d(x,y,z)]
兼容:
-ms-transform: translate(x,y);
-webkit-transform: translate(x,y);
-o-transform: translate(x,y);
-moz-transform: translate(x,y);
例:垂直居中
<div class='father'><div class='child'>child</div></div>
.father{width: 100px; height: 100px; border:1px solid #000000}
.child{ position:relative; top: 50%; transform: translateY(-50%)}
(3) scale(x,y):定义 2D 缩放转换。可只在X或Y轴定义缩放转换(scaleX(x)、scaleY(y))。[3D:scale3d(x,y,z)]
(4) rotate(angle):定义 2D 旋转,在参数中规定角度。可只沿X或Y轴定义旋转(rotateX(angle)、rotateY(angle))。[3D:rotate3d(x,y,z,angle)]
例:<div class="rotate">rotate</div>
.rotate{
/* Internet Explorer */
-ms-transform: rotate(180deg);
/* Firefox */
-moz-transform: rotate(180deg);
/* Safari 和 Chrome */
-webkit-transform: rotate(180deg);
/* Opera */
-o-transform: rotate(180deg);
/* 标准 */
transform: rotate(180deg);
}
(5) skew(x-angle,y-angle): 定义沿着 X 和 Y 轴的 2D 倾斜转换。可只沿X或Y轴定义2D倾斜转换(skewX(angle)、skewY(angle))。
(6) perspective(n): 为 3D 转换元素定义透视视图。
三、过渡效果:transition属性
1. 浏览器支持
Internet Explorer 10、Firefox、Chrome 以及 Opera 支持 transition 属性。
Safari 需要前缀 -webkit-。
注释:Internet Explorer 9 以及更早的版本,不支持 transition 属性。
注释:Chrome 25 以及更早的版本,需要前缀 -webkit-。
2. transition 属性
transition 属性是一个简写属性,用于设置四个过渡属性:
- transition-property: 规定设置过渡效果的 CSS 属性的名称。(transform、height、width等等)
- transition-duration: 规定完成过渡效果需要多少秒或毫秒。
- transition-timing-function: 规定速度效果的速度曲线。[ linear: 规定以相同速度开始至结束的过渡效果(等于 cubic-bezier(0,0,1,1)); ease: 规定慢速开始,然后变快,然后慢速结束的过渡效果(cubic-bezier(0.25,0.1,0.25,1)); ease-in: 规定以慢速开始的过渡效果(等于 cubic-bezier(0.42,0,1,1)); ease-out: 规定以慢速结束的过渡效果(等于 cubic-bezier(0,0,0.58,1)); ease-in-out: 规定以慢速开始和结束的过渡效果(等于 cubic-bezier(0.42,0,0.58,1)); cubic-bezier(n,n,n,n): 在 cubic-bezier 函数中定义自己的值。可能的值是 0 至 1 之间的数值。
- transition-delay: 定义过渡效果何时开始。
注释:请始终设置 transition-duration 属性,否则时长为 0,就不会产生过渡效果。
例:<div class="trans">height</div>
.trans {
height: 30px;
width: 100px;
/* Internet Explorer */
-ms-transition: height 2s ease-out;
/* Firefox */
-moz-transition: height 2s ease-out;
/* Safari 和 Chrome */
-webkit-transition: height 2s ease-out;
/* Opera */
-o-transition: height 2s ease-out;
/* 标准 */
transition: height 2s ease-out;
}
.trans:hover{
height: 100px;
}
四、动画: @keyframes 规则, animation 属性
1. 浏览器支持:
Internet Explorer 10、Firefox 以及 Opera 支持 @keyframes 规则和 animation 属性。
Chrome 和 Safari 需要前缀 -webkit-。
注释:Internet Explorer 9,以及更早的版本,不支持 @keyframe 规则或 animation 属性。
2. @keyframes规则
@keyframes 规则用于创建动画。在 @keyframes 中规定某项 CSS 样式,就能创建由当前样式逐渐改为新样式的动画效果。
例:
@keyframes myfirst
{
from {background: red;}
to {background: yellow;}
}
@-moz-keyframes myfirst /* Firefox */
{
from {background: red;}
to {background: yellow;}
}
@-webkit-keyframes myfirst /* Safari 和 Chrome */
{
from {background: red;}
to {background: yellow;}
}
@-o-keyframes myfirst /* Opera */
{
from {background: red;}
to {background: yellow;}
}
在 @keyframes 中创建动画时,需要把它捆绑到某个选择器,否则不会产生动画效果。
通过规定至少两项(此两项为:规定动画的名称、规定动画的时长) CSS3 动画属性,即可将动画绑定到选择器。
例:
div
{
animation: myfirst 5s;
-moz-animation: myfirst 5s; /* Firefox */
-webkit-animation: myfirst 5s; /* Safari 和 Chrome */
-o-animation: myfirst 5s; /* Opera */
}
3. animation属性
动画是使元素从一种样式逐渐变化为另一种样式的效果,可以改变任意多的样式任意多的次数。可用 百分比 来规定变化发生的时间,也可用 关键词 "from" 和 "to",等同于 0% 和 100%,0% 是动画的开始,100% 是动画的完成。为了得到最佳的浏览器支持,应该始终定义 0% 和 100% 选择器。
例:
@keyframes myfirst
{
0% {background: red; left:0px; top:0px;}
25% {background: yellow; left:200px; top:0px;}
50% {background: blue; left:200px; top:200px;}
75% {background: green; left:0px; top:200px;}
100% {background: red; left:0px; top:0px;}
}
@-moz-keyframes myfirst /* Firefox */
{
0% {background: red; left:0px; top:0px;}
25% {background: yellow; left:200px; top:0px;}
50% {background: blue; left:200px; top:200px;}
75% {background: green; left:0px; top:200px;}
100% {background: red; left:0px; top:0px;}
}
@-webkit-keyframes myfirst /* Safari 和 Chrome */
{
0% {background: red; left:0px; top:0px;}
25% {background: yellow; left:200px; top:0px;}
50% {background: blue; left:200px; top:200px;}
75% {background: green; left:0px; top:200px;}
100% {background: red; left:0px; top:0px;}
}
@-o-keyframes myfirst /* Opera */
{
0% {background: red; left:0px; top:0px;}
25% {background: yellow; left:200px; top:0px;}
50% {background: blue; left:200px; top:200px;}
75% {background: green; left:0px; top:200px;}
100% {background: red; left:0px; top:0px;}
}
所有动画属性:
animation-name:规定 @keyframes 动画的名称。(animation-name为实现动画必加属性)
animation-duration:规定动画完成一个周期所花费的秒或毫秒。默认是 0。(animation-duration为实现动画必加属性)
animation-timing-function: 规定动画的速度曲线。默认是 "ease"。animation-timing-function的所有值:
- linear:动画从头到尾的速度是相同的;
- ease:默认。动画以低速开始,然后加,在结束前变慢。
- ease-in:动画以低速开始。
- ease-out:动画以低速结束。
- ease-in-out:动画以低速开始和结束。
- cubic-bezier(n,n,n,n):在 cubic-bezier (三次贝塞尔曲线)函数中自己的值。可能的值是从 0 到 1 的数值。
animation-delay:规定动画何时开始。默认是 0。
animation-iteration-count:规定动画被播放的次数。默认是 1。
animation-direction:规定动画是否在下一周期逆向地播放。默认是 "normal"。animation-direction的全部取值:
- normal:默认值。动画应该正常播放。
- alternate:动画应该轮流反向播放。
animation-play-state:规定动画是否正在运行或暂停。默认是 "running"。animation-play-state的全部取值:
- paused:规定动画已暂停。
- running:规定动画正在播放。
animation-fill-mode:规定对象动画时间之外的状态。
- none:不改变默认行为。
- forwards:当动画完成后,保持最后一个属性值(在最后一个关键帧中定义)。
- backwards:在 animation-delay 所指定的一段时间内,在动画显示之前,应用开始属性值(在第一个关键帧中定义)。
- both:向前和向后填充模式都被应用。
animation 属性是除了 animation-play-state 属性 外所有动画属性的简写属性。
例:
全部动画属性
div
{
animation-name: myfirst;
animation-duration: 5s;
animation-timing-function: linear;
animation-delay: 2s;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-play-state: running;
/* Firefox: */
-moz-animation-name: myfirst;
-moz-animation-duration: 5s;
-moz-animation-timing-function: linear;
-moz-animation-delay: 2s;
-moz-animation-iteration-count: infinite;
-moz-animation-direction: alternate;
-moz-animation-play-state: running;
/* Safari 和 Chrome: */
-webkit-animation-name: myfirst;
-webkit-animation-duration: 5s;
-webkit-animation-timing-function: linear;
-webkit-animation-delay: 2s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-direction: alternate;
-webkit-animation-play-state: running;
/* Opera: */
-o-animation-name: myfirst;
-o-animation-duration: 5s;
-o-animation-timing-function: linear;
-o-animation-delay: 2s;
-o-animation-iteration-count: infinite;
-o-animation-direction: alternate;
-o-animation-play-state: running;
}
简写的animation属性
div
{
animation: myfirst 5s linear 2s infinite alternate;
/* Firefox: */
-moz-animation: myfirst 5s linear 2s infinite alternate;
/* Safari 和 Chrome: */
-webkit-animation: myfirst 5s linear 2s infinite alternate;
/* Opera: */
-o-animation: myfirst 5s linear 2s infinite alternate;
}
transition 和 animation 区别:transition一般需通过浏览器直接改变元素的CSS属性来实现,比如css选择器或者事件触发,animation则不需要触发任何事件,可以显式的随时间变化改变元素的CSS属性。