CSS 变换与动画
GPU Animation
动画
- 基本
style 可动画参数
参数名称 | 说明 | |
---|---|---|
width | { width: 100 } 元素当前宽度到 |
|
maxWidth | { maxWidth: 100 } 元素当前最大宽度到 |
|
minWidth | { minWidth: 100 } 元素当前最小宽度到 |
|
height | { height: 100 } 元素当前高度到 |
|
maxHeight | { maxHeight: 100 } 元素当前最大高度到 |
|
minHeight | { minHeight: 100 } 元素当前最小高度到 |
|
lineHeight | { lineHeight: 100 } 区块行高到 |
|
opacity | { opacity: 0 } 元素当前透明度到 |
|
top | { top: 100 } 元素当前顶部距离到 |
absolute` |
right | { right: 100 } 元素当前右部距离到 |
absolute` |
bottom | { bottom: 100 } 元素当前下部距离到 |
absolute` |
left | { left: 100 } 元素当前左部距离到 |
absolute` |
marginTop | { marginTop: 100 } 元素当前顶部外边距离到 |
|
marginRight | { marginRight: 100 } 元素当前右部外边距离到 |
|
marginBottom | { marginBottom: 100 } 元素当前下部外边距离到 |
|
marginLeft | { marginLeft: 100 } 元素当前左部外边距离到 |
|
paddingTop | { paddingTop: 100 } 元素当前顶部内边距离到 |
|
paddingRight | { paddingRight: 100 } 元素当前右部内边距离到 |
|
paddingBottom | { paddingBottom: 100 } 元素当前下部内边距离到 |
|
paddingLeft | { paddingLeft: 100 } 元素当前左部内边距离到 |
|
color | { color: '#FFFFFF' } 元素当前文字颜色到白色 |
|
backgroundColor | { backgroundColor: '#FFFFFF' } 元素当前背景颜色到白色 |
|
borderWidth | { borderWidth: 2 } 元素当前边框宽度到borderTopWidth borderRightWidth``borderBottomWidth borderLeftWidth |
|
borderRadius | { borderRadius: 5 } 元素当前圆角到上 左 下 右 |
|
borderColor | { borderColor: '#FFFFFF' } 元素当前边框颜色到白色 |
|
boxShadow | { boxShadow: '0 0 10px #000' } 元素当前阴影模糊到 |
|
textShadow | { textShadow: '0 0 10px #000' } 元素当前文字内容阴影模糊到 |
transform 参数
参数名称 | 说明 |
---|---|
translateX / x | { translateX: 10 } or { x: 10 } => transform: translateX(10px) |
translateY / y | { translateY: 10 } or { y: 10 } => transform: translateY(10px) |
translateZ / z | { translateZ: 10 } or { z: 10 } => transform: translateZ(10px) |
rotate | { rotate: 10 } => transform: rotate(10deg) 元素以 |
rotateX | { rotateX: 10 } => transform: rotateX(10deg) 元素以 |
rotateY | { rotateY: 10 } => transform: rotateY(10deg) 元素以 |
scale | { scale: 0 } => transform: scale(0) 元素以 |
scaleX | { scaleX: 0 } => transform: scaleX(0) 元素以 |
scaleY | { scaleY: 0 } => transform: scaleY(0) 元素以 |
transformOrigin | { transformOrigin: '50px 50px'} 元素当前中心点到 |
filter 参数
参数名称 | 说明 |
---|---|
grayScale | { grayScale: 1 } 元素 |
sepia | { sepia: 1 } 元素 |
hueRotate | { hueRotate: '90deg' } 元素 |
invert | { invert: 1 } 元素 |
brightness | { brightness: 2 } 元素 |
contrast | { contrast: 2 } 对比度到 |
saturate | { saturate: 2 } 饱和度到 |
blur | { blur: '20px' } 模糊到 |
CSS-Animation&Transition
transition
过渡、transform 变换
和 animation 动画
来实现页面中的一些样式转化
Transition( 变换)
transition : transition-property transition-duration transition-timing-function transition-delay [, ...]
- transition-property
用来指定执行
- transition-duration
动画执行的持续时间,单位为
- transition-timing-function
变换速率效果,可选值为
- transition-delay
用来指定动画开始执行的时间,取值同
一个最简单的例子如下所示:
<div style="height:150px;">
<h2><span></span>热门网站</h2>
<ul>
<li><a href="http://info.3g.qq.com/g/s?aid=index&g_f=2543">腾讯</a></li>
<li><a href="http://m.sohu.com/?_trans_=000012_qq_dh">搜狐</a></li>
<li><a href="http://3g.163.com/links/3810">网易</a></li>
</ul>
</div>
相对应的
.main {
overflow: hidden;
-webkit-transition: all 0.5s ease-in 0s;
-moz-transition: all 0.5s ease-in 0s;
-o-transition: all 0.5s ease-in 0s;
transition: all 0.5s ease-in 0s;
background: #fff;
}
.main .close {
height: 0 !important;
}
上面代码会使得类名为
Transform
2D
其主要包含以下几种变换
transform: rotate | scale | skew | translate |matrix;
-
rotate 旋转rotate 的单位是deg 度
,正数表示顺时针旋转,负数表示逆时针旋转。
DEMO: http://codepen.io/CodingMonkeyzh/pen/XbNYOa
-
scale 缩放scale 的取值范围是0~n
,小于 1
时表示缩小,反之表示放大。例如 scale(0.5, 2)
表示水平方向缩小scaleX
或者 scaleY
对一个方向进行设置。
DEMO: http://codepen.io/CodingMonkeyzh/pen/doOKrg
-
skew 扭曲skew 的单位跟rotate
一样都是
deg 度
。例如 skew(30deg, 10deg)
表示水平方向倾斜
DEMO: http://codepen.io/CodingMonkeyzh/pen/KpNeYg
-
translate 偏移偏移同样包括水平偏移和垂直偏移。
translate(x,y)
水平方向和垂直方向同时移动
( 也就是X 轴和Y 轴同时移动) ;translateX(x)
仅水平方向移动
(X 轴移动) ;translateY(Y)
仅垂直方向移动
(Y 轴移动) 。
Animation
Keyframes
Keyframes 关键帧
的玩意来控制的,他的命名是由
@keyframes IDENT {
from {
properties: Properties value;
}
Percentage {
properties: Properties value;
}
to {
properties: Properties value;
}
}
@keyframes IDENT {
0% {
properties: Properties value;
}
Percentage {
properties: Properties value;
}
100% {
properties: Properties value;
}
}
animation
- animation-name—
- 规定需要绑定到选择器的keyframe 名称。
语法:animation-name: keyframename|none;
Keyframename:规定需要绑定到选择器的
例如:
{
-webkit-animation-name: my_animation;
-moz-animation-name : my_animation;
-ms-animation-name : my_animation;
-o-animation-name: my_animation;
animation-name: my_animation;
}
@-webkit-keyframes my_animation{}
@-moz-keyframes my_animation{}
@-ms-keyframes my_animation{}
@-o-keyframes my_animation{}
@keyframes my_animation{}
- animation-duration—
- 规定完成动画所花费的时间,以秒或毫秒计。
语法:animation-duration: time;
例如:
{
-webkit-animation-duration: 2s;
-moz-animation-duration : 2s;
-ms-animation-duration : 2s;
-o-animation-duration: 2s;
animation–duration: 2s;
}
- animation-timing-function—
- 规定动画的速度曲线
语法
linear:动画从头到尾的速度是相同的。
ease:默认。动画以低速开始,然后加快,在结束前变慢。
ease-in:动画以低速开始。
ease-out:动画以低速结束。
ease-in-out:动画以低速开始和结束。
cubic-bezier(n,n,n,n):在
例如:
{animation-timing-function:linear;}
{animation-timing-function:ease;}
{animation-timing-function:ease-in;}
{animation-timing-function:ease-out;}
{animation-timing-function:ease-in-out;}
- animation-delay—
- 规定在动画开始之前的延迟
语法
{
animation-delay:2s;
-webkit-animation-delay:2s;
}
- animation-iteration-count—
- 规定动画应该播放的次数
语法
n:定义动画播放次数的数值。
infinite:规定动画应该无限次播放。默认值为:1。
示例:
{
animation-iteration-count:infinite;
-webkit-animation-iteration-count:infinite;
}
- animation-direction—
- 规定是否应该轮流反向播放动画
语法
normal:默认值。动画应该正常播放。
alternate:动画应该轮流反向播放。
注释:如果把动画设置为只播放一次,则该属性没有效果。
示例:
{
animation-direction:alternate;
-webkit-animation-direction:alternate;
}
animation-play-state 属性规定动画正在运行还是暂停
语法
paused:规定动画已暂停。
running:规定动画正在播放。
注释:可以在
示例:
{
animation-play-state:running;
-webkit-animation-play-state:running;
}
animation-fill-mode 属性规定动画在播放之前或之后,其动画效果是否可见
语法
none:不改变默认行为。
forwards:当动画完成后,保持最后一个属性值
backwards:在
both:向前和向后填充模式都被应用。
JavaScript-Animation
requestAnimationFrame
tick
/* tick https://github.com/AlloyTeam/AlloyTouch/blob/master/transformjs/asset/tick.js
* By dntzhang|当耐特
*/
(function () {
if (!Date.now)
Date.now = function () {
return new Date().getTime();
};
var vendors = ["webkit", "moz"];
for (var i = 0; i < vendors.length && !window.requestAnimationFrame; ++i) {
var vp = vendors[i];
window.requestAnimationFrame = window[vp + "RequestAnimationFrame"];
window.cancelAnimationFrame =
window[vp + "CancelAnimationFrame"] ||
window[vp + "CancelRequestAnimationFrame"];
}
if (
/iP(ad|hone|od).*OS 6/.test(window.navigator.userAgent) || // iOS6 is buggy
!window.requestAnimationFrame ||
!window.cancelAnimationFrame
) {
var lastTime = 0;
window.requestAnimationFrame = function (callback) {
var now = Date.now();
var nextTime = Math.max(lastTime + 16, now);
return setTimeout(function () {
callback((lastTime = nextTime));
}, nextTime - now);
};
window.cancelAnimationFrame = clearTimeout;
}
var tickArr = [];
var tick = function (fn) {
tickArr.push(fn);
};
var execTick = function () {
var i = 0,
len = tickArr.length;
for (; i < len; i++) {
tickArr[i]();
}
requestAnimationFrame(execTick);
};
execTick();
window.tick = tick;
})();