# css3 动画有哪些?# 一、是什么CSS 动画(CSS Animations)是为层叠样式表建议的允许可扩展标记语言(XML)元素使用 CSS 的动画的模块
即指元素从一种样式逐渐过渡为另一种样式的过程
常见的动画效果有很多,如平移、旋转、缩放等等,复杂动画则是多个简单动画的组合
css实现动画的方式,有如下几种:
transition 实现渐变动画transform 转变动画animation 实现自定义动画# 二、实现方式# transition 实现渐变动画transition的属性如下:
property:填写需要变化的 css 属性duration:完成过渡效果需要的时间单位(s 或者 ms)timing-function:完成效果的速度曲线delay: 动画效果的延迟触发时间其中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 之间的数值注意:并不是所有的属性都能使用过渡的,如display:none<->display:block
举个例子,实现鼠标移动上去发生变化动画效果
.base {
width: 100px;
height: 100px;
display: inline-block;
background-color: #0ea9ff;
border-width: 5px;
border-style: solid;
border-color: #5daf34;
transition-property: width, height, background-color, border-width;
transition-duration: 2s;
transition-timing-function: ease-in;
transition-delay: 500ms;
}
/*简写*/
/*transition: all 2s ease-in 500ms;*/
.base:hover {
width: 200px;
height: 200px;
background-color: #5daf34;
border-width: 10px;
border-color: #3a8ee6;
}
# transform 转变动画包含四个常用的功能:
translate:位移scale:缩放rotate:旋转skew:倾斜一般配合transition过度使用
注意的是,transform不支持inline元素,使用前把它变成block
举个例子
.base {
width: 100px;
height: 100px;
display: inline-block;
background-color: #0ea9ff;
border-width: 5px;
border-style: solid;
border-color: #5daf34;
transition-property: width, height, background-color, border-width;
transition-duration: 2s;
transition-timing-function: ease-in;
transition-delay: 500ms;
}
.base2 {
transform: none;
transition-property: transform;
transition-delay: 5ms;
}
.base2:hover {
transform: scale(0.8, 1.5) rotate(35deg) skew(5deg) translate(15px, 25px);
}
可以看到盒子发生了旋转,倾斜,平移,放大
# animation 实现自定义动画animation是由 8 个属性的简写,分别如下:
属性描述属性值animation-duration指定动画完成一个周期所需要时间,单位秒(s)或毫秒(ms),默认是 0animation-timing-function指定动画计时函数,即动画的速度曲线,默认是 "ease"linear、ease、ease-in、ease-out、ease-in-outanimation-delay指定动画延迟时间,即动画何时开始,默认是 0animation-iteration-count指定动画播放的次数,默认是 1animation-direction 指定动画播放的方向默认是 normalnormal、reverse、alternate、alternate-reverseanimation-fill-mode指定动画填充模式。默认是 noneforwards、backwards、bothanimation-play-state指定动画播放状态,正在运行或暂停。默认是 runningrunning、pauseranimation-name指定 @keyframes 动画的名称CSS 动画只需要定义一些关键的帧,而其余的帧,浏览器会根据计时函数插值计算出来,
通过 @keyframes 来定义关键帧
因此,如果我们想要让元素旋转一圈,只需要定义开始和结束两帧即可:
@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
from 表示最开始的那一帧,to 表示结束时的那一帧
也可以使用百分比刻画生命周期
@keyframes rotate {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(180deg);
}
100% {
transform: rotate(360deg);
}
}
定义好了关键帧后,下来就可以直接用它了:
animation: rotate 2s;
# 三、总结属性含义transition(过度)用于设置元素的样式过度,和 animation 有着类似的效果,但细节上有很大的不同transform(变形)用于元素进行旋转、缩放、移动或倾斜,和设置样式的动画并没有什么关系,就相当于 color 一样用来设置元素的“外表”translate(移动)只是 transform 的一个属性值,即移动animation(动画)用于设置动画属性,他是一个简写的属性,包含 6 个属性# 参考文献https://segmentfault.com/a/1190000022540857https://zh.m.wikipedia.org/wiki/CSS%E5%8A%A8%E7%94%BBhttps://vue3js.cn/interview