@wangyupu
2020-05-21T06:11:56.000000Z
字数 4979
阅读 33
HTML5+CSS
会使用transform 2D变形设置网页元素样式
会使用transition制作过渡动画
会使用animation制作网页动画
#CSS3属性制作动画
如何在网页中实现动画效果?
动态图片
Flash
JavaScript
CSS3变形
CSS3过渡
CSS3动画
CSS3变形是一些效果的集合
如平移、旋转、缩放、倾斜效果
每个效果都可以称为变形(transform),它们可以分别操控元素发生平移、旋转、缩放、倾斜等变化
transform:[transform-function]*;---`设置变形函数,可以是一个,也可以是多个,中间以空格分开`
变形函数
translate():平移函数,基于X、Y坐标重新定位元素的位置
scale():缩放函数,可以使任意元素对象尺寸发生变化
rotate():旋转函数,取值是一个度数值
skew():倾斜函数,取值是一个度数值
示例:
<style>
div{
width: 100px;
height: 100px;
background-color: red;
/*倾斜 单位deg*/
/*transform: skew(10deg,10deg)*/
/*旋转 单位deg*/
/*transform: rotate(100deg);*/
/*缩放*/
/*transform: scale(1.2);*/
/*平移*/
/*transform: translate(100px,100px);*/
}
div:hover{
}
</style>
<body>
<div></div>
</body>
tx---X轴(横坐标)移动的向量长度
ty---Y轴(横坐标)移动的向量长度
一个方向上的偏移
translateX(tx)
表示只设置X轴的位移
transform:translateX(100px)
translateY(ty)
表示只设置Y轴的位移
transform:translateY(100px)
scale(sx,sy);
sx---横向坐标(宽度)方向的缩放量
sy---纵轴坐标(高度)方向的缩放量
scale()函数可以只接收一个值,也可以接收两个值,只有一个值时,第二个值默认和第一个值相等
scaleX(sx):表示只设置X轴的缩放
示例:
transform:scaleX(2)
scaleY(sy):表示只设置Y轴的缩放
示例:
transform:scaleY(2)
skew(ax, ay);
ax---水平方向(X轴)的倾斜角度
ay---垂直方向(Y轴)的倾斜角度
可以仅设置沿着X轴或Y轴方向倾斜
skewX(ax):表示只设置X轴的倾斜
skewY(ay):表示只设置Y轴的倾斜
rotate(a);
参数a单位使用deg表示
参数a取正值时元素相对原来中心顺时针旋转
#小结
rotate( )函数只是旋转,而不会改变元素的形状
skew( )函数是倾斜,元素不会旋转,会改变元素的形状
transition呈现的是一种过渡,是一种动画转换的过程,如渐现、渐弱、动画快慢等
CSS3 transition的过渡功能更像是一种“黄油”,通过一些CSS的简单动作触发样式平滑过渡
transition:[transition-property transition-duration transition-timing-function transition-delay ]
transition-property---过渡或动态模拟的CSS属性
transition-duration---完成过渡所需要的时间
transition-timing-function--- 指定过渡函数
transition-delay---过渡开始出现的延迟时间
示例:
<style type="text/css">
img{
width: 100px;
height: 100px;
margin:100px 100px;
/*过渡*/
transition: transform 2s;
}
img:hover{
/*旋转360*/
transform: rotate(360deg) scale(1.5);
}
div{
width: 100px;
height: 100px;
background-color: red;
/*过渡 all所有属性*/
transition: all 1s linear 2s;
}
div:hover{
width: 1000px;
height: 200px;
}
</style>
<body>
<div></div>
<div></div>
<img src="img/1.jpg" />
</body>
过渡属性( transition-property )
定义转换动画的CSS属性名称
`IDENT`:指定的CSS属性(width、height、background-color属性等)
`all`:指定所有元素支持transition-property属性的样式,一般为了方便都会使用all
过渡所需的时间( transition-duration )
定义转换动画的时间长度,即从设置旧属性到换新属性所花费的时间,单位为秒(s)
过渡动画函数( transition-timing-function )
指定浏览器的过渡速度,以及过渡期间的操作进展情况,通过给过渡添加一个函数来指定动画的快慢方式
ease:速度由快到慢(默认值)
linear:速度恒速(匀速运动)
ease-in:速度越来越快(渐显效果)
ease-out:速度越来越慢(渐隐效果)
ease-in-out:速度先加速再减速(渐显渐隐效果)
过渡延迟时间( transition-delay )
指定一个动画开始执行的时间,当改变元素属性值后多长时间去执行过渡效果
正值:元素过渡效果不会立即触发,当过了设置的时间值后才会被触发
负值:元素过渡效果会从该时间点开始显示,之前的动作被截断
0:默认值,元素过渡效果立即执行
伪类触发
:hover
:active
:focus
:checked
媒体查询:通过@media属性判断设备的尺寸,方向等
JavaScript触发:用JavaScript脚本触发
<style type="text/css">
div{
width:1000px;
height: 600px;
background-color: red;
margin: 0 auto;
position: relative;
}
div img{
width: 200px;
height: 150px;
position: absolute;
}
img:nth-child(1){
left: 100px;
top: 20px;
transform: rotate(10deg);;
}
img:nth-child(2){
left:150px;
top: 60px;
transform: rotate(60deg);;
}
img:nth-child(3){
left: 300px;
top: 20px;
transform: rotate(10deg);;
}
img:nth-child(4){
left: 600px;
top: 100px;
transform: rotate(60deg);;
}
img:nth-child(5){
left: 500px;
top: 20px;
transform: rotate(100deg);;
}
img:nth-child(6){
left: 400px;
top: 150px;
transform: rotate(150deg);;
}
img:hover{
transform:rotate(0deg) scale(1.2);
z-index: 2;
}
</style>
<body>
<div>
<img src="img/1.jpg"/>
<img src="img/2.jpg"/>
<img src="img/3.jpg"/>
<img src="img/4.jpg"/>
<img src="img/5.jpg"/>
<img src="img/6.jpg"/>
</div>
</body>
在默认样式中声明元素的初始状态样式
声明过渡元素最终状态样式,如悬浮状态
在默认样式中通过添加过渡函数,添加一些不同的样式
animation动画简介
animation实现动画主要由两个部分组成
通过类似Flash动画的关键帧来声明一个动画
在animation属性中调用关键帧声明的动画实现一个更为复杂的动画效果
设置关键帧
@keyframes IDENT {
from {/*CSS样式写在这里*/}
percentage {/*CSS样式写在这里*/}
to {/*CSS样式写在这里*/}
}
@keyframes spread {
0% {width:0;}
33% {width:23px;}
66% {width:46px;}
100% {width:69px;}
}
```
#CSS3动画的使用过程5-2
```html5
@keyframes的浏览器兼容性
写兼容的时候浏览器前缀是放在@keyframes中间
例如:@-webkit-keyframes、@-moz- keyframes
CSS3动画的使用过程5-3
animation:animation-name animation–duration animation-timing-function
animation-delay animation-iteration-count animation-direction
animation-play-state animation-fill-mode;
animation:animation-name---由@keyframes
创建的动画名称
animation–duration---动画时间
animation-timing-function--- 动画方式
animation-delay---延迟时间
animation-iteration-count---动画的播放次数
animation-direction---动画的播放方向
animation-play-state---动画的播放状态
animation-fill-mode---动画开始之前和结束之后发生的操作
<div class="md-section-divider"></div>
动画的播放次数(animation-iteration-count)
值通常为整数,默认值为1
特殊值infinite,表示动画无限次播放
动画的播放方向(animation-direction)
normal,动画每次都是循环向前播放
alternate,动画播放为偶数次则向前播放
动画的播放状态(animation-play-state)
running将暂停的动画重新播放
paused将正在播放的元素动画停下来
<div class="md-section-divider"></div>
动画发生的操作(animation-fill-mode)
forwards表示动画在结束后继续应用最后关键帧的位置
backwards表示会在向元素应用动画样式时迅速应用动画的初始帧
both表示元素动画同时具有forwards和backwards的效果
示例:
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.div1{
width: 500px;
height: 100px;
background-color: yellow;
position: absolute;
right: 0;
}
.div2{
width: 500px;
height: 100px;
background-color: red;
position: absolute;
transition: all 2s;
right: 0;
top: 0;
}
.div2:hover{
width:0px;
}
</style>
<body>
<div class="div1">张三你好哈哈哈哈XXXXXYYYYY</div>
<div class="div2"></div>
</body>